Search in sources :

Example 36 with HttpsMethod

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

the class HttpsRequestTest method constructorThrowsIoExceptionIfCannotSetupConnection.

// Tests_SRS_HTTPSREQUEST_11_005: [If an IOException occurs in setting up the HTTPS connection, the function shall throw an IOException.]
@Test(expected = IOException.class)
public void constructorThrowsIoExceptionIfCannotSetupConnection(@Mocked final HttpsConnection mockConn) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            new HttpsConnection(mockUrl, httpsMethod);
            result = new IOException();
        }
    };
    new HttpsRequest(mockUrl, httpsMethod, body);
}
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) IOException(java.io.IOException) Test(org.junit.Test)

Example 37 with HttpsMethod

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

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

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

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod 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)45 Test (org.junit.Test)45 HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)30 NonStrictExpectations (mockit.NonStrictExpectations)26 HttpsRequest (com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest)19 IOException (java.io.IOException)11 Verifications (mockit.Verifications)10 HttpsResponse (com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse)7 HashMap (java.util.HashMap)6 LinkedList (java.util.LinkedList)6 List (java.util.List)6 URL (java.net.URL)4 Map (java.util.Map)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1