use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod 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.HttpMethod 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.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method readErrorFailsIfCannotAccessErrorStream.
// Tests_SRS_HTTPSCONNECTION_25_018: [The function shall throw an IOException if the error stream could not be accessed.]
// Assert
@Test(expected = IOException.class)
public void readErrorFailsIfCannotAccessErrorStream() throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getErrorStream();
result = new IOException();
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
conn.connect();
// Act
conn.readError();
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method readInputFailsIfCannotAccessInputStream.
// Tests_SRS_HTTPSCONNECTION_25_015: [The function shall throw an IOException if the input stream could not be accessed.]
// Assert
@Test(expected = IOException.class)
public void readInputFailsIfCannotAccessInputStream() 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 = new IOException();
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
conn.connect();
// Act
conn.readInput();
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method setRequestMethodRejectsNonPostOrPutIfHasBody.
// Tests_SRS_HTTPSCONNECTION_25_009: [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.]
// Assert
@Test(expected = IllegalArgumentException.class)
public void setRequestMethodRejectsNonPostOrPutIfHasBody() throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.POST;
final HttpMethod illegalHttpsMethod = HttpMethod.DELETE;
final byte[] body = { 1, 2, 3 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
returns(httpsMethod.name(), illegalHttpsMethod.name());
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
conn.writeOutput(body);
// Act
conn.setRequestMethod(illegalHttpsMethod);
}
Aggregations