use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class DeviceOperationsTest method invoke_httprequesttimeout_succeed.
/* Tests_SRS_DEVICE_OPERATIONS_21_017: [If the resulted status represents success, the request shall return the http response.] */
@Test
public void invoke_httprequesttimeout_succeed(@Mocked IotHubServiceSasToken iotHubServiceSasToken, @Mocked HttpRequest httpRequest) throws Exception {
//arrange
// 30 seconds
final long responseTimeout = 30000;
// 5 seconds
final long connectTimeout = 5000;
// Calculate total timeout in milliseconds
int timeoutInMs = (int) (responseTimeout + connectTimeout);
final int status = 200;
final byte[] body = { 1 };
final Map<String, List<String>> headerFields = new HashMap<>();
final byte[] errorReason = "succeed".getBytes();
HttpResponse sendResponse = new HttpResponse(status, body, headerFields, errorReason);
new NonStrictExpectations() {
{
iotHubServiceSasToken.toString();
result = STANDARD_SASTOKEN_STRING;
httpRequest.setReadTimeoutMillis(timeoutInMs + 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.getJavaServiceClientIdentifier() + TransportUtils.getServiceVersion());
result = httpRequest;
httpRequest.setHeaderField(ACCEPT, ACCEPT_VALUE);
result = httpRequest;
httpRequest.setHeaderField(CONTENT_TYPE, ACCEPT_VALUE + "; " + ACCEPT_CHARSET);
result = httpRequest;
httpRequest.send();
result = sendResponse;
IotHubExceptionManager.httpResponseVerification(sendResponse);
}
};
//act
HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, new URL(STANDARD_URL), HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, timeoutInMs);
//assert
assertEquals(response, sendResponse);
new Verifications() {
{
iotHubServiceSasToken.toString();
times = 1;
httpRequest.setReadTimeoutMillis(timeoutInMs + DEFAULT_HTTP_TIMEOUT_MS);
times = 1;
httpRequest.setHeaderField(anyString, anyString);
times = 5;
httpRequest.send();
times = 1;
IotHubExceptionManager.httpResponseVerification(sendResponse);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsError.
// 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).]
// 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 sendReturnsError(@Mocked final HttpConnection mockConn) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] body = new byte[0];
final byte[] error = { 5, 6, 7, 0, 1 };
final byte[] expectedError = error;
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(expectedError));
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsHeaderFields.
// 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 sendReturnsHeaderFields(@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 value0 = "test-value0";
final String value1 = "test-value1";
values.add(value0);
values.add(value1);
headerFields.put(field, values);
final String expectedValues = value0 + "," + value1;
final HttpMethod httpsMethod = HttpMethod.POST;
final byte[] body = new byte[0];
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
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 HttpsResponseTest method getHeaderFieldMatchesCaseInsensitive.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSRESPONSE_12_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));
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse in project azure-iot-sdk-java by Azure.
the class HttpsResponseTest method getHeaderFieldRejectsInvalidFieldName.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSRESPONSE_12_006: [If a value could not be found for the given header field name, the function shall throw an IllegalArgumentException.]
// Assert
@Test(expected = IllegalArgumentException.class)
public void getHeaderFieldRejectsInvalidFieldName() throws IllegalArgumentException {
// 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";
// Act
HttpResponse response = new HttpResponse(status, body, headerFields, errorReason);
response.getHeaderField(field);
}
Aggregations