Search in sources :

Example 36 with HttpsConnection

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method readInputFailsIfCannotAccessInputStream.

// Tests_SRS_HTTPSCONNECTION_11_012: [The function shall throw a TransportException if the input stream could not be accessed.]
@Test(expected = TransportException.class)
public void readInputFailsIfCannotAccessInputStream() throws IOException, TransportException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getInputStream();
            result = new IOException();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    conn.readInput();
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) IOException(java.io.IOException) Test(org.junit.Test)

Example 37 with HttpsConnection

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method connectThrowsIoExceptionIfCannotConnect.

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

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.connect();
            result = new IOException();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) IOException(java.io.IOException) Test(org.junit.Test)

Example 38 with HttpsConnection

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method readInputFailsIfCannotAccessInputStreamAndIsNoRouteToHost.

// Tests_SRS_HTTPSCONNECTION_34_027: [If an UnknownHostException or a NoRouteToHostException is encountered, the thrown TransportException shall be retryable.]
@Test
public void readInputFailsIfCannotAccessInputStreamAndIsNoRouteToHost() throws IOException, TransportException {
    boolean exceptionCaught = false;
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getInputStream();
            result = new NoRouteToHostException();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    try {
        conn.readInput();
    } catch (TransportException e) {
        exceptionCaught = true;
        if (!e.isRetryable()) {
            fail("NoRouteToHostException should yield retriable TransportException");
        }
    }
    assertTrue(exceptionCaught);
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) Test(org.junit.Test)

Example 39 with HttpsConnection

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method setSSLContextSetsContext.

// Tests_SRS_HTTPSCONNECTION_25_024: [**The function shall set the the SSL context with the given value.**]**
@Test
public void setSSLContextSetsContext(@Mocked final SSLContext mockedContext) throws IOException, TransportException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final String field = "test-field";
    final String value = "test-value";
    final int timeout = 1;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    final HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    Deencapsulation.invoke(conn, "setSSLContext", mockedContext);
    new Verifications() {

        {
            mockUrlConn.setSSLSocketFactory(mockedContext.getSocketFactory());
            times = 1;
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) Test(org.junit.Test)

Example 40 with HttpsConnection

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method readErrorReturnsEmptyErrorReasonIfNoErrorReason.

// Tests_SRS_HTTPSCONNECTION_11_013: [The function shall read from the error stream and return the response.]
@Test
public void readErrorReturnsEmptyErrorReasonIfNoErrorReason() throws IOException, TransportException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getErrorStream();
            result = null;
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    byte[] testError = conn.readError();
    byte[] expectedError = {};
    assertThat(testError, is(expectedError));
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) Test(org.junit.Test)

Aggregations

HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)75 Test (org.junit.Test)75 HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)73 NonStrictExpectations (mockit.NonStrictExpectations)26 HttpsRequest (com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest)13 IOException (java.io.IOException)13 Verifications (mockit.Verifications)10 URL (java.net.URL)9 TransportException (com.microsoft.azure.sdk.iot.device.exceptions.TransportException)3 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 HttpProxySocketFactory (com.microsoft.azure.sdk.iot.device.transport.HttpProxySocketFactory)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1