use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method constructorOpensConnection.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_001: [The constructor shall open a connection to the given URL.]
@Test
public void constructorOpensConnection() throws IOException {
// Arrange
final HttpMethod httpMethod = HttpMethod.PUT;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
}
};
// Act
new HttpConnection(mockUrl, httpMethod);
// Assert
new Verifications() {
{
mockUrl.openConnection();
}
};
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendHasCorrectHttpsMethod.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_005: [The function shall send an HTTPS request as formatted in the constructor.]
@Test
public void sendHasCorrectHttpsMethod() throws IOException {
// Arrange
final HttpMethod expectedMethod = HttpMethod.GET;
final byte[] body = new byte[0];
new MockUp<HttpConnection>() {
HttpMethod testMethod;
@Mock
public void $init(URL url, HttpMethod method) {
this.testMethod = method;
}
@Mock
public void connect() throws IOException {
assertThat(testMethod, is(expectedMethod));
}
@Mock
public void setRequestMethod(HttpMethod method) {
this.testMethod = method;
}
// every method that is used must be manually mocked.
@Mock
public void setRequestHeader(String field, String value) {
}
@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
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
}
};
HttpRequest request = new HttpRequest(mockUrl, expectedMethod, body);
// Act
request.send();
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsStatusCodeOnBadStatusException.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
@Test
public void sendReturnsStatusCodeOnBadStatusException(@Mocked final HttpConnection mockConn) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.POST;
final byte[] body = new byte[0];
final int badStatus = 404;
final int expectedStatus = badStatus;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
mockConn.connect();
result = new IOException();
mockConn.getResponseStatus();
result = badStatus;
}
};
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
// Act
HttpResponse response = request.send();
int testStatus = response.getStatus();
// Assert
assertThat(testStatus, is(expectedStatus));
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReadsStatusCode.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_006: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
@Test
public void sendReadsStatusCode(@Mocked final HttpConnection mockConn) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] body = new byte[0];
final int status = 204;
final int expectedStatus = status;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
mockConn.getResponseStatus();
result = status;
}
};
// Act
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
HttpResponse response = request.send();
int testStatus = response.getStatus();
// Assert
assertThat(testStatus, is(expectedStatus));
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsBody.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_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) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] requestBody = new byte[0];
final byte[] responseBody = { 1, 2, 3, 0, 4 };
final byte[] expectedBody = responseBody;
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(expectedBody));
}
Aggregations