Search in sources :

Example 31 with HttpsMethod

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

the class HttpsConnectionTest method setSSLContextThrowsOnNullContext.

//Tests_SRS_HTTPSCONNECTION_25_025: [The function shall throw IllegalArgumentException if the context is null value.**]**
@Test(expected = IllegalArgumentException.class)
public void setSSLContextThrowsOnNullContext(@Mocked final SSLContext mockedContext) throws IOException {
    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", SSLContext.class);
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 32 with HttpsMethod

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod 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 IOException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final byte[] body = new byte[0];
    final int readTimeout = 1;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    request.setReadTimeoutMillis(readTimeout);
    final int expectedReadTimeout = readTimeout;
    new Verifications() {

        {
            mockConn.setReadTimeoutMillis(expectedReadTimeout);
        }
    };
}
Also used : HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) Test(org.junit.Test)

Example 33 with HttpsMethod

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

the class HttpsConnectionTest method getResponseStatusReturnsResponseStatus.

// Tests_SRS_HTTPSCONNECTION_11_015: [The function shall return the response status code.]
@Test
public void getResponseStatusReturnsResponseStatus(@Mocked final InputStream mockIs) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final int status = 204;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getResponseCode();
            result = status;
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    int testStatus = conn.getResponseStatus();
    int expectedStatus = status;
    assertThat(testStatus, is(expectedStatus));
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 34 with HttpsMethod

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

the class HttpsRequestTest method sendSetsHeaderFieldsCorrectly.

// Tests_SRS_HTTPSREQUEST_11_008: [The function shall send an HTTPS request as formatted in the constructor.]
@Test
public void sendSetsHeaderFieldsCorrectly() throws IOException {
    final HttpsMethod expectedMethod = HttpsMethod.GET;
    final byte[] body = new byte[0];
    final String field0 = "test-field0";
    final String value0 = "test-value0";
    final String field1 = "test-field1";
    final String value1 = "test-value1";
    final String userAgent = "User-Agent";
    final String userAgentValue = TransportUtils.JAVA_DEVICE_CLIENT_IDENTIFIER + TransportUtils.CLIENT_VERSION;
    new MockUp<HttpsConnection>() {

        Map<String, String> testHeaderFields = new HashMap<>();

        @Mock
        public void $init(URL url, HttpsMethod method) {
        }

        @Mock
        public void connect() throws IOException {
            assertThat(testHeaderFields.size(), is(3));
            assertThat(testHeaderFields.get(field0), is(value0));
            assertThat(testHeaderFields.get(field1), is(value1));
            assertThat(testHeaderFields.get(userAgent), is(userAgentValue));
        }

        @Mock
        public void setRequestHeader(String field, String value) {
            testHeaderFields.put(field, value);
        }

        // every method that is used must be manually mocked.
        @Mock
        public void setRequestMethod(HttpsMethod method) {
        }

        @Mock
        public void writeOutput(byte[] body) {
        }

        @Mock
        public byte[] readInput() throws IOException {
            return new byte[0];
        }

        @Mock
        public byte[] readError() throws IOException {
            return new byte[0];
        }

        @Mock
        public int getResponseStatus() throws IOException {
            return 0;
        }

        @Mock
        public Map<String, List<String>> getResponseHeaders() throws IOException {
            return new HashMap<>();
        }
    };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, expectedMethod, body);
    request.setHeaderField(field0, value0);
    request.setHeaderField(field1, value1);
    request.send();
}
Also used : HashMap(java.util.HashMap) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) List(java.util.List) LinkedList(java.util.LinkedList) HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 35 with HttpsMethod

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

the class HttpsRequestTest method sendReturnsError.

// Tests_SRS_HTTPSREQUEST_11_009: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
// Tests_SRS_HTTPSREQUEST_11_012: [If an I/O exception occurs because of a bad response status code, the function shall attempt to flush or read the error stream so that the underlying HTTPS connection can be reused.]
@Test
public void sendReturnsError(@Mocked final HttpsConnection mockConn) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = new byte[0];
    final byte[] error = { 5, 6, 7, 0, 1 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockConn.connect();
            result = new IOException();
            mockConn.readError();
            result = error;
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    HttpsResponse response = request.send();
    byte[] testError = response.getErrorReason();
    final byte[] expectedError = error;
    assertThat(testError, is(expectedError));
}
Also used : HttpsResponse(com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)45 Test (org.junit.Test)45 HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)30 NonStrictExpectations (mockit.NonStrictExpectations)26 HttpsRequest (com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest)19 IOException (java.io.IOException)11 Verifications (mockit.Verifications)10 HttpsResponse (com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse)7 HashMap (java.util.HashMap)6 LinkedList (java.util.LinkedList)6 List (java.util.List)6 URL (java.net.URL)4 Map (java.util.Map)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1