use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method constructorThrowsIoExceptionIfCannotSetupConnection.
// Tests_SRS_HTTPSREQUEST_25_004: [If an IOException occurs in setting up the HTTPS connection, the function shall throw an IOException.]
// Assert
@Test(expected = IOException.class)
public void constructorThrowsIoExceptionIfCannotSetupConnection(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] body = new byte[0];
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
new HttpConnection(mockUrl, httpsMethod);
result = new IOException();
}
};
// Act
new HttpRequest(mockUrl, httpsMethod, body);
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsHeaderFieldsOnBadStatusException.
// 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 sendReturnsHeaderFieldsOnBadStatusException(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) 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];
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(value));
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendSetsHeaderFieldsCorrectly.
// Tests_SRS_HTTPSREQUEST_25_005: [The function shall send an HTTPS request as formatted in the constructor.]
@Test
public void sendSetsHeaderFieldsCorrectly() throws IOException {
// Arrange
final HttpMethod expectedMethod = HttpMethod.GET;
final byte[] body = new byte[0];
final String field0 = "test-field0";
final String value0 = "test-value0";
final String field1 = "test-field1";
final String value1 = "test-value1";
new MockUp<HttpConnection>() {
final Map<String, String> testHeaderFields = new HashMap<>();
@Mock
void $init(URL url, HttpMethod method) {
}
@Mock
void connect() throws IOException {
assertThat(testHeaderFields.size(), is(2));
assertThat(testHeaderFields.get(field0), is(value0));
assertThat(testHeaderFields.get(field1), is(value1));
}
@Mock
public void setRequestHeader(String field, String value) {
testHeaderFields.put(field, value);
}
// every method that is used must be manually mocked.
@Mock
public void setRequestMethod(HttpMethod method) {
}
@Mock
public void writeOutput(byte[] body) {
}
@Mock
public byte[] readInput() throws IOException {
return new byte[0];
}
@Mock
public byte[] readError() throws IOException {
return new byte[0];
}
@Mock
public int getResponseStatus() throws IOException {
return 0;
}
@Mock
public Map<String, List<String>> getResponseHeaders() throws IOException {
return new HashMap<>();
}
};
// Assert
// Act
HttpRequest request = new HttpRequest(new URL("http://www.microsoft.com"), expectedMethod, body);
request.setHeaderField(field0, value0);
request.setHeaderField(field1, value1);
request.send();
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method getResponseStatusReturnsResponseStatus.
// Tests_SRS_HTTPSCONNECTION_25_020: [The function shall return the response status code.]
@Test
public void getResponseStatusReturnsResponseStatus(@Mocked final InputStream mockIs) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final int status = 204;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getResponseCode();
result = status;
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
conn.connect();
// Act
int testStatus = conn.getResponseStatus();
// Assert
assertThat(testStatus, is(status));
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method readInputClosesInputStream.
// Tests_SRS_HTTPSCONNECTION_25_016: [The function shall close the input stream after it has been completely read.]
@Test
public void readInputClosesInputStream(@Mocked final InputStream mockIs) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getInputStream();
result = mockIs;
mockIs.read();
result = -1;
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
conn.connect();
// Act
conn.readInput();
// Assert
new Verifications() {
{
mockIs.close();
}
};
}
Aggregations