use of io.vertx.core.MultiMap in project vert.x by eclipse.
the class HttpTest method testParamDecoding.
private void testParamDecoding(String value) throws UnsupportedEncodingException {
server.requestHandler(req -> {
req.setExpectMultipart(true);
req.endHandler(v -> {
MultiMap formAttributes = req.formAttributes();
assertEquals(value, formAttributes.get("param"));
});
req.response().end();
});
String postData = "param=" + URLEncoder.encode(value, "UTF-8");
server.listen(onSuccess(server -> {
client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/").putHeader(HttpHeaders.CONTENT_TYPE, HttpHeaders.APPLICATION_X_WWW_FORM_URLENCODED).putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(postData.length())).handler(resp -> {
testComplete();
}).write(postData).end();
}));
await();
}
use of io.vertx.core.MultiMap in project vert.x by eclipse.
the class HttpTest method testRequestHeaders.
private void testRequestHeaders(boolean individually) {
MultiMap headers = getHeaders(10);
server.requestHandler(req -> {
assertTrue(headers.size() < req.headers().size());
for (Map.Entry<String, String> entry : headers) {
assertEquals(entry.getValue(), req.headers().get(entry.getKey()));
assertEquals(entry.getValue(), req.getHeader(entry.getKey()));
}
req.response().end();
});
server.listen(onSuccess(server -> {
HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> testComplete());
if (individually) {
for (Map.Entry<String, String> header : headers) {
req.headers().add(header.getKey(), header.getValue());
}
} else {
req.headers().setAll(headers);
}
req.end();
}));
await();
}
use of io.vertx.core.MultiMap in project vert.x by eclipse.
the class Http2ServerTest method testURI.
@Test
public void testURI() throws Exception {
server.requestHandler(req -> {
assertEquals("/some/path", req.path());
assertEquals("foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.query());
assertEquals("/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.uri());
assertEquals("http://whatever.com/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.absoluteURI());
assertEquals("/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2", req.getHeader(":path"));
assertEquals("whatever.com", req.host());
MultiMap params = req.params();
Set<String> names = params.names();
assertEquals(2, names.size());
assertTrue(names.contains("foo"));
assertTrue(names.contains("bar"));
assertEquals("foo_value", params.get("foo"));
assertEquals(Collections.singletonList("foo_value"), params.getAll("foo"));
assertEquals("bar_value_2", params.get("bar"));
assertEquals(Arrays.asList("bar_value_1", "bar_value_2"), params.getAll("bar"));
testComplete();
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
Http2Headers headers = new DefaultHttp2Headers().method("GET").scheme("http").authority("whatever.com").path("/some/path?foo=foo_value&bar=bar_value_1&bar=bar_value_2");
request.encoder.writeHeaders(request.context, id, headers, 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of io.vertx.core.MultiMap in project vert.x by eclipse.
the class HttpTest method testFormUploadAttributes.
@Test
public void testFormUploadAttributes() throws Exception {
AtomicInteger attributeCount = new AtomicInteger();
server.requestHandler(req -> {
if (req.method() == HttpMethod.POST) {
assertEquals(req.path(), "/form");
req.response().setChunked(true);
req.setExpectMultipart(true);
req.uploadHandler(upload -> upload.handler(buffer -> {
fail("Should get here");
}));
req.endHandler(v -> {
MultiMap attrs = req.formAttributes();
attributeCount.set(attrs.size());
assertEquals("vert x", attrs.get("framework"));
assertEquals("vert x", req.getFormAttribute("framework"));
assertEquals("jvm", attrs.get("runson"));
assertEquals("jvm", req.getFormAttribute("runson"));
req.response().end();
});
}
});
server.listen(onSuccess(s -> {
HttpClientRequest req = client.request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/form", resp -> {
assertEquals(200, resp.statusCode());
resp.bodyHandler(body -> {
assertEquals(0, body.length());
});
assertEquals(2, attributeCount.get());
testComplete();
});
try {
Buffer buffer = Buffer.buffer();
buffer.appendString("framework=" + URLEncoder.encode("vert x", "UTF-8") + "&runson=jvm", "UTF-8");
req.headers().set("content-length", String.valueOf(buffer.length()));
req.headers().set("content-type", "application/x-www-form-urlencoded");
req.write(buffer).end();
} catch (UnsupportedEncodingException e) {
fail(e.getMessage());
}
}));
await();
}
use of io.vertx.core.MultiMap in project vert.x by eclipse.
the class HttpTest method testResponseTrailers.
private void testResponseTrailers(boolean individually) {
MultiMap trailers = getHeaders(10);
server.requestHandler(req -> {
req.response().setChunked(true);
if (individually) {
for (Map.Entry<String, String> header : trailers) {
req.response().trailers().add(header.getKey(), header.getValue());
}
} else {
req.response().trailers().setAll(trailers);
}
req.response().end();
});
server.listen(onSuccess(s -> {
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
resp.endHandler(v -> {
assertEquals(trailers.size(), resp.trailers().size());
for (Map.Entry<String, String> entry : trailers) {
assertEquals(entry.getValue(), resp.trailers().get(entry.getKey()));
assertEquals(entry.getValue(), resp.getTrailer(entry.getKey()));
}
testComplete();
});
}).end();
}));
await();
}
Aggregations