Search in sources :

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

// Tests_SRS_HTTPSCONNECTION_25_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.deps.transport.http.HttpConnection) IOException(java.io.IOException) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 7 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 setRequestMethodRejectsNonPostOrPutIfHasBody.

// Tests_SRS_HTTPSCONNECTION_25_009: [The function shall throw an IllegalArgumentException if the request currently has a non-empty body and the new method is not a POST or a PUT.]
// Assert
@Test(expected = IllegalArgumentException.class)
public void setRequestMethodRejectsNonPostOrPutIfHasBody() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.POST;
    final HttpMethod illegalHttpsMethod = HttpMethod.DELETE;
    final byte[] body = { 1, 2, 3 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            returns(httpsMethod.name(), illegalHttpsMethod.name());
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.writeOutput(body);
    // Act
    conn.setRequestMethod(illegalHttpsMethod);
}
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 8 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 getResponseHeadersFailsIfDidNotReceiveResponse.

// Tests_SRS_HTTPSCONNECTION_25_023: [The function shall throw an IOException if no response was received.]
// Assert
@Test(expected = IOException.class)
public void getResponseHeadersFailsIfDidNotReceiveResponse() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    new NonStrictExpectations() {

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

Example 9 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 getResponseStatusFailsIfDidNotReceiveResponse.

// Tests_SRS_HTTPSCONNECTION_25_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.deps.transport.http.HttpConnection) IOException(java.io.IOException) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 10 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 readErrorClosesErrorStream.

// Tests_SRS_HTTPSCONNECTION_25_019: [The function shall close the error stream after it has been completely read.]
@Test
public void readErrorClosesErrorStream(@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.getErrorStream();
            result = mockIs;
            mockIs.read();
            result = -1;
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    conn.readError();
    // Assert
    new Verifications() {

        {
            mockIs.close();
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) 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