Search in sources :

Example 16 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 sendReturnsBody.

// 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 sendReturnsBody(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] requestBody = new byte[0];
    final byte[] responseBody = { 1, 2, 3, 0, 4 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.readInput();
            result = responseBody;
        }
    };
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, requestBody);
    // Act
    HttpResponse response = request.send();
    byte[] testBody = response.getBody();
    // Assert
    assertThat(testBody, is(responseBody));
}
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 17 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 setReadTimeoutSetsReadTimeout.

// Tests_SRS_HTTPSREQUEST_25_010: [The function shall set the read timeout for the request to the given value.]
@Test
public void setReadTimeoutSetsReadTimeout(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.POST;
    final byte[] body = new byte[0];
    final int readTimeout = 1;
    new NonStrictExpectations() {

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

        {
            mockConn.setReadTimeoutMillis(readTimeout);
        }
    };
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 18 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 setHeaderFieldSetsHeaderField.

// Tests_SRS_HTTPSREQUEST_25_009: [The function shall set the header field with the given name to the given value.]
@Test
public void setHeaderFieldSetsHeaderField(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.POST;
    final byte[] body = new byte[0];
    final String field = "test-field";
    final String value = "test-value";
    new NonStrictExpectations() {

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

        {
            mockConn.setRequestHeader(field, value);
        }
    };
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 19 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 constructorOpensConnection.

// Tests_SRS_HTTPSREQUEST_25_001: [The function shall open a connection with the given URL as the endpoint.]
@Test
public void constructorOpensConnection(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

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

        {
            new HttpConnection(mockUrl, (HttpMethod) any);
        }
    };
}
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 20 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 sendThrowsIoExceptionIfCannotConnect.

// Tests_SRS_HTTPSREQUEST_25_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, @Mocked final URL mockUrl) 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.deps.transport.http.HttpRequest) IOException(java.io.IOException) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) 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