/** * Starts the HTTP server. * * @return the listening port. */ static int start () throws IOException { server = ServerBootstrap.bootstrap ().registerHandler ("*", new HttpRequestHandler () { @Override public void handle (HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { response.setStatusCode (HttpStatus.SC_OK); response.setEntity (new StringEntity ("0123456789")); } }) .create (); server.start (); return server.getLocalPort (); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/content", new HttpRequestHandler() { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { String contentType = request.getFirstHeader(Exchange.CONTENT_TYPE).getValue(); assertEquals(CONTENT_TYPE, contentType); response.setEntity(new StringEntity(contentType, "ASCII")); response.setStatusCode(HttpStatus.SC_OK); } }).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { Map<String, String> expectedHeaders = new HashMap<String, String>(); expectedHeaders.put("Content-Type", "text/plain"); expectedHeaders.put("Content-Encoding", "gzip"); localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/", new HeaderValidationHandler("POST", null, getBody(), getExpectedContent(), expectedHeaders)).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/search", new BasicValidationHandler("GET", null, null, getExpectedContent())). registerHandler("/test%20/path", new BasicValidationHandler("GET", null, null, getExpectedContent())). registerHandler("/testWithQueryParams", new BasicValidationHandler("GET", "abc=123", null, getExpectedContent())). create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/test", new RedirectHandler(HttpStatus.SC_MOVED_PERMANENTLY)). registerHandler("/someplaceelse", new BasicValidationHandler("GET", null, null, "Bye World")). registerHandler("/test", new RedirectHandler(HttpStatus.SC_MOVED_PERMANENTLY)). create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { Map<String, String> expectedHeaders = new HashMap<>(); expectedHeaders.put("Proxy-Connection", "Keep-Alive"); proxy = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("*", new HeaderValidationHandler("GET", null, null, getExpectedContent(), expectedHeaders)).create(); proxy.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/", new HttpRequestHandler() { public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { try { Thread.sleep(1000); } catch (InterruptedException e) { // ignore } response.setStatusCode(HttpStatus.SC_OK); response.setEntity(new StringEntity("" + counter.incrementAndGet())); } }).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/myapp", new HttpRequestHandler() { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { String uri = request.getRequestLine().getUri(); assertEquals("/myapp?from=me&to=foo&to=bar", uri); response.setHeader("bar", "yes"); response.addHeader("foo", "123"); response.addHeader("foo", "456"); response.setEntity(new StringEntity("OK", "ASCII")); response.setStatusCode(HttpStatus.SC_OK); } }).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/myget", new BasicValidationHandler("GET", null, null, getExpectedContent())). registerHandler("/mypost", new BasicValidationHandler("POST", null, null, getExpectedContent())). registerHandler("/myget2", new BasicValidationHandler("GET", "q=Camel", null, getExpectedContent())). create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { Map<String, String> expectedHeaders = new HashMap<String, String>(); expectedHeaders.put("TestHeader", "test"); expectedHeaders.put("Accept-Language", "pl"); localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/", new MyHeaderValidationHandler("GET", "HTTP/1.0", getExpectedContent(), expectedHeaders)).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/get", new BasicValidationHandler("GET", null, null, getExpectedContent())). registerHandler("/patch", new BasicValidationHandler("PATCH", null, null, getExpectedContent())). registerHandler("/patch1", new BasicValidationHandler("PATCH", null, "rocks camel?", getExpectedContent())). registerHandler("/post", new BasicValidationHandler("POST", null, null, getExpectedContent())). registerHandler("/post1", new BasicValidationHandler("POST", null, null, getExpectedContent())). registerHandler("/put", new BasicValidationHandler("PUT", null, null, getExpectedContent())). registerHandler("/trace", new BasicValidationHandler("TRACE", null, null, getExpectedContent())). registerHandler("/options", new BasicValidationHandler("OPTIONS", null, null, getExpectedContent())). registerHandler("/delete", new BasicValidationHandler("DELETE", null, null, getExpectedContent())). registerHandler("/head", new BasicValidationHandler("HEAD", null, null, getExpectedContent())).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { Map<String, String> expectedHeaders = new HashMap<>(); expectedHeaders.put("User-Agent", "myCoolCamelCaseAgent"); localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("*", new HeaderValidationHandler("GET", null, null, getExpectedContent(), expectedHeaders)).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { Map<String, String> expectedHeaders = new HashMap<String, String>(); expectedHeaders.put("Content-Type", "image/jpeg"); localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/post", new BasicValidationHandler("POST", null, getBody(), getExpectedContent())). registerHandler("/post1", new HeaderValidationHandler("POST", null, null, getExpectedContent(), expectedHeaders)). create(); localServer.start(); super.setUp(); }
@Before public void setUp() throws Exception { final SocketConfig socketConfig = SocketConfig.custom() .setSoTimeout(15000) .build(); this.serverBootstrap = ServerBootstrap.bootstrap() .setSocketConfig(socketConfig) .setServerInfo(ORIGIN) .registerHandler("/echo/*", new EchoHandler()) .registerHandler("/random/*", new RandomHandler()); if (this.scheme.equals(ProtocolScheme.https)) { this.serverBootstrap.setSslContext(SSLTestContexts.createServerSSLContext()); } this.connManager = new PoolingHttpClientConnectionManager(); this.clientBuilder = HttpClientBuilder.create() .setDefaultSocketConfig(socketConfig) .setConnectionManager(this.connManager); }
@Test public void testBasicSSL() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLTestContexts.createClientSSLContext(), hostVerifier); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); try { final SSLSession sslsession = sslSocket.getSession(); Assert.assertNotNull(sslsession); Assert.assertTrue(hostVerifier.isFired()); } finally { sslSocket.close(); } }
@Test public void testBasicDefaultHostnameVerifier() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLTestContexts.createClientSSLContext(), SSLConnectionSocketFactory.getDefaultHostnameVerifier()); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); try { final SSLSession sslsession = sslSocket.getSession(); Assert.assertNotNull(sslsession); } finally { sslSocket.close(); } }
@Test public void testClientAuthSSL() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLTestContexts.createClientSSLContext(), hostVerifier); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); try { final SSLSession sslsession = sslSocket.getSession(); Assert.assertNotNull(sslsession); Assert.assertTrue(hostVerifier.isFired()); } finally { sslSocket.close(); } }
@Test(expected=SSLException.class) public void testSSLTrustVerification() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); // Use default SSL context final SSLContext defaultsslcontext = SSLContexts.createDefault(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(defaultsslcontext, NoopHostnameVerifier.INSTANCE); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); sslSocket.close(); }
private static HttpServer getDefaultServer(Path folderRoot) { return ServerBootstrap.bootstrap() .setListenerPort(PhotatoConfig.serverPort) .setServerInfo(serverName) .setSocketConfig(getSocketConfig()) .setExceptionLogger(new StdErrorExceptionLogger()) .registerHandler("*", new LoadingHandler(folderRoot)) .create(); }
@Override protected void before() throws Throwable { final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(5000).build(); serverBootstrap = ServerBootstrap.bootstrap().setSocketConfig(socketConfig).setServerInfo(ORIGIN); if(ProtocolScheme.https.equals(protocolScheme)) { serverBootstrap.setSslContext(SSLTestContexts.createServerSSLContext()); } registerHandlers(); server = serverBootstrap.create(); server.start(); host = new HttpHost("127.0.0.1", server.getLocalPort(), protocolScheme.name()); uri = URIUtils.rewriteURI(new URI("/"), host); }
public HttpCollector(Aggregator aggregator, HttpCollectorConfig config) { this.aggregator = aggregator; this.server = ServerBootstrap.bootstrap() .registerHandler(config.getUri(), new HttpCollectorHandler(this)) .setListenerPort(config.getPort()) .create(); this.work = false; }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/", new BasicValidationHandler("GET", null, null, getExpectedContent())). registerHandler("/query", new BasicRawQueryValidationHandler("GET", "x=%3B", null, getExpectedContent())).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/", new DelayValidationHandler("GET", null, null, getExpectedContent(), 1000)).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/search", new BasicValidationHandler("GET", null, null, getExpectedContent())).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/", new BasicValidationHandler("POST", null, getBody(), getExpectedContent())).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/myget", new BasicValidationHandler("GET", null, null, getExpectedContent())). create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/search", new AuthenticationValidationHandler("GET", null, null, getExpectedContent(), user, password)).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/mail/", new BasicValidationHandler("GET", null, null, getExpectedContent())).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/", new AuthenticationValidationHandler("GET", null, null, getExpectedContent(), user, password)).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/", new BasicValidationHandler("GET", null, null, getExpectedContent())).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/", new DelayValidationHandler("GET", null, null, getExpectedContent(), 2000)).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/hello", new HttpRequestHandler() { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { response.setStatusCode(HttpStatus.SC_OK); Object header = request.getFirstHeader(Exchange.FILE_NAME); assertNull("There should be no Camel header", header); for (Header h : request.getAllHeaders()) { if (h.getName().startsWith("Camel") || h.getName().startsWith("org.apache.camel")) { assertNull("There should be no Camel header", h); } } // set ar regular and Camel header response.setHeader("MyApp", "dude"); response.setHeader(Exchange.TO_ENDPOINT, "foo"); } }).create(); localServer.start(); super.setUp(); }
@Before @Override public void setUp() throws Exception { localServer = ServerBootstrap.bootstrap(). setHttpProcessor(getBasicHttpProcessor()). setConnectionReuseStrategy(getConnectionReuseStrategy()). setResponseFactory(getHttpResponseFactory()). setExpectationVerifier(getHttpExpectationVerifier()). setSslContext(getSSLContext()). registerHandler("/test/", new BasicValidationHandler("GET", null, null, getExpectedContent())). create(); localServer.start(); super.setUp(); }