use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsBody.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
@Test
public void sendReturnsBody(@Mocked final HttpConnection mockConn) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] requestBody = new byte[0];
final byte[] responseBody = { 1, 2, 3, 0, 4 };
final byte[] expectedBody = responseBody;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
mockConn.readInput();
result = responseBody;
}
};
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, requestBody);
// Act
HttpResponse response = request.send();
byte[] testBody = response.getBody();
// Assert
assertThat(testBody, is(expectedBody));
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsHeaderFieldsOnBadStatusException.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_008: [If an I/O exception occurs because of a bad response status code, the function shall attempt to flush or read the error stream so that the underlying HTTPS connection can be reused.]
@Test
public void sendReturnsHeaderFieldsOnBadStatusException(@Mocked final HttpConnection mockConn) throws IOException {
// Arrange
final Map<String, List<String>> headerFields = new HashMap<>();
final String field = "test-field";
final List<String> values = new LinkedList<>();
final String value = "test-value0";
values.add(value);
headerFields.put(field, values);
final HttpMethod httpsMethod = HttpMethod.POST;
final byte[] body = new byte[0];
final String expectedValues = value;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
mockConn.connect();
result = new IOException();
mockConn.getResponseHeaders();
result = headerFields;
}
};
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
// Act
HttpResponse response = request.send();
String testValues = response.getHeaderField(field);
// Assert
assertThat(testValues, is(expectedValues));
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class IotHubExceptionManagerTest method httpResponseVerification_403.
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_003: [The function shall throw IotHubTooManyDevicesException if the Http response status equal 403]
// Assert
@Test(expected = IotHubTooManyDevicesException.class)
public void httpResponseVerification_403() throws IotHubException {
// Arrange
final int status = 403;
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 httpResponseVerification_412.
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_005: [The function shall throw IotHubPreconditionFailedException if the Http response status equal 412]
// Assert
@Test(expected = IotHubPreconditionFailedException.class)
public void httpResponseVerification_412() throws IotHubException {
// Arrange
final int status = 412;
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 httpResponseVerification_300.
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBEXCEPTIONMANAGER_12_012: [The function shall return without exception if the response status equal or less than 300]
@Test
public void httpResponseVerification_300() throws IotHubException {
// Arrange
final int status = 300;
final byte[] body = { 1 };
final Map<String, List<String>> headerFields = new HashMap<>();
final byte[] errorReason = { 123, 125 };
HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
// Act
IotHubExceptionManager.httpResponseVerification(response);
IotHubException iotHubException = new IotHubException();
IotHubExceptionManager iotHubExceptionManager = new IotHubExceptionManager();
// Assert
assertNotEquals(null, iotHubException);
assertNotEquals(null, iotHubExceptionManager);
}
Aggregations