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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations