use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsBody.
// Tests_SRS_HTTPSREQUEST_25_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
@Test
public void sendReturnsBody(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] requestBody = new byte[0];
final byte[] responseBody = { 1, 2, 3, 0, 4 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
mockConn.readInput();
result = responseBody;
}
};
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, requestBody);
// Act
HttpResponse response = request.send();
byte[] testBody = response.getBody();
// Assert
assertThat(testBody, is(responseBody));
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method setReadTimeoutSetsReadTimeout.
// Tests_SRS_HTTPSREQUEST_25_010: [The function shall set the read timeout for the request to the given value.]
@Test
public void setReadTimeoutSetsReadTimeout(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.POST;
final byte[] body = new byte[0];
final int readTimeout = 1;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
}
};
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
// Act
request.setReadTimeoutMillis(readTimeout);
// Assert
new Verifications() {
{
mockConn.setReadTimeoutMillis(readTimeout);
}
};
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method setHeaderFieldSetsHeaderField.
// Tests_SRS_HTTPSREQUEST_25_009: [The function shall set the header field with the given name to the given value.]
@Test
public void setHeaderFieldSetsHeaderField(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.POST;
final byte[] body = new byte[0];
final String field = "test-field";
final String value = "test-value";
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
}
};
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
// Act
request.setHeaderField(field, value);
// Assert
new Verifications() {
{
mockConn.setRequestHeader(field, value);
}
};
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest 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.HttpRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendThrowsIoExceptionIfCannotConnect.
// Tests_SRS_HTTPSREQUEST_25_007: [If the client cannot connect to the server, the function shall throw an IOException.]
// Assert
@Test(expected = IOException.class)
public void sendThrowsIoExceptionIfCannotConnect(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
// Arrange
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 = new IOException();
mockConn.getResponseStatus();
result = new IOException();
mockConn.readInput();
result = new IOException();
mockConn.readError();
result = new IOException();
}
};
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
// Act
request.send();
}
Aggregations