use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReadsStatusCode.
// 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 sendReadsStatusCode(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] body = new byte[0];
final int status = 204;
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(status));
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method connectStreamsRequestBody.
// Tests_SRS_HTTPSCONNECTION_25_006: [The function shall stream the request body, if present, through the connection.]
@Test
public void connectStreamsRequestBody() throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.PUT;
byte[] body = { 1, 2, 3 };
final byte[] expectedBody = { 1, 2, 3 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
}
};
// Act
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
conn.writeOutput(body);
body[0] = 5;
conn.connect();
// Assert
new Verifications() {
{
mockUrl.openConnection().getOutputStream().write(expectedBody);
}
};
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method constructorRejectsNonHttpsUrl.
// Tests_SRS_HTTPSCONNECTION_25_004: [If the URI given does not use the HTTPS protocol, the constructor shall throw an IllegalArgumentException.]
// Assert
@Test(expected = IllegalArgumentException.class)
public void constructorRejectsNonHttpsUrl() throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.PUT;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
}
};
// Act
new HttpConnection(mockUrl, httpsMethod);
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method constructorThrowsIoExceptionIfCannotOpenConnection.
// Tests_SRS_HTTPSCONNECTION_25_002: [The constructor shall throw an IOException if the connection was unable to be opened.]
// Assert
@Test(expected = IOException.class)
public void constructorThrowsIoExceptionIfCannotOpenConnection() throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.PUT;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = new IOException();
}
};
// Act
new HttpConnection(mockUrl, httpsMethod);
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendWritesBodyToOutputStream.
// Tests_SRS_HTTPSREQUEST_25_005: [The function shall send an HTTPS request as formatted in the constructor.]
@Test
public void sendWritesBodyToOutputStream() throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.POST;
final byte[] expectedBody = { 1, 2, 3 };
new MockUp<HttpConnection>() {
byte[] testBody;
@Mock
public void $init(URL url, HttpMethod method) {
}
@Mock
public void connect() throws IOException {
assertThat(testBody, is(expectedBody));
}
@Mock
public void writeOutput(byte[] body) {
this.testBody = body;
}
// every method that is used must be manually mocked.
@Mock
public void setRequestHeader(String field, String value) {
}
@Mock
public void setRequestMethod(HttpMethod method) {
}
@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
// Act
HttpRequest request = new HttpRequest(new URL("http://www.microsoft.com"), httpsMethod, expectedBody);
request.send();
}
Aggregations