Search in sources :

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

// Tests_SRS_HTTPSCONNECTION_25_016: [The function shall close the input stream after it has been completely read.]
@Test
public void readInputClosesInputStreamEvenIfExceptionOccurs(@Mocked final InputStream mockIs) throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getInputStream();
            result = mockIs;
            mockIs.read();
            result = new IOException("This is a test exception");
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    try {
        conn.readInput();
    } catch (IOException e) {
    // expected exception, but not testing for it, so it can be ignored
    }
    // Assert
    new Verifications() {

        {
            mockIs.close();
            times = 1;
        }
    };
}
Also used : HttpConnection(com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection) IOException(java.io.IOException) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

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

// Tests_SRS_HTTPSREQUEST_25_009: [The function shall set the header field with the given name to the given value.]
@Test
public void setHeaderFieldSetsHeaderField(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) 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.deps.transport.http.HttpRequest) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

Example 23 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 constructorOpensConnection.

// Tests_SRS_HTTPSREQUEST_25_001: [The function shall open a connection with the given URL as the endpoint.]
@Test
public void constructorOpensConnection(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) 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.deps.transport.http.HttpRequest) HttpConnection(com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

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

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

        {
            mockUrl.getProtocol();
            result = "http";
            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();
        }
    };
    HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
    // Act
    request.send();
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) IOException(java.io.IOException) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) Test(org.junit.Test)

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

// Tests_SRS_HTTPSREQUEST_25_003: [The function shall use the given HTTPS method (i.e. GET) as the request method.]
@Test
public void constructorSetsHttpsMethodCorrectly(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) 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((URL) any, httpsMethod);
        }
    };
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest) HttpConnection(com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection) HttpMethod(com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod) 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