Search in sources :

Example 86 with HttpResponse

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

the class Tools method bulkRegistryOperation.

private static void bulkRegistryOperation(String jsonPayload, URL url, String connectionString) throws IOException, IotHubException {
    if (jsonPayload == null) {
        throw new IllegalArgumentException("devices cannot be null");
    }
    IotHubConnectionString iotHubConnectionString = IotHubConnectionString.createConnectionString(connectionString);
    String sasTokenString = new IotHubServiceSasToken(iotHubConnectionString).toString();
    HttpRequest request = new HttpRequest(url, HttpMethod.POST, jsonPayload.getBytes(StandardCharsets.UTF_8));
    request.setReadTimeoutMillis(IntegrationTest.HTTP_READ_TIMEOUT);
    request.setHeaderField("authorization", sasTokenString);
    request.setHeaderField("Accept", "application/json");
    request.setHeaderField("Content-Type", "application/json");
    request.setHeaderField("charset", "utf-8");
    HttpResponse response = request.send();
    IotHubExceptionManager.httpResponseVerification(response);
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString)

Example 87 with HttpResponse

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

the class DeviceOperationsTest method invokeThrowOnAcceptFailed.

/* Tests_SRS_DEVICE_OPERATIONS_21_013: [The request shall add to the HTTP header a `Accept` key with `application/json`.] */
@Test(expected = IllegalArgumentException.class)
public void invokeThrowOnAcceptFailed(@Mocked IotHubServiceSasToken iotHubServiceSasToken, @Mocked HttpRequest httpRequest) throws Exception {
    // arrange
    new NonStrictExpectations() {

        {
            iotHubServiceSasToken.toString();
            result = STANDARD_SASTOKEN_STRING;
            httpRequest.setReadTimeoutMillis(DEFAULT_HTTP_TIMEOUT_MS);
            result = httpRequest;
            httpRequest.setHeaderField(AUTHORIZATION, STANDARD_SASTOKEN_STRING);
            result = httpRequest;
            httpRequest.setHeaderField(REQUEST_ID, STANDARD_REQUEST_ID);
            result = httpRequest;
            httpRequest.setHeaderField(USER_AGENT, TransportUtils.USER_AGENT_STRING);
            result = httpRequest;
            httpRequest.setHeaderField(ACCEPT, ACCEPT_VALUE);
            result = new IllegalArgumentException();
        }
    };
    // act
    HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, new URL(STANDARD_URL), HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, 0);
}
Also used : HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL) Test(org.junit.Test)

Example 88 with HttpResponse

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

the class DeviceOperationsTest method invokeThrowOnCreateIotHubServiceSasTokenFailed.

/* Tests_SRS_DEVICE_OPERATIONS_21_006: [The request shall create a new SASToken with the ServiceConnect rights.] */
@Test(expected = IllegalArgumentException.class)
public void invokeThrowOnCreateIotHubServiceSasTokenFailed() throws Exception {
    // arrange
    new MockUp<IotHubServiceSasToken>() {

        @Mock
        void $init(IotHubConnectionString iotHubConnectionString) {
            throw new IllegalArgumentException();
        }
    };
    // act
    HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, new URL(STANDARD_URL), HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, 0);
}
Also used : HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URL(java.net.URL) Test(org.junit.Test)

Example 89 with HttpResponse

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

the class DeviceOperationsTest method invokeThrowOnsetReadTimeoutMillisFailed.

/* Tests_SRS_DEVICE_OPERATIONS_21_009: [The request shall add to the HTTP header the sum of timeout and default timeout in milliseconds.] */
@Test(expected = IllegalArgumentException.class)
public void invokeThrowOnsetReadTimeoutMillisFailed(@Mocked IotHubServiceSasToken iotHubServiceSasToken, @Mocked HttpRequest httpRequest) throws Exception {
    // arrange
    new NonStrictExpectations() {

        {
            iotHubServiceSasToken.toString();
            result = STANDARD_SASTOKEN_STRING;
            httpRequest.setReadTimeoutMillis(DEFAULT_HTTP_TIMEOUT_MS);
            result = new IllegalArgumentException();
        }
    };
    // act
    HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, new URL(STANDARD_URL), HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, 0);
}
Also used : HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL) Test(org.junit.Test)

Example 90 with HttpResponse

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

the class DeviceOperationsTest method invokeThrowOnHttpRequestFailed.

/* Tests_SRS_DEVICE_OPERATIONS_21_008: [The request shall create a new HttpRequest with the provided `url`, http `method`, and `payload`.] */
@Test(expected = IOException.class)
public void invokeThrowOnHttpRequestFailed(@Mocked IotHubServiceSasToken iotHubServiceSasToken) throws Exception {
    // arrange
    new NonStrictExpectations() {

        {
            iotHubServiceSasToken.toString();
            result = STANDARD_SASTOKEN_STRING;
        }
    };
    new MockUp<HttpRequest>() {

        @Mock
        void $init(URL url, HttpMethod method, byte[] body) throws IOException {
            throw new IOException();
        }
    };
    // act
    HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, new URL(STANDARD_URL), HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, DEFAULT_HTTP_TIMEOUT_MS);
}
Also used : HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IOException(java.io.IOException) URL(java.net.URL) HttpMethod(com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod) Test(org.junit.Test)

Aggregations

HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)130 Test (org.junit.Test)91 HashMap (java.util.HashMap)63 List (java.util.List)60 URL (java.net.URL)56 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)39 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)24 LinkedList (java.util.LinkedList)16 HttpMethod (com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod)14 IOException (java.io.IOException)10 DeviceParser (com.microsoft.azure.sdk.iot.deps.serializer.DeviceParser)8 Proxy (java.net.Proxy)7 ProxyOptions (com.microsoft.azure.sdk.iot.service.ProxyOptions)6 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)6 Map (java.util.Map)6 IotHubBadFormatException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException)5 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)5 ConfigurationParser (com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser)4 QueryRequestParser (com.microsoft.azure.sdk.iot.deps.serializer.QueryRequestParser)4 MalformedURLException (java.net.MalformedURLException)4