use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest 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);
}
};
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method constructorThrowsIoExceptionIfCannotSetupConnection.
// Tests_SRS_HTTPSREQUEST_25_004: [If an IOException occurs in setting up the HTTPS connection, the function shall throw an IOException.]
// Assert
@Test(expected = IOException.class)
public void constructorThrowsIoExceptionIfCannotSetupConnection(@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";
new HttpConnection(mockUrl, httpsMethod);
result = new IOException();
}
};
// Act
new HttpRequest(mockUrl, httpsMethod, body);
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsHeaderFieldsOnBadStatusException.
// Tests_SRS_HTTPSREQUEST_25_008: [If an I/O exception occurs because of a bad response status code, the function shall attempt to flush or read the error stream so that the underlying HTTPS connection can be reused.]
@Test
public void sendReturnsHeaderFieldsOnBadStatusException(@Mocked final HttpConnection mockConn, @Mocked final URL mockUrl) throws IOException {
// Arrange
final Map<String, List<String>> headerFields = new HashMap<>();
final String field = "test-field";
final List<String> values = new LinkedList<>();
final String value = "test-value0";
values.add(value);
headerFields.put(field, values);
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 = headerFields;
}
};
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
// Act
HttpResponse response = request.send();
String testValues = response.getHeaderField(field);
// Assert
assertThat(testValues, is(value));
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendSetsHeaderFieldsCorrectly.
// Tests_SRS_HTTPSREQUEST_25_005: [The function shall send an HTTPS request as formatted in the constructor.]
@Test
public void sendSetsHeaderFieldsCorrectly() throws IOException {
// Arrange
final HttpMethod expectedMethod = HttpMethod.GET;
final byte[] body = new byte[0];
final String field0 = "test-field0";
final String value0 = "test-value0";
final String field1 = "test-field1";
final String value1 = "test-value1";
new MockUp<HttpConnection>() {
final Map<String, String> testHeaderFields = new HashMap<>();
@Mock
void $init(URL url, HttpMethod method) {
}
@Mock
void connect() throws IOException {
assertThat(testHeaderFields.size(), is(2));
assertThat(testHeaderFields.get(field0), is(value0));
assertThat(testHeaderFields.get(field1), is(value1));
}
@Mock
public void setRequestHeader(String field, String value) {
testHeaderFields.put(field, value);
}
// every method that is used must be manually mocked.
@Mock
public void setRequestMethod(HttpMethod method) {
}
@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
// Act
HttpRequest request = new HttpRequest(new URL("http://www.microsoft.com"), expectedMethod, body);
request.setHeaderField(field0, value0);
request.setHeaderField(field1, value1);
request.send();
}
use of com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest in project azure-iot-sdk-java by Azure.
the class ContractApiHttpTest method requestCreatesHttpRequest.
/* SRS_HTTP_DEVICE_REGISTRATION_CLIENT_21_010: [The request shall create a new HttpRequest.*/
@Test
public void requestCreatesHttpRequest() throws ProvisioningServiceClientException, IOException {
// arrange
new Expectations() {
{
new ProvisioningSasToken(mockedProvisioningConnectionString);
result = mockedProvisioningSasToken;
mockedProvisioningSasToken.toString();
result = VALID_SASTOKEN;
mockedProvisioningConnectionString.getHostName();
result = VALID_HOST_NAME;
new URL((String) any);
result = mockedURL;
new HttpRequest(mockedURL, HttpMethod.PUT, VALID_PAYLOAD.getBytes(StandardCharsets.UTF_8));
result = mockedHttpRequest;
mockedHttpRequest.send();
result = mockedHttpResponse;
mockedHttpResponse.getStatus();
result = VALID_SUCCESS_STATUS;
mockedHttpResponse.getErrorReason();
result = VALID_SUCCESS_MESSAGE.getBytes(StandardCharsets.UTF_8);
mockedHttpResponse.getBody();
result = VALID_BODY;
mockedHttpResponse.getHeaderFields();
result = VALID_HEADER;
ProvisioningServiceClientExceptionManager.httpResponseVerification(VALID_SUCCESS_STATUS, VALID_SUCCESS_MESSAGE);
}
};
ContractApiHttp contractApiHttp = ContractApiHttp.createFromConnectionString(mockedProvisioningConnectionString);
requestNonStrictExpectations();
// act
contractApiHttp.request(HttpMethod.PUT, VALID_PATH, VALID_HEADER, VALID_PAYLOAD);
}
Aggregations