Search in sources :

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

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_001: [The constructor shall open a connection to the given URL.]
@Test
public void constructorOpensConnection() throws IOException {
    // Arrange
    final HttpMethod httpMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
        }
    };
    // Act
    new HttpConnection(mockUrl, httpMethod);
    // Assert
    new Verifications() {

        {
            mockUrl.openConnection();
        }
    };
}
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 2 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 connectStreamsRequestBody.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_006: [The function shall stream the request body, if present, through the connection.]
@Test
public void connectStreamsRequestBody() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    byte[] body = { 1, 2, 3 };
    final byte[] expectedBody = { 1, 2, 3 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    // Act
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.writeOutput(body);
    body[0] = 5;
    conn.connect();
    // Assert
    new Verifications() {

        {
            mockUrl.openConnection().getOutputStream().write(expectedBody);
        }
    };
}
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 3 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 getResponseStatusFailsIfDidNotReceiveResponse.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_021: [The function shall throw an IOException if no response was received.]
// Assert
@Test(expected = IOException.class)
public void getResponseStatusFailsIfDidNotReceiveResponse(@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.getResponseCode();
            result = new IOException();
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    conn.getResponseStatus();
}
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 4 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 getResponseStatusReturnsResponseStatus.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_020: [The function shall return the response status code.]
@Test
public void getResponseStatusReturnsResponseStatus(@Mocked final InputStream mockIs) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final int status = 204;
    int expectedStatus = status;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getResponseCode();
            result = status;
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    int testStatus = conn.getResponseStatus();
    // Assert
    assertThat(testStatus, is(expectedStatus));
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 5 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 constructorSetsRequestMethod.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_003: [The constructor shall set the HTTPS method to the given method.]
@Test
public void constructorSetsRequestMethod() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

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

        {
            mockUrl.openConnection();
        }
    };
}
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