use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection 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.HttpConnection 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.HttpConnection 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();
}
};
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method writeOutputFailsWhenMethodIsNotPostOrPut.
// Tests_SRS_HTTPSCONNECTION_25_012: [The function shall save the body to be sent with the request.]
// Assert
@Test(expected = IllegalArgumentException.class)
public void writeOutputFailsWhenMethodIsNotPostOrPut() throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] body = { 1, 2 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
// Act
conn.writeOutput(body);
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method readInputCompletelyReadsInputStream.
// Tests_SRS_HTTPSCONNECTION_25_014: [The function shall read from the input stream (response stream) and return the response.]
@Test
public void readInputCompletelyReadsInputStream(@Mocked final InputStream mockIs) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
byte[] expectedResponse = { 1, 2, 3 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getInputStream();
result = mockIs;
mockIs.read();
returns(1, 2, 3, -1);
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
conn.connect();
// Act
byte[] testResponse = conn.readInput();
// Assert
assertThat(testResponse, is(expectedResponse));
}
Aggregations