Search in sources :

Example 26 with HttpMethod

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.

the class HttpsRequestTest method setHeaderFieldSetsHeaderField.

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

        {
            mockUrl.getProtocol();
            result = "http";
        }
    };
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    // Act
    request.setHeaderField(field, value);
    // Assert
    new Verifications() {

        {
            mockConn.setRequestHeader(field, value);
        }
    };
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 27 with HttpMethod

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.

the class HttpsRequestTest method constructorThrowsIoExceptionIfCannotSetupConnection.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_004: [If an IOException occurs in setting up the HTTPS connection, the function shall throw an IOException.]
// Assert
@Test(expected = IOException.class)
public void constructorThrowsIoExceptionIfCannotSetupConnection(@Mocked final HttpConnection mockConn) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            new HttpConnection(mockUrl, httpsMethod);
            result = new IOException();
        }
    };
    // Act
    new HttpRequest(mockUrl, httpsMethod, body);
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) IOException(java.io.IOException) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 28 with HttpMethod

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.

the class HttpsRequestTest method constructorOpensConnection.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_001: [The function shall open a connection with the given URL as the endpoint.]
@Test
public void constructorOpensConnection(@Mocked final HttpConnection mockConn) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] body = new byte[0];
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
        }
    };
    // Act
    new HttpRequest(mockUrl, httpsMethod, body);
    // Assert
    new Verifications() {

        {
            new HttpConnection(mockUrl, (HttpMethod) any);
        }
    };
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 29 with HttpMethod

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.

the class HttpConnectionTest method connectThrowsIoExceptionIfCannotConnect.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_007: [The function shall throw an IOException if the connection could not be established, or the server responded with a bad status code.]
// Assert
@Test(expected = IOException.class)
public void connectThrowsIoExceptionIfCannotConnect() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.connect();
            result = new IOException();
        }
    };
    // Act
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) IOException(java.io.IOException) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Example 30 with HttpMethod

use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.

the class HttpConnectionTest method getResponseHeadersReturnsResponseHeaders.

// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_022: [The function shall return a mapping of header field names to the values associated with the header field name.]
@Test
public void getResponseHeadersReturnsResponseHeaders() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final String field0 = "test-field0";
    final String value0 = "test-value0";
    final String field1 = "test-field1";
    final String value1 = "test-value1";
    final List<String> values0 = new LinkedList<>();
    values0.add(value0);
    final List<String> values1 = new LinkedList<>();
    values1.add(value1);
    final Map<String, List<String>> responseHeaders = new HashMap<>();
    responseHeaders.put(field0, values0);
    responseHeaders.put(field1, values1);
    final Map<String, List<String>> expectedResponseHeaders = responseHeaders;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getHeaderFields();
            result = responseHeaders;
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    Map<String, List<String>> testResponseHeaders = conn.getResponseHeaders();
    // Assert
    assertThat(testResponseHeaders.size(), is(expectedResponseHeaders.size()));
    // the list of values for each field is of size 1, so the lists
    // can be directly compared.
    assertThat(testResponseHeaders.get(field0), is(expectedResponseHeaders.get(field0)));
    assertThat(testResponseHeaders.get(field1), is(expectedResponseHeaders.get(field1)));
}
Also used : HashMap(java.util.HashMap) HttpConnection(com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection) List(java.util.List) LinkedList(java.util.LinkedList) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)42 Test (org.junit.Test)42 HttpConnection (com.microsoft.azure.sdk.iot.service.transport.http.HttpConnection)28 NonStrictExpectations (mockit.NonStrictExpectations)24 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)16 IOException (java.io.IOException)12 Verifications (mockit.Verifications)9 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)7 HashMap (java.util.HashMap)6 LinkedList (java.util.LinkedList)6 List (java.util.List)6 URL (java.net.URL)5 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)1 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)1 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)1 Map (java.util.Map)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1