Search in sources :

Example 21 with HttpMethod

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

the class HttpsRequestTest method sendThrowsIoExceptionIfCannotConnect.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_007: [If the client cannot connect to the server, the function shall throw an IOException.]
// Assert
@Test(expected = IOException.class)
public void sendThrowsIoExceptionIfCannotConnect(@Mocked final HttpConnection mockConn) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.POST;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.connect();
            result = new IOException();
            mockConn.getResponseHeaders();
            result = new IOException();
            mockConn.getResponseStatus();
            result = new IOException();
            mockConn.readInput();
            result = new IOException();
            mockConn.readError();
            result = new IOException();
        }
    };
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    // Act
    request.send();
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) IOException(java.io.IOException) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 22 with HttpMethod

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

the class HttpsRequestTest method sendSetsHeaderFieldsCorrectly.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_005: [The function shall send an HTTPS request as formatted in the constructor.]
@Test
public void sendSetsHeaderFieldsCorrectly() throws IOException {
    // Arrange
    final HttpMethod expectedMethod = HttpMethod.GET;
    final byte[] body = new byte[0];
    final String field0 = "test-field0";
    final String value0 = "test-value0";
    final String field1 = "test-field1";
    final String value1 = "test-value1";
    final String userAgent = "User-Agent";
    final String userAgentValue = TransportUtils.javaServiceClientIdentifier + TransportUtils.serviceVersion;
    new MockUp<HttpConnection>() {

        Map<String, String> testHeaderFields = new HashMap<>();

        @Mock
        public void $init(URL url, HttpMethod method) {
        }

        @Mock
        public void connect() throws IOException {
            assertThat(testHeaderFields.size(), is(3));
            assertThat(testHeaderFields.get(field0), is(value0));
            assertThat(testHeaderFields.get(field1), is(value1));
            assertThat(testHeaderFields.get(userAgent), is(userAgentValue));
        }

        @Mock
        public void setRequestHeader(String field, String value) {
            testHeaderFields.put(field, value);
        }

        // every method that is used must be manually mocked.
        @Mock
        public void setRequestMethod(HttpMethod method) {
        }

        @Mock
        public void writeOutput(byte[] body) {
        }

        @Mock
        public byte[] readInput() throws IOException {
            return new byte[0];
        }

        @Mock
        public byte[] readError() throws IOException {
            return new byte[0];
        }

        @Mock
        public int getResponseStatus() throws IOException {
            return 0;
        }

        @Mock
        public Map<String, List<String>> getResponseHeaders() throws IOException {
            return new HashMap<>();
        }
    };
    // Assert
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
        }
    };
    // Act
    HttpRequest request = new HttpRequest(mockUrl, expectedMethod, body);
    request.setHeaderField(field0, value0);
    request.setHeaderField(field1, value1);
    request.send();
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HashMap(java.util.HashMap) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) URL(java.net.URL) Test(org.junit.Test)

Example 23 with HttpMethod

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

the class HttpsRequestTest method constructorWritesBodyToConnection.

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

        {
            mockUrl.getProtocol();
            result = "http";
        }
    };
    // Act
    new HttpRequest(mockUrl, httpsMethod, body);
    // Assert
    new Verifications() {

        {
            new HttpConnection(mockUrl, (HttpMethod) any).writeOutput(expectedBody);
        }
    };
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 24 with HttpMethod

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

the class HttpsRequestTest method sendReturnsError.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_008: [If an I/O exception occurs because of a bad response status code, the function shall attempt to flush or read the error stream so that the underlying HTTPS connection can be reused.]
@Test
public void sendReturnsError(@Mocked final HttpConnection mockConn) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] body = new byte[0];
    final byte[] error = { 5, 6, 7, 0, 1 };
    final byte[] expectedError = error;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.connect();
            result = new IOException();
            mockConn.readError();
            result = error;
        }
    };
    // Act
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    HttpResponse response = request.send();
    byte[] testError = response.getErrorReason();
    // Assert
    assertThat(testError, is(expectedError));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IOException(java.io.IOException) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 25 with HttpMethod

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

the class HttpsRequestTest method sendReturnsHeaderFields.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_006: [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 HttpConnection mockConn) throws IOException {
    // Arrange
    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 String expectedValues = value0 + "," + value1;
    final HttpMethod httpsMethod = HttpMethod.POST;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.getResponseHeaders();
            result = headerFields;
        }
    };
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    // Act
    HttpResponse response = request.send();
    String testValues = response.getHeaderField(field);
    // Assert
    assertThat(testValues, is(expectedValues));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Aggregations

HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)42 Test (org.junit.Test)42 HttpConnection (com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection)28 NonStrictExpectations (mockit.NonStrictExpectations)24 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)16 IOException (java.io.IOException)12 Verifications (mockit.Verifications)9 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)7 HashMap (java.util.HashMap)6 LinkedList (java.util.LinkedList)6 List (java.util.List)6 URL (java.net.URL)5 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)1 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)1 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)1 Map (java.util.Map)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1