use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method writeOutputDoesNotFailWhenBodyIsEmpty.
// Tests_SRS_HTTPSCONNECTION_25_013: [The function shall throw an IllegalArgumentException if the request does not currently use method POST or PUT and the body is non-empty.]
@Test
public void writeOutputDoesNotFailWhenBodyIsEmpty() throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] body = new byte[0];
// Assert
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 readErrorCompletelyReadsErrorStream.
// Tests_SRS_HTTPSCONNECTION_25_017: [The function shall read from the error stream and return the response.]
@Test
public void readErrorCompletelyReadsErrorStream(@Mocked final InputStream mockIs) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
byte[] expectedError = { 1, 2, 3 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getErrorStream();
result = mockIs;
mockIs.read();
returns(1, 2, 3, -1);
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
conn.connect();
// Act
byte[] testError = conn.readError();
// Assert
assertThat(testError, is(expectedError));
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method readInputClosesInputStreamEvenIfExceptionOccurs.
// Tests_SRS_HTTPSCONNECTION_25_016: [The function shall close the input stream after it has been completely read.]
@Test
public void readInputClosesInputStreamEvenIfExceptionOccurs(@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 = new IOException("This is a test exception");
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
conn.connect();
// Act
try {
conn.readInput();
} catch (IOException e) {
// expected exception, but not testing for it, so it can be ignored
}
// Assert
new Verifications() {
{
mockIs.close();
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method constructorOpensConnection.
// Tests_SRS_HTTPSREQUEST_25_001: [The function shall open a connection with the given URL as the endpoint.]
@Test
public void constructorOpensConnection(@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";
}
};
// Act
new HttpRequest(mockUrl, httpsMethod, body);
// Assert
new Verifications() {
{
new HttpConnection(mockUrl, (HttpMethod) any);
}
};
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpConnection in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method constructorSetsHttpsMethodCorrectly.
// Tests_SRS_HTTPSREQUEST_25_003: [The function shall use the given HTTPS method (i.e. GET) as the request method.]
@Test
public void constructorSetsHttpsMethodCorrectly(@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";
}
};
// Act
new HttpRequest(mockUrl, httpsMethod, body);
// Assert
new Verifications() {
{
new HttpConnection((URL) any, httpsMethod);
}
};
}
Aggregations