Search in sources :

Example 16 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 sendHasCorrectHttpsMethod.

// Tests_SRS_HTTPSREQUEST_11_008: [The function shall send an HTTPS request as formatted in the constructor.]
@Test
public void sendHasCorrectHttpsMethod() throws IOException {
    final HttpsMethod expectedMethod = HttpsMethod.GET;
    final byte[] body = new byte[0];
    new MockUp<HttpsConnection>() {

        HttpsMethod testMethod;

        @Mock
        public void $init(URL url, HttpsMethod method) {
            this.testMethod = method;
        }

        @Mock
        public void connect() throws IOException {
            assertThat(testMethod, is(expectedMethod));
        }

        @Mock
        public void setRequestMethod(HttpsMethod method) {
            this.testMethod = method;
        }

        // every method that is used must be manually mocked.
        @Mock
        public void setRequestHeader(String field, String value) {
        }

        @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.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) URL(java.net.URL) Test(org.junit.Test)

Example 17 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 constructorSetsHttpsMethodCorrectly.

// Tests_SRS_HTTPSREQUEST_11_004: [The function shall use the given HTTPS method (i.e. GET) as the request method.]
@Test
public void constructorSetsHttpsMethodCorrectly(@Mocked final HttpsConnection mockConn) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
        }
    };
    new HttpsRequest(mockUrl, httpsMethod, body);
    new Verifications() {

        {
            new HttpsConnection((URL) any, httpsMethod);
        }
    };
}
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) Test(org.junit.Test)

Example 18 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 sendReadsStatusCode.

// 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).]
@Test
public void sendReadsStatusCode(@Mocked final HttpsConnection mockConn) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = new byte[0];
    final int status = 204;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockConn.getResponseStatus();
            result = status;
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    HttpsResponse response = request.send();
    int testStatus = response.getStatus();
    final int expectedStatus = status;
    assertThat(testStatus, is(expectedStatus));
}
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) Test(org.junit.Test)

Example 19 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 sendWritesBodyToOutputStream.

// Tests_SRS_HTTPSREQUEST_11_008: [The function shall send an HTTPS request as formatted in the constructor.]
@Test
public void sendWritesBodyToOutputStream() throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final byte[] expectedBody = { 1, 2, 3 };
    new MockUp<HttpsConnection>() {

        byte[] testBody;

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

        @Mock
        public void connect() throws IOException {
            assertThat(testBody, is(expectedBody));
        }

        @Mock
        public void writeOutput(byte[] body) {
            this.testBody = body;
        }

        // every method that is used must be manually mocked.
        @Mock
        public void setRequestHeader(String field, String value) {
        }

        @Mock
        public void setRequestMethod(HttpsMethod method) {
        }

        @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, httpsMethod, expectedBody);
    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) URL(java.net.URL) Test(org.junit.Test)

Example 20 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 sendReturnsStatusCodeOnBadStatusException.

// 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).]
@Test
public void sendReturnsStatusCodeOnBadStatusException(@Mocked final HttpsConnection mockConn) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final byte[] body = new byte[0];
    final int badStatus = 404;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockConn.connect();
            result = new IOException();
            mockConn.getResponseStatus();
            result = badStatus;
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    HttpsResponse response = request.send();
    int testStatus = response.getStatus();
    final int expectedStatus = badStatus;
    assertThat(testStatus, is(expectedStatus));
}
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