Search in sources :

Example 26 with HttpsMethod

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod in project azure-iot-sdk-java by Azure.

the class HttpsRequestIT method sendHttpsRequestGetsCorrectResponse.

@Test
public void sendHttpsRequestGetsCorrectResponse() throws IOException {
    URL url = new URL("https://fonts.googleapis.com/css?family=Inconsolata");
    HttpsMethod method = HttpsMethod.GET;
    byte[] body = new byte[0];
    String encoding = "UTF-8";
    HttpsRequest request = new HttpsRequest(url, method, body);
    request.setHeaderField("accept-charset", encoding);
    HttpsResponse response = request.send();
    int testStatus = response.getStatus();
    String testBody = new String(response.getBody(), encoding);
    int expectedStatus = 200;
    String expectedBodyPrefix = "@font-face {\n" + "  font-family: 'Inconsolata';";
    assertThat(testStatus, is(expectedStatus));
    assertThat(testBody, is(startsWith(expectedBodyPrefix)));
}
Also used : HttpsResponse(com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) HttpsRequest(com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest) URL(java.net.URL) Test(org.junit.Test)

Example 27 with HttpsMethod

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method readInputCompletelyReadsInputStream.

// Tests_SRS_HTTPSCONNECTION_11_011: [The function shall read from the input stream (response stream) and return the response.]
@Test
public void readInputCompletelyReadsInputStream(@Mocked final InputStream mockIs) throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
            mockUrlConn.getInputStream();
            result = mockIs;
            mockIs.read();
            returns(1, 2, 3, -1);
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.connect();
    byte[] testResponse = conn.readInput();
    byte[] expectedResponse = { 1, 2, 3 };
    assertThat(testResponse, is(expectedResponse));
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 28 with HttpsMethod

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method setRequestMethodRejectsNonPostOrPutIfHasBody.

// Tests_SRS_HTTPSCONNECTION_11_007: [The function shall throw an IllegalArgumentException if the request currently has a non-empty body and the new method is not a POST or a PUT.]
@Test(expected = IllegalArgumentException.class)
public void setRequestMethodRejectsNonPostOrPutIfHasBody() throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.POST;
    final HttpsMethod illegalHttpsMethod = HttpsMethod.DELETE;
    final byte[] body = { 1, 2, 3 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            returns(httpsMethod.name(), illegalHttpsMethod.name());
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.writeOutput(body);
    conn.setRequestMethod(illegalHttpsMethod);
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 29 with HttpsMethod

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method setRequestMethodSetsRequestMethod.

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

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

        {
            ((HttpsURLConnection) mockUrl.openConnection()).setRequestMethod(httpsMethod.name());
        }
    };
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 30 with HttpsMethod

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod in project azure-iot-sdk-java by Azure.

the class HttpsConnectionTest method writeOutputFailsWhenMethodIsNotPostOrPut.

// Tests_SRS_HTTPSCONNECTION_11_010: [The function shall throw an IllegalArgumentException if the request does not currently use method POST or PUT and the body is non-empty.]
@Test(expected = IllegalArgumentException.class)
public void writeOutputFailsWhenMethodIsNotPostOrPut() throws IOException {
    final HttpsMethod httpsMethod = HttpsMethod.GET;
    final byte[] body = { 1, 2 };
    new NonStrictExpectations() {

        {
            mockUrl.getProtocol();
            result = "https";
            mockUrl.openConnection();
            result = mockUrlConn;
            mockUrlConn.getRequestMethod();
            result = httpsMethod.name();
        }
    };
    HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
    conn.writeOutput(body);
}
Also used : HttpsConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection) HttpsMethod(com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod) NonStrictExpectations(mockit.NonStrictExpectations) 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