Search in sources :

Example 26 with HttpConnection

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

the class HttpConnectionTest method constructorOpensConnection.

// Tests_SRS_HTTPSCONNECTION_25_001: [The constructor shall open a connection to the given URL.]
@Test
public void constructorOpensConnection() throws IOException {
    // Arrange
    final HttpMethod httpMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
        }
    };
    // Act
    new HttpConnection(mockUrl, httpMethod);
    // Assert
    new Verifications() {

        {
            mockUrl.openConnection();
        }
    };
}
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 27 with HttpConnection

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

the class HttpConnectionTest method setRequestMethodSetsRequestMethod.

// Tests_SRS_HTTPSCONNECTION_25_008: [The function shall set the request method.]
@Test
public void setRequestMethodSetsRequestMethod() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    // Act
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.setRequestMethod(httpsMethod);
    // Assert
    new Verifications() {

        {
            ((HttpsURLConnection) mockUrl.openConnection()).setRequestMethod(httpsMethod.name());
        }
    };
}
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) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 28 with HttpConnection

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

the class HttpConnectionTest method setRequestHeaderSetsRequestHeader.

// Tests_SRS_HTTPSCONNECTION_25_010: [The function shall set the given request header field.]
@Test
public void setRequestHeaderSetsRequestHeader() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.POST;
    final String field = "test-field";
    final String value = "test-value";
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    // Act
    conn.setRequestHeader(field, value);
    // Assert
    new Verifications() {

        {
            mockUrl.openConnection().setRequestProperty(field, value);
        }
    };
}
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 29 with HttpConnection

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

the class HttpConnectionTest method connectUsesCorrectUrl.

// Tests_SRS_HTTPSCONNECTION_25_005: [The function shall send a request to the URL given in the constructor.]
@Test
public void connectUsesCorrectUrl() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.PUT;
    new NonStrictExpectations() {

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

        {
            mockUrl.openConnection().connect();
        }
    };
}
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 30 with HttpConnection

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

the class HttpConnectionTest method readErrorReturnsEmptyErrorReasonIfNoErrorReason.

// Tests_SRS_HTTPSCONNECTION_25_017: [The function shall read from the error stream and return the response.]
@Test
public void readErrorReturnsEmptyErrorReasonIfNoErrorReason() throws IOException {
    // Arrange
    final HttpMethod httpsMethod = HttpMethod.GET;
    byte[] expectedError = {};
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getErrorStream();
            result = null;
        }
    };
    HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
    conn.connect();
    // Act
    byte[] testError = conn.readError();
    // Assert
    assertThat(testError, is(expectedError));
}
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)

Aggregations

HttpConnection (com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection)30 HttpMethod (com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod)30 Test (org.junit.Test)30 NonStrictExpectations (mockit.NonStrictExpectations)26 Verifications (mockit.Verifications)11 IOException (java.io.IOException)9 HttpRequest (com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest)4 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1