Search in sources :

Example 31 with HttpsConnection

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

the class HttpsRequestTest method constructorWritesBodyToConnection.

// Tests_SRS_HTTPSREQUEST_11_002: [The function shall write the body to the connection.]
@Test
public void constructorWritesBodyToConnection(@Mocked final HttpsConnection mockConn) throws TransportException, MalformedURLException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = { 1, 2, 3 };
    final URL mockUrl = new URL("https://www.microsoft.com");
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body, "");
    request.send();
    new Verifications() {

        {
            new HttpsConnection(mockUrl, (HttpsMethod) any, null, true).writeOutput(body);
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) URL(java.net.URL) Test(org.junit.Test)

Example 32 with HttpsConnection

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

the class HttpsRequestTest method setConnectTimeoutSetsConnectTimeout.

@Test
public void setConnectTimeoutSetsConnectTimeout(@Mocked final HttpsConnection mockConn) throws TransportException, MalformedURLException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final byte[] body = new byte[0];
    final int readTimeout = 1;
    final URL mockUrl = new URL("https://www.microsoft.com");
    new NonStrictExpectations() {

        {
            new HttpsConnection(mockUrl, httpsMethod, null, true);
            result = mockConn;
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body, "");
    request.setConnectTimeout(readTimeout);
    request.send();
    new Verifications() {

        {
            mockConn.setConnectTimeout(readTimeout);
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) URL(java.net.URL) Test(org.junit.Test)

Example 33 with HttpsConnection

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

the class HttpsRequestTest method setReadTimeoutSetsReadTimeout.

// Tests_SRS_HTTPSREQUEST_11_014: [The function shall set the read timeout for the request to the given value.]
@Test
public void setReadTimeoutSetsReadTimeout(@Mocked final HttpsConnection mockConn) throws TransportException, MalformedURLException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final byte[] body = new byte[0];
    final int readTimeout = 1;
    final URL mockUrl = new URL("https://www.microsoft.com");
    new NonStrictExpectations() {

        {
            new HttpsConnection(mockUrl, httpsMethod, null, true);
            result = mockConn;
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body, "");
    request.setReadTimeout(readTimeout);
    request.send();
    new Verifications() {

        {
            mockConn.setReadTimeout(readTimeout);
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) URL(java.net.URL) Test(org.junit.Test)

Example 34 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 writeOutputFailsWhenMethodIsNotPostOrPut.

// Tests_SRS_HTTPSCONNECTION_11_010: [The function shall throw an IllegalArgumentException if the request does not currently use method POST or PUT and the body is non-empty.]
@Test(expected = IllegalArgumentException.class)
public void writeOutputFailsWhenMethodIsNotPostOrPut() throws IOException, TransportException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = { 1, 2 };
    new Expectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.writeOutput(body);
}
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 35 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 readInputCompletelyReadsInputStream.

// Tests_SRS_HTTPSCONNECTION_11_011: [The function shall read from the input stream (response stream) and return the response.]
@Test
public void readInputCompletelyReadsInputStream(@Mocked final InputStream mockIs) 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 = mockIs;
            mockIs.read();
            returns(1, 2, 3, -1);
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    byte[] testResponse = conn.readInput();
    byte[] expectedResponse = { 1, 2, 3 };
    assertThat(testResponse, is(expectedResponse));
}
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