Search in sources :

Example 16 with HttpsRequest

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest 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 17 with HttpsRequest

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

the class HttpsRequestTest method setSSLContextSetsSSLContext.

//Tests_SRS_HTTPSREQUEST_25_016: [The function shall set the SSL context for the IotHub.]
@Test
public void setSSLContextSetsSSLContext(@Mocked final HttpsConnection mockConn, @Mocked final IotHubSSLContext mockedContext) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    request.setSSLContext(mockedContext);
    new Verifications() {

        {
            Deencapsulation.invoke(mockConn, "setSSLContext", mockedContext.getIotHubSSlContext());
        }
    };
}
Also used : 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 18 with HttpsRequest

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest 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)

Example 19 with HttpsRequest

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

the class HttpsRequestTest method constructorWritesBodyToConnection.

// Tests_SRS_HTTPSREQUEST_11_002: [The function shall write the body to the connection.]
@Test
public void constructorWritesBodyToConnection(@Mocked final HttpsConnection mockConn) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = { 1, 2, 3 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
        }
    };
    new HttpsRequest(mockUrl, httpsMethod, body);
    final byte[] expectedBody = body;
    new Verifications() {

        {
            new HttpsConnection(mockUrl, (HttpsMethod) any).writeOutput(expectedBody);
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) Test(org.junit.Test)

Aggregations

HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)19 HttpsRequest (com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest)19 Test (org.junit.Test)19 HttpsResponse (com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse)7 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)4 URL (java.net.URL)4 Map (java.util.Map)1