use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method readInputCompletelyReadsInputStream.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_014: [The function shall read from the input stream (response stream) and return the response.]
@Test
public void readInputCompletelyReadsInputStream(@Mocked final InputStream mockIs) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
byte[] expectedResponse = { 1, 2, 3 };
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);
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
conn.connect();
// Act
byte[] testResponse = conn.readInput();
// Assert
assertThat(testResponse, is(expectedResponse));
}
use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method constructorThrowsIoExceptionIfCannotOpenConnection.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_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 mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method setRequestHeaderSetsRequestHeader.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_010: [The function shall set the given request header field.]
@Test
public void setRequestHeaderSetsRequestHeader() throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.POST;
final String field = "test-field";
final String value = "test-value";
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
// Act
conn.setRequestHeader(field, value);
// Assert
new Verifications() {
{
mockUrl.openConnection().setRequestProperty(field, value);
}
};
}
use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method readInputClosesInputStream.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_016: [The function shall close the input stream after it has been completely read.]
@Test
public void readInputClosesInputStream(@Mocked final InputStream mockIs) throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
mockUrlConn.getInputStream();
result = mockIs;
mockIs.read();
result = -1;
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
conn.connect();
// Act
conn.readInput();
// Assert
new Verifications() {
{
mockIs.close();
}
};
}
use of mockit.NonStrictExpectations in project azure-iot-sdk-java by Azure.
the class HttpConnectionTest method writeOutputFailsWhenMethodIsNotPostOrPut.
// Tests_SRS_SERVICE_SDK_JAVA_HTTPSCONNECTION_12_012: [The function shall save the body to be sent with the request.]
// Assert
@Test(expected = IllegalArgumentException.class)
public void writeOutputFailsWhenMethodIsNotPostOrPut() throws IOException {
// Arrange
final HttpMethod httpsMethod = HttpMethod.GET;
final byte[] body = { 1, 2 };
new NonStrictExpectations() {
{
mockUrl.getProtocol();
result = "https";
mockUrl.openConnection();
result = mockUrlConn;
mockUrlConn.getRequestMethod();
result = httpsMethod.name();
}
};
HttpConnection conn = new HttpConnection(mockUrl, httpsMethod);
// Act
conn.writeOutput(body);
}
Aggregations