Search in sources :

Example 56 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, TransportException {
    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();
    assertThat(testResponseHeaders.size(), is(responseHeaders.size()));
    // the list of values for each field is of size 1, so the lists
    // can be directly compared.
    assertThat(testResponseHeaders.get(field0), is(responseHeaders.get(field0)));
    assertThat(testResponseHeaders.get(field1), is(responseHeaders.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) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 57 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 setRequestMethodRejectsNonPostOrPutIfHasBody.

// Tests_SRS_HTTPSCONNECTION_11_007: [The function shall throw an IllegalArgumentException if the request currently has a non-empty body and the new method is not a POST or a PUT.]
@Test(expected = IllegalArgumentException.class)
public void setRequestMethodRejectsNonPostOrPutIfHasBody() throws IOException, TransportException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final HttpsMethod illegalHttpsMethod = HttpsMethod.DELETE;
    final byte[] body = { 1, 2, 3 };
    new NonStrictExpectations() {

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

Example 58 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 readErrorAlwaysClosesStream.

// Tests_SRS_HTTPSCONNECTION_11_020: [The function shall close the error stream after it has been completely read.]
@Test
public void readErrorAlwaysClosesStream(@Mocked final InputStream mockIs) throws IOException, TransportException {
    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 = new TransportException("This is a test exception");
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    // act
    try {
        byte[] testError = conn.readError();
    } catch (TransportException e) {
    // expected exception, but not testing for it, so it can be ignored
    }
    new Verifications() {

        {
            mockIs.close();
            times = 1;
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) Test(org.junit.Test)

Example 59 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 getBodyReturnsSavedBody.

// Tests_SRS_HTTPSCONNECTION_34_031: [The function shall return the saved body.]
@Test
public void getBodyReturnsSavedBody() throws IOException, TransportException {
    // arrange
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getURL();
            result = mockUrl;
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    byte[] expectedBody = "test".getBytes(StandardCharsets.UTF_8);
    conn.writeOutput(expectedBody);
    // act
    byte[] actual = Deencapsulation.invoke(conn, "getBody");
    // assert
    Assert.assertArrayEquals(expectedBody, actual);
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) Test(org.junit.Test)

Example 60 with HttpsConnection

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

the class HttpsRequestTest method constructorOpensConnection.

// Tests_SRS_HTTPSREQUEST_11_001: [The function shall open a connection with the given URL as the endpoint.]
@Test
public void constructorOpensConnection(@Mocked final HttpsConnection mockConn) throws TransportException, MalformedURLException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = new byte[0];
    final URL mockUrl = new URL("https://www.microsoft.com");
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body, "");
    request.send();
    new Verifications() {

        {
            new HttpsConnection(mockUrl, (HttpsMethod) any, null, true);
        }
    };
}
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) URL(java.net.URL) Test(org.junit.Test)

Aggregations

HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)75 Test (org.junit.Test)75 HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)73 NonStrictExpectations (mockit.NonStrictExpectations)26 HttpsRequest (com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest)13 IOException (java.io.IOException)13 Verifications (mockit.Verifications)10 URL (java.net.URL)9 TransportException (com.microsoft.azure.sdk.iot.device.exceptions.TransportException)3 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 HttpProxySocketFactory (com.microsoft.azure.sdk.iot.device.transport.HttpProxySocketFactory)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1