use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class DeviceOperationsTest method invokeThrowOnUserAgentFailed.
/* Tests_SRS_DEVICE_OPERATIONS_21_012: [The request shall add to the HTTP header a `User-Agent` key with the client Id and service version.] */
@Test(expected = IllegalArgumentException.class)
public void invokeThrowOnUserAgentFailed(@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, anyString);
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 IotHubExceptionManagerTest method httpResponseVerification400WithInnerMessageAndException.
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_013: [If the httpresponse contains a reason message, the function must print this reason in the error message]
// Assert
@Test
public void httpResponseVerification400WithInnerMessageAndException() throws IotHubException {
// Arrange
final int status = 400;
final byte[] body = { 1 };
final Map<String, List<String>> headerFields = new HashMap<>();
final byte[] errorReason = "{\"Message\":\"ErrorCode:ArgumentInvalid;Error: BadRequest {\\\"Message\\\":\\\"ErrorCode:ArgumentInvalid;Missing or invalid etag for job type ScheduleUpdateTwin. ScheduleUpdateTwin job type is a force update, which only accepts '*' as the Etag.\\\",\\\"ExceptionMessage\\\":\\\"Tracking ID:1234-TimeStamp:06/26/2017 20:56:33\\\"}\",\"ExceptionMessage\":\"Tracking ID:5678-G:10-TimeStamp:06/26/2017 20:56:33\"}".getBytes(StandardCharsets.UTF_8);
HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
// Act
try {
IotHubExceptionManager.httpResponseVerification(response);
assert true;
} catch (IotHubBadFormatException expected) {
// Expected throw.
assertThat(expected.getMessage(), is("ErrorCode:ArgumentInvalid;Missing or invalid etag for job type ScheduleUpdateTwin. ScheduleUpdateTwin job type is a force update, which only accepts '*' as the Etag. Tracking ID:1234-TimeStamp:06/26/2017 20:56:33"));
}
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class IotHubExceptionManagerTest method httpResponseVerification409.
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_34_018: [The function shall throw IotHubConflictException if the Http response status equal 409]
// Assert
@Test(expected = IotHubConflictException.class)
public void httpResponseVerification409() throws IotHubException {
// Arrange
final int status = 409;
final byte[] body = { 1 };
final Map<String, List<String>> headerFields = new HashMap<>();
final byte[] errorReason = { 2, 3, 4, 5 };
HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
// Act
IotHubExceptionManager.httpResponseVerification(response);
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class IotHubExceptionManagerTest method httpResponseVerification400WithNULLErrorReason.
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_013: [If the httpresponse contains a reason message, the function must print this reason in the error message]
// Assert
@Test
public void httpResponseVerification400WithNULLErrorReason() throws IotHubException {
// Arrange
final int status = 400;
final byte[] body = { 1 };
final Map<String, List<String>> headerFields = new HashMap<>();
final byte[] errorReason = "{\"ExceptionMessage\":null}".getBytes(StandardCharsets.UTF_8);
HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
// Act
try {
IotHubExceptionManager.httpResponseVerification(response);
assert true;
} catch (IotHubBadFormatException expected) {
// Expected throw.
assertThat(expected.getMessage(), is("{\"ExceptionMessage\":null}"));
}
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class IotHubExceptionManagerTest method httpResponseVerification502.
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_21_008: [The function shall throw IotHubBadGatewayException if the Http response status equal 502]
// Assert
@Test(expected = IotHubBadGatewayException.class)
public void httpResponseVerification502() throws IotHubException {
// Arrange
final int status = 502;
final byte[] body = { 1 };
final Map<String, List<String>> headerFields = new HashMap<>();
final byte[] errorReason = { 2, 3, 4, 5 };
HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
// Act
IotHubExceptionManager.httpResponseVerification(response);
}
Aggregations