Search in sources :

Example 6 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_throwOnSend_failed.

/* Tests_SRS_DEVICE_OPERATIONS_21_015: [The request shall send the created request and get the response.] */
@Test(expected = IOException.class)
public void invoke_throwOnSend_failed(@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, 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 = new IOException();
        }
    };
    //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) IOException(java.io.IOException) URL(java.net.URL) Test(org.junit.Test)

Example 7 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 sendReturnsStatusCodeOnBadStatusException.

// 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 sendReturnsStatusCodeOnBadStatusException(@Mocked final HttpConnection mockConn) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.POST;
    final byte[] body = new byte[0];
    final int badStatus = 404;
    final int expectedStatus = badStatus;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.connect();
            result = new IOException();
            mockConn.getResponseStatus();
            result = badStatus;
        }
    };
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    // Act
    HttpResponse response = request.send();
    int testStatus = response.getStatus();
    // Assert
    assertThat(testStatus, is(expectedStatus));
}
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 8 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 sendReadsStatusCode.

// 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 sendReadsStatusCode(@Mocked final HttpConnection mockConn) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] body = new byte[0];
    final int status = 204;
    final int expectedStatus = status;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.getResponseStatus();
            result = status;
        }
    };
    // Act
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    HttpResponse response = request.send();
    int testStatus = response.getStatus();
    // Assert
    assertThat(testStatus, is(expectedStatus));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 9 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 sendReturnsBody.

// 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 sendReturnsBody(@Mocked final HttpConnection mockConn) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] requestBody = new byte[0];
    final byte[] responseBody = { 1, 2, 3, 0, 4 };
    final byte[] expectedBody = responseBody;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.readInput();
            result = responseBody;
        }
    };
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, requestBody);
    // Act
    HttpResponse response = request.send();
    byte[] testBody = response.getBody();
    // Assert
    assertThat(testBody, is(expectedBody));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 10 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 sendReturnsHeaderFieldsOnBadStatusException.

// 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 sendReturnsHeaderFieldsOnBadStatusException(@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 value = "test-value0";
    values.add(value);
    headerFields.put(field, values);
    final HttpMethod httpsMethod = HttpMethod.POST;
    final byte[] body = new byte[0];
    final String expectedValues = value;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.connect();
            result = new IOException();
            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) IOException(java.io.IOException) LinkedList(java.util.LinkedList) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Aggregations

HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)57 Test (org.junit.Test)44 HashMap (java.util.HashMap)28 List (java.util.List)28 URL (java.net.URL)26 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)16 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)10 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)9 LinkedList (java.util.LinkedList)8 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)7 IOException (java.io.IOException)7 IotHubBadFormatException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException)4 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)2 IotHubExceptionManager (com.microsoft.azure.sdk.iot.service.exceptions.IotHubExceptionManager)2 MethodParser (com.microsoft.azure.sdk.iot.deps.serializer.MethodParser)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 JsonArray (javax.json.JsonArray)1 JsonObject (javax.json.JsonObject)1 JsonReader (javax.json.JsonReader)1