use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection in project azure-iot-sdk-java by Azure.
the class HttpsConnectionTest method getResponseHeadersReturnsResponseHeaders.
// Tests_SRS_HTTPSCONNECTION_11_017: [The function shall return a mapping of header field names to the values associated with the header field name.]
@Test
public void getResponseHeadersReturnsResponseHeaders() throws IOException, TransportException {
final HttpsMethod httpsMethod = HttpsMethod.GET;
final String field0 = "test-field0";
final String value0 = "test-value0";
final String field1 = "test-field1";
final String value1 = "test-value1";
final List<String> values0 = new LinkedList<>();
values0.add(value0);
final List<String> values1 = new LinkedList<>();
values1.add(value1);
final Map<String, List<String>> responseHeaders = new HashMap<>();
responseHeaders.put(field0, values0);
responseHeaders.put(field1, values1);
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getHeaderFields();
result = responseHeaders;
}
};
HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
conn.connect();
Map<String, List<String>> testResponseHeaders = conn.getResponseHeaders();
assertThat(testResponseHeaders.size(), is(responseHeaders.size()));
// the list of values for each field is of size 1, so the lists
// can be directly compared.
assertThat(testResponseHeaders.get(field0), is(responseHeaders.get(field0)));
assertThat(testResponseHeaders.get(field1), is(responseHeaders.get(field1)));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection in project azure-iot-sdk-java by Azure.
the class HttpsConnectionTest method setRequestMethodRejectsNonPostOrPutIfHasBody.
// Tests_SRS_HTTPSCONNECTION_11_007: [The function shall throw an IllegalArgumentException if the request currently has a non-empty body and the new method is not a POST or a PUT.]
@Test(expected = IllegalArgumentException.class)
public void setRequestMethodRejectsNonPostOrPutIfHasBody() throws IOException, TransportException {
final HttpsMethod httpsMethod = HttpsMethod.POST;
final HttpsMethod illegalHttpsMethod = HttpsMethod.DELETE;
final byte[] body = { 1, 2, 3 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
returns(httpsMethod.name(), illegalHttpsMethod.name());
}
};
HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
conn.writeOutput(body);
conn.setRequestMethod(illegalHttpsMethod);
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection in project azure-iot-sdk-java by Azure.
the class HttpsConnectionTest method readErrorAlwaysClosesStream.
// Tests_SRS_HTTPSCONNECTION_11_020: [The function shall close the error stream after it has been completely read.]
@Test
public void readErrorAlwaysClosesStream(@Mocked final InputStream mockIs) throws IOException, TransportException {
final HttpsMethod httpsMethod = HttpsMethod.GET;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getErrorStream();
result = mockIs;
mockIs.read();
result = new TransportException("This is a test exception");
}
};
HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
conn.connect();
// act
try {
byte[] testError = conn.readError();
} catch (TransportException e) {
// expected exception, but not testing for it, so it can be ignored
}
new Verifications() {
{
mockIs.close();
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection in project azure-iot-sdk-java by Azure.
the class HttpsConnectionTest method getBodyReturnsSavedBody.
// Tests_SRS_HTTPSCONNECTION_34_031: [The function shall return the saved body.]
@Test
public void getBodyReturnsSavedBody() throws IOException, TransportException {
// arrange
final HttpsMethod httpsMethod = HttpsMethod.POST;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getURL();
result = mockUrl;
}
};
HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
byte[] expectedBody = "test".getBytes(StandardCharsets.UTF_8);
conn.writeOutput(expectedBody);
// act
byte[] actual = Deencapsulation.invoke(conn, "getBody");
// assert
Assert.assertArrayEquals(expectedBody, actual);
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method constructorOpensConnection.
// Tests_SRS_HTTPSREQUEST_11_001: [The function shall open a connection with the given URL as the endpoint.]
@Test
public void constructorOpensConnection(@Mocked final HttpsConnection mockConn) throws TransportException, MalformedURLException {
final HttpsMethod httpsMethod = HttpsMethod.GET;
final byte[] body = new byte[0];
final URL mockUrl = new URL("https://www.microsoft.com");
HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body, "");
request.send();
new Verifications() {
{
new HttpsConnection(mockUrl, (HttpsMethod) any, null, true);
}
};
}
Aggregations