Search in sources :

Example 91 with HttpResponse

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class DeviceOperationsTest method invokeThrowOnUserAgentFailed.

/* Tests_SRS_DEVICE_OPERATIONS_21_012: [The request shall add to the HTTP header a `User-Agent` key with the client Id and service version.] */
@Test(expected = IllegalArgumentException.class)
public void invokeThrowOnUserAgentFailed(@Mocked IotHubServiceSasToken iotHubServiceSasToken, @Mocked HttpRequest httpRequest) throws Exception {
    // arrange
    new NonStrictExpectations() {

        {
            iotHubServiceSasToken.toString();
            result = STANDARD_SASTOKEN_STRING;
            httpRequest.setReadTimeoutMillis(DEFAULT_HTTP_TIMEOUT_MS);
            result = httpRequest;
            httpRequest.setHeaderField(AUTHORIZATION, STANDARD_SASTOKEN_STRING);
            result = httpRequest;
            httpRequest.setHeaderField(REQUEST_ID, STANDARD_REQUEST_ID);
            result = httpRequest;
            httpRequest.setHeaderField(USER_AGENT, anyString);
            result = new IllegalArgumentException();
        }
    };
    // act
    HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, new URL(STANDARD_URL), HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, 0);
}
Also used : HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL) Test(org.junit.Test)

Example 92 with HttpResponse

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class IotHubExceptionManagerTest method httpResponseVerification400WithInnerMessageAndException.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_013: [If the httpresponse contains a reason message, the function must print this reason in the error message]
// Assert
@Test
public void httpResponseVerification400WithInnerMessageAndException() throws IotHubException {
    // Arrange
    final int status = 400;
    final byte[] body = { 1 };
    final Map<String, List<String>> headerFields = new HashMap<>();
    final byte[] errorReason = "{\"Message\":\"ErrorCode:ArgumentInvalid;Error: BadRequest {\\\"Message\\\":\\\"ErrorCode:ArgumentInvalid;Missing or invalid etag for job type ScheduleUpdateTwin. ScheduleUpdateTwin job type is a force update, which only accepts '*' as the Etag.\\\",\\\"ExceptionMessage\\\":\\\"Tracking ID:1234-TimeStamp:06/26/2017 20:56:33\\\"}\",\"ExceptionMessage\":\"Tracking ID:5678-G:10-TimeStamp:06/26/2017 20:56:33\"}".getBytes(StandardCharsets.UTF_8);
    HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
    // Act
    try {
        IotHubExceptionManager.httpResponseVerification(response);
        assert true;
    } catch (IotHubBadFormatException expected) {
        // Expected throw.
        assertThat(expected.getMessage(), is("ErrorCode:ArgumentInvalid;Missing or invalid etag for job type ScheduleUpdateTwin. ScheduleUpdateTwin job type is a force update, which only accepts '*' as the Etag. Tracking ID:1234-TimeStamp:06/26/2017 20:56:33"));
    }
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) Test(org.junit.Test)

Example 93 with HttpResponse

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class IotHubExceptionManagerTest method httpResponseVerification409.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_34_018: [The function shall throw IotHubConflictException if the Http response status equal 409]
// Assert
@Test(expected = IotHubConflictException.class)
public void httpResponseVerification409() throws IotHubException {
    // Arrange
    final int status = 409;
    final byte[] body = { 1 };
    final Map<String, List<String>> headerFields = new HashMap<>();
    final byte[] errorReason = { 2, 3, 4, 5 };
    HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
    // Act
    IotHubExceptionManager.httpResponseVerification(response);
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) Test(org.junit.Test)

Example 94 with HttpResponse

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class IotHubExceptionManagerTest method httpResponseVerification400WithNULLErrorReason.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_013: [If the httpresponse contains a reason message, the function must print this reason in the error message]
// Assert
@Test
public void httpResponseVerification400WithNULLErrorReason() throws IotHubException {
    // Arrange
    final int status = 400;
    final byte[] body = { 1 };
    final Map<String, List<String>> headerFields = new HashMap<>();
    final byte[] errorReason = "{\"ExceptionMessage\":null}".getBytes(StandardCharsets.UTF_8);
    HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
    // Act
    try {
        IotHubExceptionManager.httpResponseVerification(response);
        assert true;
    } catch (IotHubBadFormatException expected) {
        // Expected throw.
        assertThat(expected.getMessage(), is("{\"ExceptionMessage\":null}"));
    }
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) Test(org.junit.Test)

Example 95 with HttpResponse

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.

the class IotHubExceptionManagerTest method httpResponseVerification502.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_008: [The function shall throw IotHubBadGatewayException if the Http response status equal 502]
// Assert
@Test(expected = IotHubBadGatewayException.class)
public void httpResponseVerification502() throws IotHubException {
    // Arrange
    final int status = 502;
    final byte[] body = { 1 };
    final Map<String, List<String>> headerFields = new HashMap<>();
    final byte[] errorReason = { 2, 3, 4, 5 };
    HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
    // Act
    IotHubExceptionManager.httpResponseVerification(response);
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) Test(org.junit.Test)

Aggregations

HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)130 Test (org.junit.Test)91 HashMap (java.util.HashMap)63 List (java.util.List)60 URL (java.net.URL)56 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)39 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)24 LinkedList (java.util.LinkedList)16 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)14 IOException (java.io.IOException)10 DeviceParser (com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser)8 Proxy (java.net.Proxy)7 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)6 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)6 Map (java.util.Map)6 IotHubBadFormatException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException)5 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)5 ConfigurationParser (com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser)4 QueryRequestParser (com.microsoft.azure.sdk.iot.deps.serializer.QueryRequestParser)4 MalformedURLException (java.net.MalformedURLException)4