Search in sources :

Example 21 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 sendThrowsIoExceptionIfCannotConnect.

// Tests_SRS_HTTPSREQUEST_11_011: [If the client cannot connect to the server, the function shall throw an IOException.]
@Test(expected = IOException.class)
public void sendThrowsIoExceptionIfCannotConnect(@Mocked final HttpsConnection mockConn) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockConn.connect();
            result = new IOException();
            mockConn.getResponseHeaders();
            result = new IOException();
            mockConn.getResponseStatus();
            result = new IOException();
            mockConn.readInput();
            result = new IOException();
            mockConn.readError();
            result = new IOException();
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    request.send();
}
Also used : 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)

Example 22 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 setHeaderFieldSetsHeaderField.

// Tests_SRS_HTTPSREQUEST_11_013: [The function shall set the header field with the given name to the given value.]
@Test
public void setHeaderFieldSetsHeaderField(@Mocked final HttpsConnection mockConn) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final byte[] body = new byte[0];
    final String field = "test-field";
    final String value = "test-value";
    new NonStrictExpectations() {

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

        {
            mockConn.setRequestHeader(field, value);
        }
    };
}
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 23 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 constructorOpensConnection.

// Tests_SRS_HTTPSREQUEST_11_001: [The function shall open a connection with the given URL as the endpoint.]
@Test
public void constructorOpensConnection(@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(mockUrl, (HttpsMethod) any);
        }
    };
}
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 24 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 sendReturnsHeaderFieldsOnBadStatusException.

// 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 sendReturnsHeaderFieldsOnBadStatusException(@Mocked final HttpsConnection mockConn) throws IOException {
    final Map<String, List<String>> headerFields = new HashMap<>();
    final String field = "test-field";
    final List<String> values = new LinkedList<>();
    final String value = "test-value0";
    values.add(value);
    headerFields.put(field, values);
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockConn.connect();
            result = new IOException();
            mockConn.getResponseHeaders();
            result = headerFields;
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    HttpsResponse response = request.send();
    String testValues = response.getHeaderField(field);
    final String expectedValues = value;
    assertThat(testValues, is(expectedValues));
}
Also used : HashMap(java.util.HashMap) HttpsResponse(com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse) List(java.util.List) LinkedList(java.util.LinkedList) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 25 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 setSSLContextThrowsOnNull.

//Tests_SRS_HTTPSREQUEST_25_015: [**The function shall throw IllegalArgumentException if parameter is null .**]**
@Test(expected = IllegalArgumentException.class)
public void setSSLContextThrowsOnNull(@Mocked final HttpsConnection mockConn, @Mocked final IotHubSSLContext mockedContext) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
        }
    };
    HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
    request.setSSLContext(null);
}
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)

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