use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method setReadTimeoutSetsReadTimeout.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_010: [The function shall set the read timeout for the request to the given value.]
@Test
public void setReadTimeoutSetsReadTimeout(@Mocked final HttpConnection mockConn) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.POST;
final byte[] body = new byte[0];
final int readTimeout = 1;
final int expectedReadTimeout = readTimeout;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
}
};
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, body);
// Act
request.setReadTimeoutMillis(readTimeout);
// Assert
new Verifications() {
{
mockConn.setReadTimeoutMillis(expectedReadTimeout);
}
};
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class DeviceOperationsTest method invoke_throwOnHttpRequest_failed.
/* Tests_SRS_DEVICE_OPERATIONS_21_008: [The request shall create a new HttpRequest with the provided `url`, http `method`, and `payload`.] */
@Test(expected = IOException.class)
public void invoke_throwOnHttpRequest_failed(@Mocked IotHubServiceSasToken iotHubServiceSasToken) throws Exception {
//arrange
new NonStrictExpectations() {
{
iotHubServiceSasToken.toString();
result = STANDARD_SASTOKEN_STRING;
}
};
new MockUp<HttpRequest>() {
@Mock
void $init(URL url, HttpMethod method, byte[] body) throws IOException {
throw new IOException();
}
};
//act
HttpResponse response = DeviceOperations.request(IOT_HUB_CONNECTION_STRING, new URL(STANDARD_URL), HttpMethod.POST, STANDARD_PAYLOAD, STANDARD_REQUEST_ID, DEFAULT_HTTP_TIMEOUT_MS);
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class DeviceMethodTest method invoke_throwOnHttpRequester_failed.
/* Tests_SRS_DEVICEMETHOD_21_009: [The invoke shall send the created request and get the response using the HttpRequester.] */
/* Tests_SRS_DEVICEMETHOD_21_010: [The invoke shall create a new HttpRequest with http method as `POST`.] */
@Test(expected = IotHubException.class)
public void invoke_throwOnHttpRequester_failed(@Mocked final MethodParser methodParser, @Mocked final IotHubConnectionStringBuilder mockedConnectionStringBuilder) throws Exception {
//arrange
DeviceMethod testMethod = DeviceMethod.createFromConnectionString(STANDARD_CONNECTIONSTRING);
new NonStrictExpectations() {
{
methodParser.toJson();
result = STANDARD_JSON;
iotHubConnectionString.getUrlMethod(STANDARD_DEVICEID);
result = STANDARD_URL;
}
};
new MockUp<DeviceOperations>() {
@Mock
HttpResponse request(IotHubConnectionString iotHubConnectionString, URL url, HttpMethod method, byte[] payload, String requestId, long timeoutInMs) throws IOException, IotHubException, IllegalArgumentException {
throw new IotHubException();
}
};
//act
testMethod.invoke(STANDARD_DEVICEID, STANDARD_METHODNAME, STANDARD_TIMEOUT_SECONDS, STANDARD_TIMEOUT_SECONDS, STANDARD_PAYLOAD_MAP);
}
use of com.microsoft.azure.sdk.iot.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method constructorSetsHttpsMethodCorrectly.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_003: [The function shall use the given HTTPS method (i.e. GET) as the request method.]
@Test
public void constructorSetsHttpsMethodCorrectly(@Mocked final HttpConnection mockConn) 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.service.transport.http.HttpMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendWritesBodyToOutputStream.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSREQUEST_12_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
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "http";
}
};
// Act
HttpRequest request = new HttpRequest(mockUrl, httpsMethod, expectedBody);
request.send();
}
Aggregations