use of com.sun.net.httpserver.HttpExchange in project incubator-heron by apache.
the class NetworkUtilsTest method testSendHttpResponse.
@Test
public void testSendHttpResponse() throws Exception {
HttpExchange exchange = Mockito.mock(HttpExchange.class);
Mockito.doNothing().when(exchange).sendResponseHeaders(Matchers.anyInt(), Matchers.anyLong());
OutputStream os = Mockito.mock(OutputStream.class);
Mockito.doReturn(os).when(exchange).getResponseBody();
Mockito.doNothing().when(os).write(Matchers.any(byte[].class));
Mockito.doNothing().when(os).close();
Assert.assertTrue(NetworkUtils.sendHttpResponse(exchange, new byte[0]));
Mockito.verify(exchange).getResponseBody();
Mockito.verify(os, Mockito.atLeastOnce()).write(Matchers.any(byte[].class));
Mockito.verify(os, Mockito.atLeastOnce()).close();
}
use of com.sun.net.httpserver.HttpExchange in project incubator-heron by apache.
the class NetworkUtilsTest method testReadHttpRequestBody.
@Test
public void testReadHttpRequestBody() throws Exception {
byte[] expectedBytes = "TO READ".getBytes();
InputStream is = Mockito.spy(new ByteArrayInputStream(expectedBytes));
HttpExchange exchange = Mockito.mock(HttpExchange.class);
Headers headers = Mockito.mock(Headers.class);
Mockito.doReturn(Integer.toString(expectedBytes.length)).when(headers).getFirst(Matchers.anyString());
Mockito.doReturn(headers).when(exchange).getRequestHeaders();
Mockito.doReturn(is).when(exchange).getRequestBody();
Assert.assertArrayEquals(expectedBytes, NetworkUtils.readHttpRequestBody(exchange));
Mockito.verify(is, Mockito.atLeastOnce()).close();
}
use of com.sun.net.httpserver.HttpExchange in project tomee by apache.
the class HttpConnectionTest method init.
@Before
public void init() throws Exception {
server = HttpServer.create(new InetSocketAddress(0), 5);
server.createContext("/e", new HttpHandler() {
@Override
public void handle(final HttpExchange exchange) throws IOException {
exchange.getResponseHeaders().set("Content-Type", "text/plain");
exchange.sendResponseHeaders(200, 0);
final OutputStream responseBody = exchange.getResponseBody();
responseBody.write("secure page".getBytes());
final String query = exchange.getRequestURI().getQuery();
if (query != null) {
responseBody.write(query.getBytes());
}
final String authorization = exchange.getRequestHeaders().getFirst("Authorization");
if (authorization != null) {
responseBody.write(authorization.getBytes("UTF-8"));
}
final String authorization2 = exchange.getRequestHeaders().getFirst("AltAuthorization");
if (authorization2 != null) {
responseBody.write(("alt" + authorization2).getBytes("UTF-8"));
}
responseBody.close();
}
});
server.start();
}
use of com.sun.net.httpserver.HttpExchange in project heron by twitter.
the class NetworkUtilsTest method testReadHttpRequestBodyFail.
@Test
public void testReadHttpRequestBodyFail() throws Exception {
HttpExchange exchange = Mockito.mock(HttpExchange.class);
Headers headers = Mockito.mock(Headers.class);
Mockito.doReturn(headers).when(exchange).getRequestHeaders();
Mockito.doReturn("-1").when(headers).getFirst(Matchers.anyString());
Assert.assertArrayEquals(new byte[0], NetworkUtils.readHttpRequestBody(exchange));
Mockito.doReturn("10").when(headers).getFirst(Matchers.anyString());
InputStream inputStream = Mockito.mock(InputStream.class);
Mockito.doReturn(inputStream).when(exchange).getRequestBody();
Mockito.doThrow(new IOException("Designed IO exception for testing")).when(inputStream).read(Matchers.any(byte[].class), Matchers.anyInt(), Matchers.anyInt());
Assert.assertArrayEquals(new byte[0], NetworkUtils.readHttpRequestBody(exchange));
Mockito.verify(inputStream, Mockito.atLeastOnce()).close();
}
use of com.sun.net.httpserver.HttpExchange in project heron by twitter.
the class NetworkUtilsTest method testReadHttpRequestBody.
@Test
public void testReadHttpRequestBody() throws Exception {
byte[] expectedBytes = "TO READ".getBytes();
InputStream is = Mockito.spy(new ByteArrayInputStream(expectedBytes));
HttpExchange exchange = Mockito.mock(HttpExchange.class);
Headers headers = Mockito.mock(Headers.class);
Mockito.doReturn(Integer.toString(expectedBytes.length)).when(headers).getFirst(Matchers.anyString());
Mockito.doReturn(headers).when(exchange).getRequestHeaders();
Mockito.doReturn(is).when(exchange).getRequestBody();
Assert.assertArrayEquals(expectedBytes, NetworkUtils.readHttpRequestBody(exchange));
Mockito.verify(is, Mockito.atLeastOnce()).close();
}
Aggregations