use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsBody.
// Tests_SRS_HTTPSREQUEST_11_009: [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 HttpsConnection mockConn) throws IOException {
final HttpsMethod httpsMethod = HttpsMethod.GET;
final byte[] requestBody = new byte[0];
final byte[] responseBody = { 1, 2, 3, 0, 4 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockConn.readInput();
result = responseBody;
}
};
HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, requestBody);
HttpsResponse response = request.send();
byte[] testBody = response.getBody();
final byte[] expectedBody = responseBody;
assertThat(testBody, is(expectedBody));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method setSSLContextSetsSSLContext.
//Tests_SRS_HTTPSREQUEST_25_016: [The function shall set the SSL context for the IotHub.]
@Test
public void setSSLContextSetsSSLContext(@Mocked final HttpsConnection mockConn, @Mocked final IotHubSSLContext mockedContext) throws IOException {
final HttpsMethod httpsMethod = HttpsMethod.POST;
final byte[] body = new byte[0];
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
}
};
HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
request.setSSLContext(mockedContext);
new Verifications() {
{
Deencapsulation.invoke(mockConn, "setSSLContext", mockedContext.getIotHubSSlContext());
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method sendReturnsHeaderFields.
// Tests_SRS_HTTPSREQUEST_11_009: [The function shall return the HTTPS response received, including the status code, body, header fields, and error reason (if any).]
@Test
public void sendReturnsHeaderFields(@Mocked final HttpsConnection mockConn) throws IOException {
final Map<String, List<String>> headerFields = new HashMap<>();
final String field = "test-field";
final List<String> values = new LinkedList<>();
final String value0 = "test-value0";
final String value1 = "test-value1";
values.add(value0);
values.add(value1);
headerFields.put(field, values);
final HttpsMethod httpsMethod = HttpsMethod.POST;
final byte[] body = new byte[0];
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockConn.getResponseHeaders();
result = headerFields;
}
};
HttpsRequest request = new HttpsRequest(mockUrl, httpsMethod, body);
HttpsResponse response = request.send();
String testValues = response.getHeaderField(field);
final String expectedValues = value0 + "," + value1;
assertThat(testValues, is(expectedValues));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest in project azure-iot-sdk-java by Azure.
the class HttpsRequestTest method constructorWritesBodyToConnection.
// Tests_SRS_HTTPSREQUEST_11_002: [The function shall write the body to the connection.]
@Test
public void constructorWritesBodyToConnection(@Mocked final HttpsConnection mockConn) throws IOException {
final HttpsMethod httpsMethod = HttpsMethod.GET;
final byte[] body = { 1, 2, 3 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
}
};
new HttpsRequest(mockUrl, httpsMethod, body);
final byte[] expectedBody = body;
new Verifications() {
{
new HttpsConnection(mockUrl, (HttpsMethod) any).writeOutput(expectedBody);
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsRequest in project azure-iot-sdk-java by Azure.
the class HttpsHsmClient method sendRequestBasedOnScheme.
/**
* Send a given httpsRequest using the appropriate means based on the scheme (http vs unix) of the baseUrl
* @param httpsMethod the type of https method to call
* @param body the body of the https call
* @param baseUri the base uri to send the request to
* @param path the relative path of the request
* @param queryString the query string for the https request. Do not include the ? character
* @return the http response to the request
* @throws TransportException if the hsm cannot be reached
* @throws IOException if the hsm cannot be reached
*/
private HttpsResponse sendRequestBasedOnScheme(HttpsMethod httpsMethod, byte[] body, String baseUri, String path, String queryString) throws TransportException, IOException {
URL requestUrl;
if (this.scheme.equalsIgnoreCase(HTTPS_SCHEME) || this.scheme.equalsIgnoreCase(HTTP_SCHEME)) {
if (queryString != null && !queryString.isEmpty()) {
requestUrl = new URL(baseUri + path + "?" + queryString);
} else {
requestUrl = new URL(baseUri + path);
}
} else if (this.scheme.equalsIgnoreCase(UNIX_SCHEME)) {
// leave the url null, for unix flow, there is no need to build a URL instance
requestUrl = null;
} else {
throw new UnsupportedOperationException("unrecognized URI scheme. Only HTTPS, HTTP and UNIX are supported");
}
// requestUrl will be null, if unix socket is used, but HttpsRequest won't null check it until we send the request.
// In the unix case, we don't build the https request to send it, we just build it to hold all the information that
// will go into the unix socket request later, such as headers, method, etc.
HttpsRequest httpsRequest = new HttpsRequest(requestUrl, httpsMethod, body, "");
// Codes_SRS_HSMHTTPCLIENT_34_003: [This function shall build an http request with headers ContentType and Accept with value application/json.]
httpsRequest.setHeaderField("Accept", "application/json");
if (body.length > 0) {
httpsRequest.setHeaderField("Content-Type", "application/json");
}
HttpsResponse response;
if (this.scheme.equalsIgnoreCase(HTTPS_SCHEME)) {
response = httpsRequest.send();
} else if (this.scheme.equalsIgnoreCase(HTTP_SCHEME)) {
response = httpsRequest.sendAsHttpRequest();
} else if (this.scheme.equalsIgnoreCase(UNIX_SCHEME)) {
String unixAddressPrefix = UNIX_SCHEME + "://";
String localUnixSocketPath = baseUri.substring(baseUri.indexOf(unixAddressPrefix) + unixAddressPrefix.length());
// Codes_SRS_HSMHTTPCLIENT_34_006: [If the scheme of the provided url is Unix, this function shall send the http request using unix domain sockets.]
response = sendHttpRequestUsingUnixSocket(httpsRequest, path, queryString, localUnixSocketPath);
} else {
throw new UnsupportedOperationException("unrecognized URI scheme. Only HTTPS, HTTP and UNIX are supported");
}
return response;
}
Aggregations