Search in sources :

Example 11 with HttpRequest

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

the class HttpsRequestTest method sendReturnsStatusCodeOnBadStatusException.

// Tests_SRS_HTTPSREQUEST_25_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
@Test
public void sendReturnsStatusCodeOnBadStatusException(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.POST;
    final byte[] body = new byte[0];
    final int badStatus = 404;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.connect();
            result = new IOException();
            mockConn.getResponseStatus();
            result = badStatus;
        }
    };
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    // Act
    HttpResponse response = request.send();
    int testStatus = response.getStatus();
    // Assert
    assertThat(testStatus, is(badStatus));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse) IOException(java.io.IOException) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 12 with HttpRequest

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

the class HttpsRequestTest method sendReturnsError.

// Tests_SRS_HTTPSREQUEST_25_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
// Tests_SRS_HTTPSREQUEST_25_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, @Mocked final URL mockUrl) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] body = new byte[0];
    final byte[] error = { 5, 6, 7, 0, 1 };
    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(error));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse) IOException(java.io.IOException) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 13 with HttpRequest

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

the class HttpsRequestTest method constructorWritesBodyToConnection.

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

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

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

Example 14 with HttpRequest

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

the class HttpsRequestTest method sendReadsStatusCode.

// Tests_SRS_HTTPSREQUEST_25_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
@Test
public void sendReadsStatusCode(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] body = new byte[0];
    final int status = 204;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.getResponseStatus();
            result = status;
        }
    };
    // Act
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    HttpResponse response = request.send();
    int testStatus = response.getStatus();
    // Assert
    assertThat(testStatus, is(status));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 15 with HttpRequest

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

the class HttpsRequestTest method sendWritesBodyToOutputStream.

// Tests_SRS_HTTPSREQUEST_25_005: [The function shall send an HTTPS request as formatted in the constructor.]
@Test
public void sendWritesBodyToOutputStream() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.POST;
    final byte[] expectedBody = { 1, 2, 3 };
    new MockUp<HttpConnection>() {

        byte[] testBody;

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

        @Mock
        public void connect() throws IOException {
            assertThat(testBody, is(expectedBody));
        }

        @Mock
        public void writeOutput(byte[] body) {
            this.testBody = body;
        }

        // every method that is used must be manually mocked.
        @Mock
        public void setRequestHeader(String field, String value) {
        }

        @Mock
        public void setRequestMethod(HttpMethod method) {
        }

        @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
    // Act
    HttpRequest request = new HttpRequest(new URL("http://www.microsoft.com"), httpsMethod, expectedBody);
    request.send();
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) HashMap(java.util.HashMap) List(java.util.List) LinkedList(java.util.LinkedList) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) URL(java.net.URL) Test(org.junit.Test)

Aggregations

HttpRequest (com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest)27 Test (org.junit.Test)21 HttpMethod (com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod)16 URL (java.net.URL)12 IOException (java.io.IOException)11 HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)10 ProvisioningSasToken (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken)6 HashMap (java.util.HashMap)6 ContractApiHttp (com.microsoft.azure.sdk.iot.provisioning.service.contract.ContractApiHttp)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 HttpConnection (com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection)4 UrlPathBuilder (com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder)3 ResponseData (com.microsoft.azure.sdk.iot.provisioning.device.internal.task.ResponseData)3 Map (java.util.Map)3 DeviceRegistrationParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.DeviceRegistrationParser)2 ProvisioningConnectionString (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString)2 ProvisioningServiceClientTransportException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientTransportException)2 TpmRegistrationResultParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser)1 ProvisioningServiceClientException (com.microsoft.azure.sdk.iot.provisioning.service.exceptions.ProvisioningServiceClientException)1