Search in sources :

Example 11 with HttpConnection

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

the class HttpConnectionTest method writeOutputDoesNotFailWhenBodyIsEmpty.

// Tests_SRS_HTTPSCONNECTION_25_013: [The function shall throw an IllegalArgumentException if the request does not currently use method POST or PUT and the body is non-empty.]
@Test
public void writeOutputDoesNotFailWhenBodyIsEmpty() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] body = new byte[0];
    // Assert
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    // Act
    conn.writeOutput(body);
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 12 with HttpConnection

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

the class HttpConnectionTest method readErrorCompletelyReadsErrorStream.

// Tests_SRS_HTTPSCONNECTION_25_017: [The function shall read from the error stream and return the response.]
@Test
public void readErrorCompletelyReadsErrorStream(@Mocked final InputStream mockIs) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    byte[] expectedError = { 1, 2, 3 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getErrorStream();
            result = mockIs;
            mockIs.read();
            returns(1, 2, 3, -1);
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    byte[] testError = conn.readError();
    // Assert
    assertThat(testError, is(expectedError));
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 13 with HttpConnection

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

the class HttpConnectionTest method readInputClosesInputStreamEvenIfExceptionOccurs.

// Tests_SRS_HTTPSCONNECTION_25_016: [The function shall close the input stream after it has been completely read.]
@Test
public void readInputClosesInputStreamEvenIfExceptionOccurs(@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.getInputStream();
            result = mockIs;
            mockIs.read();
            result = new IOException("This is a test exception");
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    try {
        conn.readInput();
    } catch (IOException e) {
    // expected exception, but not testing for it, so it can be ignored
    }
    // Assert
    new Verifications() {

        {
            mockIs.close();
            times = 1;
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection) IOException(java.io.IOException) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 14 with HttpConnection

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

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

the class HttpsRequestTest method constructorSetsHttpsMethodCorrectly.

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

Aggregations

HttpConnection (com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection)30 HttpMethod (com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod)30 Test (org.junit.Test)30 NonStrictExpectations (mockit.NonStrictExpectations)26 Verifications (mockit.Verifications)11 IOException (java.io.IOException)9 HttpRequest (com.microsoft.azure.sdk.iot.deps.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