Search in sources :

Example 11 with HttpConnection

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

the class HttpsRequestTest method constructorSetsHttpsMethodCorrectly.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_003: [The function shall use the given HTTPS method (i.e. GET) as the request method.]
@Test
public void constructorSetsHttpsMethodCorrectly(@Mocked final HttpConnection mockConn) 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((URL) any, httpsMethod);
        }
    };
}
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 12 with HttpConnection

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection 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 13 with HttpConnection

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

the class HttpsRequestTest method constructorThrowsIoExceptionIfCannotSetupConnection.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_004: [If an IOException occurs in setting up the HTTPS connection, the function shall throw an IOException.]
// Assert
@Test(expected = IOException.class)
public void constructorThrowsIoExceptionIfCannotSetupConnection(@Mocked final HttpConnection mockConn) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

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

Example 14 with HttpConnection

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

the class HttpsRequestTest method constructorOpensConnection.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_001: [The function shall open a connection with the given URL as the endpoint.]
@Test
public void constructorOpensConnection(@Mocked final HttpConnection mockConn) 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.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 15 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 connectThrowsIoExceptionIfCannotConnect.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_007: [The function shall throw an IOException if the connection could not be established, or the server responded with a bad status code.]
// Assert
@Test(expected = IOException.class)
public void connectThrowsIoExceptionIfCannotConnect() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.connect();
            result = new IOException();
        }
    };
    // Act
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
}
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)

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