Search in sources :

Example 36 with HttpsRequest

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

the class HttpsRequestTest method constructorSavesFields.

// Tests_SRS_HTTPSREQUEST_34_031: [The function shall save the provided arguments to be used when the http connection is built during the call to send().]
@Test
public void constructorSavesFields() throws MalformedURLException, TransportException {
    // arrange
    URL url = new URL("http://www.microsoft.com");
    HttpsMethod method = HttpsMethod.POST;
    byte[] body = new byte[2];
    String userAgentString = "user agent";
    // act
    HttpsRequest request = new HttpsRequest(url, method, body, userAgentString);
    // assert
    assertEquals(url, Deencapsulation.getField(request, "url"));
    assertEquals(method, Deencapsulation.getField(request, "method"));
    assertArrayEquals(body, (byte[]) Deencapsulation.getField(request, "body"));
    assertEquals(userAgentString, ((Map<String, List<String>>) Deencapsulation.getField(request, "headers")).get("User-Agent").get(0));
}
Also used : 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 37 with HttpsRequest

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest 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 TransportException, MalformedURLException {
    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.USER_AGENT_STRING;
    new MockUp<HttpsConnection>() {

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

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

        @Mock
        public void connect() {
            assertThat(testHeaderFields.size(), is(4));
            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() {
            return new byte[0];
        }

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

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

        @Mock
        public Map<String, List<String>> getResponseHeaders() throws IOException {
            return new HashMap<>();
        }
    };
    URL mockUrl = new URL("Https://www.microsoft.com");
    HttpsRequest request = new HttpsRequest(mockUrl, expectedMethod, body, userAgentValue);
    request.setHeaderField(field0, value0);
    request.setHeaderField(field1, value1);
    request.send();
}
Also used : ProxySettings(com.microsoft.azure.sdk.iot.device.ProxySettings) 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 38 with HttpsRequest

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest 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 TransportException, MalformedURLException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = new byte[0];
    final URL mockUrl = new URL("https://www.microsoft.com");
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body, "");
    request.send();
    new Verifications() {

        {
            new HttpsConnection((URL) any, httpsMethod, null, true);
        }
    };
}
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 39 with HttpsRequest

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

the class HttpsRequestResponseSerializerTest method serializeTestWithoutQuery.

// Tests_SRS_HTTPREQUESTRESPONSESERIALIZER_34_003: [This function shall serialize the provided httpsRequest into the form:
// POST /modules/<moduleName>/sign?api-version=2018-06-28 HTTP/1.1
// Host: localhost:8081
// Connection: close
// <header>: <value>
// <header>: <value1>; <value2>
// .]
@Test
public void serializeTestWithoutQuery() throws MalformedURLException, TransportException, UnsupportedEncodingException, URISyntaxException {
    // arrange
    new NonStrictExpectations() {

        {
            mockedHttpsRequest.getRequestUrl();
            result = mockedURL;
            mockedHttpsRequest.getRequestHeaders();
            result = "Connection: close\r\n" + "Content-Type: application/json\r\n";
            mockedHttpsRequest.getBody();
            result = "11111".getBytes(StandardCharsets.UTF_8);
        }
    };
    uriExpectations();
    String expected = "POST /modules/testModule/sign HTTP/1.1\r\nHost: localhost:8081\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: 5\r\n\r\n";
    byte[] body = "11111".getBytes(StandardCharsets.UTF_8);
    HttpsRequest request = new HttpsRequest(new URL("https://localhost:8081/modules/testModule/sign"), HttpsMethod.GET, body, null);
    request.setHeaderField("content-type", "application/json");
    request.setHeaderField("content-length", "5");
    // act
    byte[] httpsRequestData = HttpsRequestResponseSerializer.serializeRequest(request, "/modules/testModule/sign", "", "localhost:8081");
    // assert
    String httpsRequestString = new String(httpsRequestData);
    assertEquals(expected, httpsRequestString);
}
Also used : HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) URL(java.net.URL) Test(org.junit.Test)

Example 40 with HttpsRequest

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

the class HttpsRequestResponseSerializerTest method serializeTestWithQuery.

// Tests_SRS_HTTPREQUESTRESPONSESERIALIZER_34_003: [This function shall serialize the provided httpsRequest into the form:
// POST /modules/<moduleName>/sign?api-version=2018-06-28 HTTP/1.1
// Host: localhost:8081
// Connection: close
// <header>: <value>
// <header>: <value1>; <value2>
// .]
@Test
public void serializeTestWithQuery() throws MalformedURLException, TransportException, UnsupportedEncodingException, URISyntaxException {
    // arrange
    new NonStrictExpectations() {

        {
            mockedHttpsRequest.getRequestUrl();
            result = mockedURL;
            mockedHttpsRequest.getRequestHeaders();
            result = "Connection: close\r\n" + "Content-Type: application/json\r\n";
            mockedHttpsRequest.getBody();
            result = "11111".getBytes(StandardCharsets.UTF_8);
        }
    };
    uriExpectations();
    String expected = "POST /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1\r\nHost: localhost:8081\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: 5\r\n\r\n";
    byte[] body = "11111".getBytes(StandardCharsets.UTF_8);
    HttpsRequest request = new HttpsRequest(new URL("https://localhost:8081/modules/testModule/sign?api-version=2018-06-28"), HttpsMethod.GET, body, null);
    request.setHeaderField("content-type", "application/json");
    request.setHeaderField("content-length", "5");
    // act
    byte[] httpsRequestData = HttpsRequestResponseSerializer.serializeRequest(request, "/modules/testModule/sign", "api-version=2018-06-28", "localhost:8081");
    // assert
    String httpsRequestString = new String(httpsRequestData);
    assertEquals(expected, httpsRequestString);
}
Also used : HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) URL(java.net.URL) Test(org.junit.Test)

Aggregations

HttpsRequest (com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest)40 Test (org.junit.Test)39 HttpsMethod (com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod)35 URL (java.net.URL)23 HttpsConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection)13 HttpsResponse (com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse)11 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 ProxySettings (com.microsoft.azure.sdk.iot.device.ProxySettings)2 Map (java.util.Map)1