Search in sources :

Example 1 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 sendReadsStatusCode.

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

        {
            mockUrl.getProtocol();
            result = "https";
            mockConn.getResponseStatus();
            result = status;
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    HttpsResponse response = request.send();
    int testStatus = response.getStatus();
    final int expectedStatus = status;
    assertThat(testStatus, is(expectedStatus));
}
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 2 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 sendReturnsStatusCodeOnBadStatusException.

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

        {
            mockUrl.getProtocol();
            result = "https";
            mockConn.connect();
            result = new IOException();
            mockConn.getResponseStatus();
            result = badStatus;
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    HttpsResponse response = request.send();
    int testStatus = response.getStatus();
    final int expectedStatus = badStatus;
    assertThat(testStatus, is(expectedStatus));
}
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 3 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 sendReturnsHeaderFieldsOnBadStatusException.

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

        {
            mockUrl.getProtocol();
            result = "https";
            mockConn.connect();
            result = new IOException();
            mockConn.getResponseHeaders();
            result = headerFields;
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    HttpsResponse response = request.send();
    String testValues = response.getHeaderField(field);
    final String expectedValues = value;
    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) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 4 with HttpsResponse

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

the class HttpsRequestIT method sendHttpsRequestGetsCorrectResponse.

@Test
public void sendHttpsRequestGetsCorrectResponse() throws IOException {
    URL url = new URL("https://fonts.googleapis.com/css?family=Inconsolata");
    HttpsMethod method = HttpsMethod.GET;
    byte[] body = new byte[0];
    String encoding = "UTF-8";
    HttpsRequest request = new HttpsRequest(url, method, body);
    request.setHeaderField("accept-charset", encoding);
    HttpsResponse response = request.send();
    int testStatus = response.getStatus();
    String testBody = new String(response.getBody(), encoding);
    int expectedStatus = 200;
    String expectedBodyPrefix = "@font-face {\n" + "  font-family: 'Inconsolata';";
    assertThat(testStatus, is(expectedStatus));
    assertThat(testBody, is(startsWith(expectedBodyPrefix)));
}
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) URL(java.net.URL) Test(org.junit.Test)

Example 5 with HttpsResponse

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

the class HttpsResponseTest method getHeaderFieldReturnsHeaderField.

// Tests_SRS_HTTPSRESPONSE_11_001: [The constructor shall store the input arguments so that the getters can return them later.]
// Tests_SRS_HTTPSRESPONSE_11_004: [The function shall return a comma-separated list of the values associated with the header field name.]
@Test
public void getHeaderFieldReturnsHeaderField() {
    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";
    values.add(value0);
    values.add(value1);
    headerFields.put(field, values);
    HttpsResponse response = new HttpsResponse(status, body, headerFields, errorReason);
    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) 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