SimpleHttpServer implements Closeable { public void addHandler(Pattern uriPattern, RequestHandler requestHandler) { LOGGER.debug("Registering pattern {}", uriPattern.pattern()); synchronized (requestHandles) { requestHandles.add(new RequestHandle(uriPattern, requestHandler)); } } SimpleHttpServer(SocketAddress localAddress);  SimpleHttpServer(SocketAddress localAddress, NioEventLoopGroup parentGroup, NioEventLoopGroup childGroup, Executor executor); @Override void close(); void addHandler(Pattern uriPattern, RequestHandler requestHandler);  }
@Test public void simpleTextServer() throws Exception { final String responseBody = "This is a simple HTTP test."; final String contentType = "text/plain"; final HttpResponseStatus responseStatus = HttpResponseStatus.OK; try (SimpleHttpServer server = new SimpleHttpServer(new InetSocketAddress(PORT))) { server.addHandler(Pattern.compile("/"), new RequestHandler() { @Override public HttpResponse handleRequest(HttpRequest request, Matcher uriMatcher, ByteBuf body) throws RequestException { final ByteBuf buffer = Unpooled.copiedBuffer(responseBody, CharsetUtil.UTF_8); final DefaultHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, responseStatus, buffer); response.headers().add(HttpHeaders.Names.CONTENT_TYPE, contentType); response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, buffer.readableBytes()); return response; } }); final URL url = new URL("http: final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); Assert.assertEquals(connection.getResponseCode(), responseStatus.code()); Assert.assertEquals(connection.getContentType(), contentType); final InputStream in = (InputStream) connection.getContent(); final String content = new BufferedReader(new InputStreamReader(in)).readLine(); Assert.assertEquals(content, responseBody); } }
@Test(expectedExceptions = IOException.class) public void handlerThrowsException() throws Exception { try (SimpleHttpServer server = new SimpleHttpServer(new InetSocketAddress(PORT))) { server.addHandler(Pattern.compile("/"), new RequestHandler() { @Override public HttpResponse handleRequest(HttpRequest request, Matcher uriMatcher, ByteBuf body) throws RequestException { throw new RuntimeException("This should result in a 500 response."); } }); final URL url = new URL("http: url.getContent(); } }
@Test(expectedExceptions = IOException.class) public void handlerThrowsRequestException() throws Exception { try (SimpleHttpServer server = new SimpleHttpServer(new InetSocketAddress(PORT))) { server.addHandler(Pattern.compile("/"), new RequestHandler() { @Override public HttpResponse handleRequest(HttpRequest request, Matcher uriMatcher, ByteBuf body) throws RequestException { throw new RequestException(HttpResponseStatus.FORBIDDEN); } }); final URL url = new URL("http: url.getContent(); } }
@Test public void jsonResponse() throws Exception { final String json = "{}"; try (SimpleHttpServer server = new SimpleHttpServer(new InetSocketAddress(PORT))) { server.addHandler(Pattern.compile("/"), new JsonTextResponseRequestHandler() { @Override public String handle(HttpRequest request, Matcher uriMatcher, ByteBuf body) throws RequestException { return json; } }); final URL url = new URL("http: final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); Assert.assertTrue(connection.getContentType().startsWith("application/json")); final InputStream in = (InputStream) connection.getContent(); final String content = new BufferedReader(new InputStreamReader(in)).readLine(); Assert.assertEquals(content, json); } }