Search in sources :

Example 6 with HttpMethod

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

the class HttpsRequestTest method sendReadsStatusCode.

// Tests_SRS_HTTPSREQUEST_25_006: [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 HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    final byte[] body = new byte[0];
    final int status = 204;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "http";
            mockConn.getResponseStatus();
            result = status;
        }
    };
    // Act
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    HttpResponse response = request.send();
    int testStatus = response.getStatus();
    // Assert
    assertThat(testStatus, is(status));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) HttpResponse(com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 7 with HttpMethod

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

the class HttpConnectionTest method connectStreamsRequestBody.

// Tests_SRS_HTTPSCONNECTION_25_006: [The function shall stream the request body, if present, through the connection.]
@Test
public void connectStreamsRequestBody() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    byte[] body = { 1, 2, 3 };
    final byte[] expectedBody = { 1, 2, 3 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    // Act
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.writeOutput(body);
    body[0] = 5;
    conn.connect();
    // Assert
    new Verifications() {

        {
            mockUrl.openConnection().getOutputStream().write(expectedBody);
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 8 with HttpMethod

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

the class HttpConnectionTest method constructorRejectsNonHttpsUrl.

// Tests_SRS_HTTPSCONNECTION_25_004: [If the URI given does not use the HTTPS protocol, the constructor shall throw an IllegalArgumentException.]
// Assert
@Test(expected = IllegalArgumentException.class)
public void constructorRejectsNonHttpsUrl() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

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

Example 9 with HttpMethod

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

the class HttpConnectionTest method constructorThrowsIoExceptionIfCannotOpenConnection.

// Tests_SRS_HTTPSCONNECTION_25_002: [The constructor shall throw an IOException if the connection was unable to be opened.]
// Assert
@Test(expected = IOException.class)
public void constructorThrowsIoExceptionIfCannotOpenConnection() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

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

Example 10 with HttpMethod

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

the class HttpsRequestTest method sendWritesBodyToOutputStream.

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

        byte[] testBody;

        @Mock
        public void $init(URL url, HttpMethod 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(HttpMethod 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<>();
        }
    };
    // Assert
    // Act
    HttpRequest request = new HttpRequest(new URL("http://www.microsoft.com"), httpsMethod, expectedBody);
    request.send();
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) HashMap(java.util.HashMap) List(java.util.List) LinkedList(java.util.LinkedList) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) URL(java.net.URL) Test(org.junit.Test)

Aggregations

HttpMethod (com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod)42 Test (org.junit.Test)42 HttpConnection (com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection)30 NonStrictExpectations (mockit.NonStrictExpectations)26 HttpRequest (com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest)16 IOException (java.io.IOException)13 Verifications (mockit.Verifications)11 HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)6 HashMap (java.util.HashMap)6 LinkedList (java.util.LinkedList)6 List (java.util.List)6 URL (java.net.URL)3 Map (java.util.Map)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1