Search in sources :

Example 6 with HttpsConnection

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

the class HttpsConnectionTest method readErrorClosesErrorStream.

// Tests_SRS_HTTPSCONNECTION_11_020: [The function shall close the error stream after it has been completely read.]
@Test
public void readErrorClosesErrorStream(@Mocked final InputStream mockIs) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getErrorStream();
            result = mockIs;
            mockIs.read();
            result = -1;
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    conn.readError();
    new Verifications() {

        {
            mockIs.close();
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 7 with HttpsConnection

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

the class HttpsConnectionTest method connectStreamsRequestBody.

// Tests_SRS_HTTPSCONNECTION_11_004: [The function shall stream the request body, if present, through the connection.]
// Tests_SRS_HTTPSCONNECTION_11_009: [The function shall save the body to be sent with the request.]
@Test
public void connectStreamsRequestBody() throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.PUT;
    byte[] body = { 1, 2, 3 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.writeOutput(body);
    body[0] = 5;
    conn.connect();
    final byte[] expectedBody = { 1, 2, 3 };
    new Verifications() {

        {
            mockUrl.openConnection().getOutputStream().write(expectedBody);
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 8 with HttpsConnection

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

the class HttpsConnectionTest method writeOutputDoesNotFailWhenBodyIsEmpty.

// Tests_SRS_HTTPSCONNECTION_11_010: [The function shall throw an IllegalArgumentException if the request does not currently use method POST or PUT and the body is non-empty.]
@Test
public void writeOutputDoesNotFailWhenBodyIsEmpty() throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.writeOutput(body);
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 9 with HttpsConnection

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

the class HttpsConnectionTest method getResponseStatusFailsIfDidNotReceiveResponse.

// Tests_SRS_HTTPSCONNECTION_11_016: [The function shall throw an IOException if no response was received.]
@Test(expected = IOException.class)
public void getResponseStatusFailsIfDidNotReceiveResponse(@Mocked final InputStream mockIs) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getResponseCode();
            result = new IOException();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    conn.getResponseStatus();
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) IOException(java.io.IOException) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 10 with HttpsConnection

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

the class HttpsConnectionTest method getResponseHeadersReturnsResponseHeaders.

// Tests_SRS_HTTPSCONNECTION_11_017: [The function shall return a mapping of header field names to the values associated with the header field name.]
@Test
public void getResponseHeadersReturnsResponseHeaders() throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final String field0 = "test-field0";
    final String value0 = "test-value0";
    final String field1 = "test-field1";
    final String value1 = "test-value1";
    final List<String> values0 = new LinkedList<>();
    values0.add(value0);
    final List<String> values1 = new LinkedList<>();
    values1.add(value1);
    final Map<String, List<String>> responseHeaders = new HashMap<>();
    responseHeaders.put(field0, values0);
    responseHeaders.put(field1, values1);
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getHeaderFields();
            result = responseHeaders;
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    Map<String, List<String>> testResponseHeaders = conn.getResponseHeaders();
    final Map<String, List<String>> expectedResponseHeaders = responseHeaders;
    assertThat(testResponseHeaders.size(), is(expectedResponseHeaders.size()));
    // the list of values for each field is of size 1, so the lists
    // can be directly compared.
    assertThat(testResponseHeaders.get(field0), is(expectedResponseHeaders.get(field0)));
    assertThat(testResponseHeaders.get(field1), is(expectedResponseHeaders.get(field1)));
}
Also used : HashMap(java.util.HashMap) HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) List(java.util.List) LinkedList(java.util.LinkedList) NonStrictExpectations(mockit.NonStrictExpectations) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)30 HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)30 Test (org.junit.Test)30 NonStrictExpectations (mockit.NonStrictExpectations)26 Verifications (mockit.Verifications)10 IOException (java.io.IOException)7 HttpsRequest (com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest)4 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1