use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsStatusCodeOnBadStatusException.
// Tests_SRS_HTTPSREQUEST_25_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
@Test
public void sendReturnsStatusCodeOnBadStatusException(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.POST;
final byte[] body = new byte[0];
final int badStatus = 404;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
mockConn.connect();
result = new IOException();
mockConn.getResponseStatus();
result = badStatus;
}
};
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
// Act
HttpResponse response = request.send();
int testStatus = response.getStatus();
// Assert
assertThat(testStatus, is(badStatus));
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsError.
// Tests_SRS_HTTPSREQUEST_25_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
// Tests_SRS_HTTPSREQUEST_25_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 sendReturnsError(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] body = new byte[0];
final byte[] error = { 5, 6, 7, 0, 1 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
mockConn.connect();
result = new IOException();
mockConn.readError();
result = error;
}
};
// Act
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
HttpResponse response = request.send();
byte[] testError = response.getErrorReason();
// Assert
assertThat(testError, is(error));
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReadsStatusCode.
// Tests_SRS_HTTPSREQUEST_25_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
@Test
public void sendReadsStatusCode(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] body = new byte[0];
final int status = 204;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
mockConn.getResponseStatus();
result = status;
}
};
// Act
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
HttpResponse response = request.send();
int testStatus = response.getStatus();
// Assert
assertThat(testStatus, is(status));
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsBody.
// Tests_SRS_HTTPSREQUEST_25_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, @Mocked final URL mockUrl) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] requestBody = new byte[0];
final byte[] responseBody = { 1, 2, 3, 0, 4 };
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(responseBody));
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class HttpsResponseTest method getHeaderFieldMatchesCaseInsensitive.
// Tests_SRS_HTTPSRESPONSE_25_005: [The function shall match the header field name in a case-insensitive manner.]
@Test
public void getHeaderFieldMatchesCaseInsensitive() {
// Arrange
final int status = 200;
final byte[] body = { 1 };
final byte[] errorReason = {};
final Map<String, List<String>> headerFields = new HashMap<>();
final String field = "test-field";
final List<String> values = new LinkedList<>();
final String value0 = "test-field-value0";
final String value1 = "test-field-value1";
final String expectedValues = value0 + "," + value1;
values.add(value0);
values.add(value1);
headerFields.put(field, values);
// Act
HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
String differentCaseField = "Test-Field";
String testValues = response.getHeaderField(differentCaseField);
// Assert
assertThat(testValues, is(expectedValues));
}
Aggregations