use of org.jboss.netty.handler.codec.http.HttpResponse in project http-kit by http-kit.
the class WebSocketClientHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Channel ch = ctx.getChannel();
if (!handshaker.isHandshakeComplete()) {
handshaker.finishHandshake(ch, (HttpResponse) e.getMessage());
latch.countDown();
return;
}
if (e.getMessage() instanceof HttpResponse) {
HttpResponse response = (HttpResponse) e.getMessage();
throw new Exception("Unexpected HttpResponse (status=" + response.getStatus() + ", content=" + response.getContent().toString(CharsetUtil.UTF_8) + ')');
}
WebSocketFrame frame = (WebSocketFrame) e.getMessage();
if (frame != null)
queue.offer(frame);
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class DummyHttpRequestHandler method testWriteTwoChunks.
@Test
public void testWriteTwoChunks() {
LOG.info("Start: Testing headers with one chunk");
setupClient();
String chunk1 = "hello";
String chunk2 = "bye";
ArrayList<byte[]> chunks = new ArrayList<byte[]>();
chunks.add(chunk1.getBytes(Charset.defaultCharset()));
chunks.add(chunk2.getBytes(Charset.defaultCharset()));
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("header1", "value1");
headers.put("header2", "value2");
HashMap<String, String> footers = new HashMap<String, String>();
footers.put("footer1", "1value");
footers.put("footer2", "2value");
setupServer(HttpResponseStatus.OK, chunks, headers, footers);
ChannelFuture connectFuture = _clientBootstrap.connect(_serverAddress);
connectFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
assertTrue("connect succeeded", connectFuture.isSuccess());
HttpRequest request = new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/test");
Channel requestChannel = connectFuture.getChannel();
ChannelFuture writeFuture = requestChannel.write(request);
writeFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
assertTrue("connect succeeded", writeFuture.isSuccess());
HttpResponse response = _responseHandler.getResponse();
assertEquals("response code", Integer.toString(HttpResponseStatus.OK.getCode()), response.getHeader(ChunkedBodyWritableByteChannel.RESPONSE_CODE_FOOTER_NAME));
assertEquals("Checking header1 value", "value1", response.getHeader("header1"));
assertEquals("Checking header2 value", "value2", response.getHeader("header2"));
assertEquals("Checking footer1 value", "1value", response.getHeader("footer1"));
assertEquals("Checking footer2 value", "2value", response.getHeader("footer2"));
byte[] responseBody = _responseHandler.getReceivedBytes();
assertEquals("response length", chunk1.getBytes(Charset.defaultCharset()).length + chunk2.getBytes(Charset.defaultCharset()).length, responseBody.length);
byte[] fullBody = new byte[chunk1.getBytes(Charset.defaultCharset()).length + chunk2.getBytes(Charset.defaultCharset()).length];
System.arraycopy(chunk1.getBytes(Charset.defaultCharset()), 0, fullBody, 0, chunk1.getBytes(Charset.defaultCharset()).length);
System.arraycopy(chunk2.getBytes(Charset.defaultCharset()), 0, fullBody, chunk1.getBytes(Charset.defaultCharset()).length, chunk2.getBytes(Charset.defaultCharset()).length);
assertTrue("response content", Arrays.equals(fullBody, responseBody));
LOG.info("Done: Testing headers with one chunk");
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class DummyHttpRequestHandler method testWriteEmptyResponse.
@Test
public void testWriteEmptyResponse() {
LOG.info("Start: Testing empty response");
setupClient();
ArrayList<byte[]> chunks = new ArrayList<byte[]>();
HashMap<String, String> headers = new HashMap<String, String>();
HashMap<String, String> footers = new HashMap<String, String>();
setupServer(HttpResponseStatus.OK, chunks, headers, footers);
ChannelFuture connectFuture = _clientBootstrap.connect(_serverAddress);
connectFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
assertTrue("connect succeeded", connectFuture.isSuccess());
HttpRequest request = new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/test");
Channel requestChannel = connectFuture.getChannel();
ChannelFuture writeFuture = requestChannel.write(request);
writeFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
assertTrue("connect succeeded", writeFuture.isSuccess());
HttpResponse response = _responseHandler.getResponse();
assertEquals("response code", HttpResponseStatus.OK, response.getStatus());
byte[] responseBody = _responseHandler.getReceivedBytes();
assertTrue("empty response", null == responseBody || responseBody.length == 0);
LOG.info("Done: Testing empty response");
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class TestRelayCommandsLocal method prepareTestOneDataStreamCommand.
private void prepareTestOneDataStreamCommand() throws Exception {
//generate an event
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/genDataEvents/start?src_ids=100&fromScn=10&eventsPerSec=1&duration=10");
SimpleTestHttpClient httpClient = SimpleTestHttpClient.createLocal(TimeoutPolicy.ALL_TIMEOUTS);
SimpleHttpResponseHandler respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
String respString = new String(respHandler.getReceivedBytes());
LOG.debug("Response string:" + respString);
ByteArrayInputStream in = new ByteArrayInputStream(respHandler.getReceivedBytes());
ObjectMapper objMapper = new ObjectMapper();
Map<String, String> genRes = objMapper.readValue(in, new TypeReference<Map<String, String>>() {
});
HttpResponse respObj = respHandler.getResponse();
assertNull("/genDataEvents returned unexpected error", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
assertEquals("event-generation failed to start", "true", genRes.get("genDataEventsStarted"));
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
}
}
use of org.jboss.netty.handler.codec.http.HttpResponse in project databus by linkedin.
the class TestRelayCommandsLocal method testRegisterCommandThreeSources.
@Test
public void testRegisterCommandThreeSources() throws Exception {
LOG.debug("\n\nstarting testRegisterCommandThreeSources()\n");
// /register?sources=1,2,3
SimpleTestHttpClient httpClient = SimpleTestHttpClient.createLocal(TimeoutPolicy.ALL_TIMEOUTS);
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/register?sources=5001,5002,5003");
SimpleHttpResponseHandler respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
if (LOG.isDebugEnabled()) {
LOG.debug("/register response:" + new String(respHandler.getReceivedBytes()));
}
HttpResponse respObj = respHandler.getResponse();
// Note that v3 client code doesn't currently (May 2013) support "sources" param for "/register".
assertNotNull("exception class header not present", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
assertEquals("exception class name mismatch", InvalidRequestParamValueException.class.getName(), respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
}
Aggregations