Search in sources :

Example 26 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 27 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)

Example 28 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 httpResponseVerification_403.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_003: [The function shall throw IotHubTooManyDevicesException if the Http response status equal 403]
// Assert
@Test(expected = IotHubTooManyDevicesException.class)
public void httpResponseVerification_403() throws IotHubException {
    // Arrange
    final int status = 403;
    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 29 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 httpResponseVerification_412.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_005: [The function shall throw IotHubPreconditionFailedException if the Http response status equal 412]
// Assert
@Test(expected = IotHubPreconditionFailedException.class)
public void httpResponseVerification_412() throws IotHubException {
    // Arrange
    final int status = 412;
    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 30 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 httpResponseVerification_300.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_012: [The function shall return without exception if the response status equal or less than 300]
@Test
public void httpResponseVerification_300() throws IotHubException {
    // Arrange
    final int status = 300;
    final byte[] body = { 1 };
    final Map<String, List<String>> headerFields = new HashMap<>();
    final byte[] errorReason = { 123, 125 };
    HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
    // Act
    IotHubExceptionManager.httpResponseVerification(response);
    IotHubException iotHubException = new IotHubException();
    IotHubExceptionManager iotHubExceptionManager = new IotHubExceptionManager();
    // Assert
    assertNotEquals(null, iotHubException);
    assertNotEquals(null, iotHubExceptionManager);
}
Also used : IotHubExceptionManager(com.microsoft.azure.sdk.iot.service.exceptions.IotHubExceptionManager) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException) 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