use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReadsStatusCode.
// Tests_SRS_HTTPSREQUEST_11_009: [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 HttpsConnection mockConn) throws IOException {
final HttpsMethod httpsMethod = HttpsMethod.GET;
final byte[] body = new byte[0];
final int status = 204;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockConn.getResponseStatus();
result = status;
}
};
HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
HttpsResponse response = request.send();
int testStatus = response.getStatus();
final int expectedStatus = status;
assertThat(testStatus, is(expectedStatus));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsStatusCodeOnBadStatusException.
// Tests_SRS_HTTPSREQUEST_11_009: [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 HttpsConnection mockConn) throws IOException {
final HttpsMethod httpsMethod = HttpsMethod.POST;
final byte[] body = new byte[0];
final int badStatus = 404;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockConn.connect();
result = new IOException();
mockConn.getResponseStatus();
result = badStatus;
}
};
HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
HttpsResponse response = request.send();
int testStatus = response.getStatus();
final int expectedStatus = badStatus;
assertThat(testStatus, is(expectedStatus));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsHeaderFieldsOnBadStatusException.
// Tests_SRS_HTTPSREQUEST_11_009: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
@Test
public void sendReturnsHeaderFieldsOnBadStatusException(@Mocked final HttpsConnection mockConn) throws IOException {
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 HttpsMethod httpsMethod = HttpsMethod.POST;
final byte[] body = new byte[0];
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockConn.connect();
result = new IOException();
mockConn.getResponseHeaders();
result = headerFields;
}
};
HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
HttpsResponse response = request.send();
String testValues = response.getHeaderField(field);
final String expectedValues = value;
assertThat(testValues, is(expectedValues));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse in project azure-iot-sdk-java by Azure.
the class HttpsRequestIT method sendHttpsRequestGetsCorrectResponse.
@Test
public void sendHttpsRequestGetsCorrectResponse() throws IOException {
URL url = new URL("https://fonts.googleapis.com/css?family=Inconsolata");
HttpsMethod method = HttpsMethod.GET;
byte[] body = new byte[0];
String encoding = "UTF-8";
HttpsRequest request = new HttpsRequest(url, method, body);
request.setHeaderField("accept-charset", encoding);
HttpsResponse response = request.send();
int testStatus = response.getStatus();
String testBody = new String(response.getBody(), encoding);
int expectedStatus = 200;
String expectedBodyPrefix = "@font-face {\n" + " font-family: 'Inconsolata';";
assertThat(testStatus, is(expectedStatus));
assertThat(testBody, is(startsWith(expectedBodyPrefix)));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsResponse in project azure-iot-sdk-java by Azure.
the class HttpsResponseTest method getHeaderFieldReturnsHeaderField.
// Tests_SRS_HTTPSRESPONSE_11_001: [The constructor shall store the input arguments so that the getters can return them later.]
// Tests_SRS_HTTPSRESPONSE_11_004: [The function shall return a comma-separated list of the values associated with the header field name.]
@Test
public void getHeaderFieldReturnsHeaderField() {
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";
values.add(value0);
values.add(value1);
headerFields.put(field, values);
HttpsResponse response = new HttpsResponse(status, body, headerFields, errorReason);
String testValues = response.getHeaderField(field);
final String expectedValues = value0 + "," + value1;
assertThat(testValues, is(expectedValues));
}
Aggregations