Search in sources :

Example 26 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 readErrorReturnsEmptyErrorReasonIfNoErrorReason.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_017: [The function shall read from the error stream and return the response.]
@Test
public void readErrorReturnsEmptyErrorReasonIfNoErrorReason() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    byte[] expectedError = {};
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getErrorStream();
            result = null;
        }
    };
    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.service.transport.http.HttpConnection) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 27 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 readInputFailsIfCannotAccessInputStream.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_015: [The function shall throw an IOException if the input stream could not be accessed.]
// Assert
@Test(expected = IOException.class)
public void readInputFailsIfCannotAccessInputStream() 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 = new IOException();
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    conn.readInput();
}
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 28 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 writeOutputDoesNotFailWhenBodyIsEmpty.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_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.service.transport.http.HttpConnection) 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