Search in sources :

Example 11 with HttpsResponse

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse in project azure-iot-sdk-java by Azure.

the class HttpsRequestTest method sendReturnsError.

// Tests_SRS_HTTPSREQUEST_11_009: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
// Tests_SRS_HTTPSREQUEST_11_012: [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 HttpsConnection mockConn) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = new byte[0];
    final byte[] error = { 5, 6, 7, 0, 1 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockConn.connect();
            result = new IOException();
            mockConn.readError();
            result = error;
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    HttpsResponse response = request.send();
    byte[] testError = response.getErrorReason();
    final byte[] expectedError = error;
    assertThat(testError, is(expectedError));
}
Also used : HttpsResponse(com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with HttpsResponse

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse in project azure-iot-sdk-java by Azure.

the class HttpsRequestTest method sendReturnsBody.

// Tests_SRS_HTTPSREQUEST_11_009: [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 HttpsConnection mockConn) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] requestBody = new byte[0];
    final byte[] responseBody = { 1, 2, 3, 0, 4 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockConn.readInput();
            result = responseBody;
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, requestBody);
    HttpsResponse response = request.send();
    byte[] testBody = response.getBody();
    final byte[] expectedBody = responseBody;
    assertThat(testBody, is(expectedBody));
}
Also used : HttpsResponse(com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) Test(org.junit.Test)

Example 13 with HttpsResponse

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse in project azure-iot-sdk-java by Azure.

the class HttpsRequestTest method sendReturnsHeaderFields.

// Tests_SRS_HTTPSREQUEST_11_009: [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 HttpsConnection mockConn) throws IOException {
    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 HttpsMethod httpsMethod = HttpsMethod.POST;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockConn.getResponseHeaders();
            result = headerFields;
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    HttpsResponse response = request.send();
    String testValues = response.getHeaderField(field);
    final String expectedValues = value0 + "," + value1;
    assertThat(testValues, is(expectedValues));
}
Also used : HashMap(java.util.HashMap) HttpsResponse(com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse) List(java.util.List) LinkedList(java.util.LinkedList) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

HttpsResponse (com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)8 LinkedList (java.util.LinkedList)8 List (java.util.List)8 HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)7 HttpsRequest (com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest)7 IOException (java.io.IOException)3 URL (java.net.URL)1