Search in sources :

Example 16 with HttpConnection

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection in project azure-iot-sdk-java by Azure.

the class HttpConnectionTest method getResponseHeadersReturnsResponseHeaders.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_022: [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 {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.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);
    final Map<String, List<String>> expectedResponseHeaders = responseHeaders;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getHeaderFields();
            result = responseHeaders;
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    Map<String, List<String>> testResponseHeaders = conn.getResponseHeaders();
    // Assert
    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) HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) List(java.util.List) LinkedList(java.util.LinkedList) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 17 with HttpConnection

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection in project azure-iot-sdk-java by Azure.

the class HttpConnectionTest method setReadTimeoutSetsRequestTimeout.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_011: [The function shall set the read timeout to the given value.]
@Test
public void setReadTimeoutSetsRequestTimeout() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.POST;
    final String field = "test-field";
    final String value = "test-value";
    final int timeout = 1;
    final int expectedTimeout = timeout;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    // Act
    conn.setReadTimeoutMillis(timeout);
    // Assert
    new Verifications() {

        {
            mockUrl.openConnection().setReadTimeout(expectedTimeout);
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 18 with HttpConnection

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection in project azure-iot-sdk-java by Azure.

the class HttpConnectionTest method getResponseHeadersFailsIfDidNotReceiveResponse.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_023: [The function shall throw an IOException if no response was received.]
// Assert
@Test(expected = IOException.class)
public void getResponseHeadersFailsIfDidNotReceiveResponse() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getHeaderFields();
            result = new IOException();
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    conn.getResponseHeaders();
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) IOException(java.io.IOException) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 19 with HttpConnection

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection in project azure-iot-sdk-java by Azure.

the class HttpConnectionTest method setRequestMethodSetsRequestMethod.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_008: [The function shall set the request method.]
@Test
public void setRequestMethodSetsRequestMethod() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    // Act
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.setRequestMethod(httpsMethod);
    // Assert
    new Verifications() {

        {
            ((HttpsURLConnection) mockUrl.openConnection()).setRequestMethod(httpsMethod.name());
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 20 with HttpConnection

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection in project azure-iot-sdk-java by Azure.

the class HttpConnectionTest method readErrorClosesErrorStream.

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

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

        {
            mockIs.close();
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Aggregations

HttpConnection (com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection)28 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)28 Test (org.junit.Test)28 NonStrictExpectations (mockit.NonStrictExpectations)24 Verifications (mockit.Verifications)9 IOException (java.io.IOException)7 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)4 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1