use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod in project azure-iot-sdk-java by Azure.
the class HttpsRequestIT method sendHttpsRequestGetsCorrectResponse.
@Test
public void sendHttpsRequestGetsCorrectResponse() throws IOException {
URL url = new URL("https://fonts.googleapis.com/css?family=Inconsolata");
HttpsMethod method = HttpsMethod.GET;
byte[] body = new byte[0];
String encoding = "UTF-8";
HttpsRequest request = new HttpsRequest(url, method, body);
request.setHeaderField("accept-charset", encoding);
HttpsResponse response = request.send();
int testStatus = response.getStatus();
String testBody = new String(response.getBody(), encoding);
int expectedStatus = 200;
String expectedBodyPrefix = "@font-face {\n" + " font-family: 'Inconsolata';";
assertThat(testStatus, is(expectedStatus));
assertThat(testBody, is(startsWith(expectedBodyPrefix)));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod in project azure-iot-sdk-java by Azure.
the class HttpsConnectionTest method readInputCompletelyReadsInputStream.
// Tests_SRS_HTTPSCONNECTION_11_011: [The function shall read from the input stream (response stream) and return the response.]
@Test
public void readInputCompletelyReadsInputStream(@Mocked final InputStream mockIs) throws IOException {
final HttpsMethod httpsMethod = HttpsMethod.GET;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getInputStream();
result = mockIs;
mockIs.read();
returns(1, 2, 3, -1);
}
};
HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
conn.connect();
byte[] testResponse = conn.readInput();
byte[] expectedResponse = { 1, 2, 3 };
assertThat(testResponse, is(expectedResponse));
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod in project azure-iot-sdk-java by Azure.
the class HttpsConnectionTest method setRequestMethodRejectsNonPostOrPutIfHasBody.
// Tests_SRS_HTTPSCONNECTION_11_007: [The function shall throw an IllegalArgumentException if the request currently has a non-empty body and the new method is not a POST or a PUT.]
@Test(expected = IllegalArgumentException.class)
public void setRequestMethodRejectsNonPostOrPutIfHasBody() throws IOException {
final HttpsMethod httpsMethod = HttpsMethod.POST;
final HttpsMethod illegalHttpsMethod = HttpsMethod.DELETE;
final byte[] body = { 1, 2, 3 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
returns(httpsMethod.name(), illegalHttpsMethod.name());
}
};
HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
conn.writeOutput(body);
conn.setRequestMethod(illegalHttpsMethod);
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod in project azure-iot-sdk-java by Azure.
the class HttpsConnectionTest method setRequestMethodSetsRequestMethod.
// Tests_SRS_HTTPSCONNECTION_11_006: [The function shall set the request method.]
@Test
public void setRequestMethodSetsRequestMethod() throws IOException {
final HttpsMethod httpsMethod = HttpsMethod.PUT;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
}
};
HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
conn.setRequestMethod(httpsMethod);
new Verifications() {
{
((HttpsURLConnection) mockUrl.openConnection()).setRequestMethod(httpsMethod.name());
}
};
}
use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsMethod in project azure-iot-sdk-java by Azure.
the class HttpsConnectionTest method writeOutputFailsWhenMethodIsNotPostOrPut.
// Tests_SRS_HTTPSCONNECTION_11_010: [The function shall throw an IllegalArgumentException if the request does not currently use method POST or PUT and the body is non-empty.]
@Test(expected = IllegalArgumentException.class)
public void writeOutputFailsWhenMethodIsNotPostOrPut() throws IOException {
final HttpsMethod httpsMethod = HttpsMethod.GET;
final byte[] body = { 1, 2 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
}
};
HttpsConnection conn = new HttpsConnection(mockUrl, httpsMethod);
conn.writeOutput(body);
}
Aggregations