public void request( final String method, final String url, final MultiMap headers, final Buffer body, final Handler<Response> handler) { try { final URI uri = new URI(url); final boolean urlEncoded = "application/x-www-form-urlencoded".equalsIgnoreCase(headers.get("content-type")); final Response response = new Response(vertx, handler); // start yoke fakeServer .requestHandler() .handle( new HttpServerRequest() { MultiMap params = null; MultiMap attributes = null; @Override public HttpVersion version() { return HttpVersion.HTTP_1_1; } @Override public String method() { return method.toUpperCase(); } @Override public String uri() { return uri.getPath() + "?" + uri.getQuery() + "#" + uri.getFragment(); } @Override public String path() { return uri.getPath(); } @Override public String query() { return uri.getQuery(); } @Override public HttpServerResponse response() { return response; } @Override public MultiMap headers() { return headers; } @Override public MultiMap params() { if (params == null) { QueryStringDecoder queryStringDecoder = new QueryStringDecoder(uri()); Map<String, List<String>> prms = queryStringDecoder.parameters(); params = new CaseInsensitiveMultiMap(); if (!prms.isEmpty()) { for (Map.Entry<String, List<String>> entry : prms.entrySet()) { params.add(entry.getKey(), entry.getValue()); } } } return params; } @Override public InetSocketAddress remoteAddress() { return new InetSocketAddress("127.0.0.1", 80); } @Override public X509Certificate[] peerCertificateChain() throws SSLPeerUnverifiedException { return null; } @Override public URI absoluteURI() { return uri; } @Override public HttpServerRequest bodyHandler(Handler<Buffer> bodyHandler) { bodyHandler.handle(body); return this; } @Override public HttpServerRequest dataHandler(Handler<Buffer> handler) { handler.handle(body); return this; } @Override public HttpServerRequest pause() { throw new UnsupportedOperationException("This mock does not support pause"); } @Override public HttpServerRequest resume() { throw new UnsupportedOperationException("This mock does not support resume"); } @Override public HttpServerRequest endHandler(Handler<Void> endHandler) { endHandler.handle(null); return this; } @Override public NetSocket netSocket() { throw new UnsupportedOperationException("This mock does not support netSocket"); } @Override public HttpServerRequest expectMultiPart(boolean expect) { // NOOP return this; } @Override public HttpServerRequest uploadHandler( Handler<HttpServerFileUpload> uploadHandler) { throw new UnsupportedOperationException( "This mock does not support uploadHandler"); } @Override public MultiMap formAttributes() { if (attributes == null) { attributes = new CaseInsensitiveMultiMap(); if (urlEncoded) { QueryStringDecoder queryStringDecoder = new QueryStringDecoder(body.toString(), false); Map<String, List<String>> prms = queryStringDecoder.parameters(); if (!prms.isEmpty()) { for (Map.Entry<String, List<String>> entry : prms.entrySet()) { attributes.add(entry.getKey(), entry.getValue()); } } } } return attributes; } @Override public HttpServerRequest exceptionHandler(Handler<Throwable> handler) { throw new UnsupportedOperationException( "This mock does not support exceptionHandler"); } }); } catch (URISyntaxException e) { throw new RuntimeException(e); } }
public YokeTester(Verticle verticle, boolean fakeSSL) { super(verticle); this.vertx = verticle.getVertx(); fakeServer.setSSL(fakeSSL); listen(fakeServer); }