Search in sources :

Example 71 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 invoke_httprequesttimeout_succeed.

/* Tests_SRS_DEVICE_OPERATIONS_21_017: [If the resulted status represents success, the request shall return the http response.] */
@Test
public void invoke_httprequesttimeout_succeed(@Mocked IotHubServiceSasToken iotHubServiceSasToken, @Mocked HttpRequest httpRequest) throws Exception {
    //arrange
    // 30 seconds
    final long responseTimeout = 30000;
    // 5 seconds
    final long connectTimeout = 5000;
    // Calculate total timeout in milliseconds
    int timeoutInMs = (int) (responseTimeout + connectTimeout);
    final int status = 200;
    final byte[] body = { 1 };
    final Map<String, List<String>> headerFields = new HashMap<>();
    final byte[] errorReason = "succeed".getBytes();
    HttpResponse sendResponse = new HttpResponse(status, body, headerFields, errorReason);
    new NonStrictExpectations() {

        {
            iotHubServiceSasToken.toString();
            result = STANDARD_SASTOKEN_STRING;
            httpRequest.setReadTimeoutMillis(timeoutInMs + 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, TransportUtils.getJavaServiceClientIdentifier() + TransportUtils.getServiceVersion());
            result = httpRequest;
            httpRequest.setHeaderField(ACCEPT, ACCEPT_VALUE);
            result = httpRequest;
            httpRequest.setHeaderField(CONTENT_TYPE, ACCEPT_VALUE + "; " + ACCEPT_CHARSET);
            result = httpRequest;
            httpRequest.send();
            result = sendResponse;
            IotHubExceptionManager.httpResponseVerification(sendResponse);
        }
    };
    //act
    HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, new URL(STANDARD_URL), HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, timeoutInMs);
    //assert
    assertEquals(response, sendResponse);
    new Verifications() {

        {
            iotHubServiceSasToken.toString();
            times = 1;
            httpRequest.setReadTimeoutMillis(timeoutInMs + DEFAULT_HTTP_TIMEOUT_MS);
            times = 1;
            httpRequest.setHeaderField(anyString, anyString);
            times = 5;
            httpRequest.send();
            times = 1;
            IotHubExceptionManager.httpResponseVerification(sendResponse);
            times = 1;
        }
    };
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URL(java.net.URL) Test(org.junit.Test)

Example 72 with HttpResponse

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

the class HttpsRequestTest method sendReturnsError.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_008: [If an I/O exception occurs because of a bad response status code, the function shall attempt to flush or read the error stream so that the underlying HTTPS connection can be reused.]
@Test
public void sendReturnsError(@Mocked final HttpConnection mockConn) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] body = new byte[0];
    final byte[] error = { 5, 6, 7, 0, 1 };
    final byte[] expectedError = error;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.connect();
            result = new IOException();
            mockConn.readError();
            result = error;
        }
    };
    // Act
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    HttpResponse response = request.send();
    byte[] testError = response.getErrorReason();
    // Assert
    assertThat(testError, is(expectedError));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IOException(java.io.IOException) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 73 with HttpResponse

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

the class HttpsRequestTest method sendReturnsHeaderFields.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
@Test
public void sendReturnsHeaderFields(@Mocked final HttpConnection mockConn) throws IOException {
    // Arrange
    final Map<String, List<String>> headerFields = new HashMap<>();
    final String field = "test-field";
    final List<String> values = new LinkedList<>();
    final String value0 = "test-value0";
    final String value1 = "test-value1";
    values.add(value0);
    values.add(value1);
    headerFields.put(field, values);
    final String expectedValues = value0 + "," + value1;
    final HttpMethod httpsMethod = HttpMethod.POST;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.getResponseHeaders();
            result = headerFields;
        }
    };
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    // Act
    HttpResponse response = request.send();
    String testValues = response.getHeaderField(field);
    // Assert
    assertThat(testValues, is(expectedValues));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 74 with HttpResponse

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

the class HttpsResponseTest method getHeaderFieldMatchesCaseInsensitive.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSRESPONSE_12_005: [The function shall match the header field name in a case-insensitive manner.]
@Test
public void getHeaderFieldMatchesCaseInsensitive() {
    // Arrange
    final int status = 200;
    final byte[] body = { 1 };
    final byte[] errorReason = {};
    final Map<String, List<String>> headerFields = new HashMap<>();
    final String field = "test-field";
    final List<String> values = new LinkedList<>();
    final String value0 = "test-field-value0";
    final String value1 = "test-field-value1";
    final String expectedValues = value0 + "," + value1;
    values.add(value0);
    values.add(value1);
    headerFields.put(field, values);
    // Act
    HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
    String differentCaseField = "Test-Field";
    String testValues = response.getHeaderField(differentCaseField);
    // Assert
    assertThat(testValues, is(expectedValues));
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 75 with HttpResponse

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

the class HttpsResponseTest method getHeaderFieldRejectsInvalidFieldName.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSRESPONSE_12_006: [If a value could not be found for the given header field name, the function shall throw an IllegalArgumentException.]
// Assert
@Test(expected = IllegalArgumentException.class)
public void getHeaderFieldRejectsInvalidFieldName() throws IllegalArgumentException {
    // Arrange
    final int status = 200;
    final byte[] body = { 1 };
    final byte[] errorReason = {};
    final Map<String, List<String>> headerFields = new HashMap<>();
    final String field = "test-field";
    // Act
    HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
    response.getHeaderField(field);
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) LinkedList(java.util.LinkedList) 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