use of io.vertx.core.http.HttpServerRequest in project java-chassis by ServiceComb.
the class TestRestVertxHttpRequest method testGetQueryParam.
@Test
public void testGetQueryParam() {
boolean status = true;
try {
HttpServerRequest httpServerRequest = Mockito.mock(HttpServerRequest.class);
Deencapsulation.setField(instance, "request", httpServerRequest);
MultiMap multiMap = Mockito.mock(MultiMap.class);
Mockito.when(httpServerRequest.params()).thenReturn(multiMap);
List<String> stringList = new ArrayList<String>();
stringList.add("sters");
Mockito.when(multiMap.getAll("key")).thenReturn(stringList);
String[] str = instance.getQueryParam("key");
Assert.assertEquals("sters", str[0]);
} catch (Exception ex) {
status = false;
}
Assert.assertTrue(status);
}
use of io.vertx.core.http.HttpServerRequest in project java-chassis by ServiceComb.
the class TestRestVertxHttpRequest method testGetHeaderParam.
@Test
public void testGetHeaderParam() {
boolean status = false;
try {
HttpServerRequest httpServerRequest = Mockito.mock(HttpServerRequest.class);
Deencapsulation.setField(instance, "request", httpServerRequest);
MultiMap multiMap = Mockito.mock(MultiMap.class);
Mockito.when(httpServerRequest.headers()).thenReturn(multiMap);
@SuppressWarnings({ "unchecked" }) Iterator<Entry<String, String>> iterator = Mockito.mock(Iterator.class);
Mockito.when(multiMap.iterator()).thenReturn(iterator);
Mockito.when(iterator.hasNext()).thenReturn(true).thenReturn(false);
Assert.assertNotNull(instance.getHeaderParam("key"));
} catch (Exception ex) {
status = true;
}
Assert.assertTrue(status);
}
use of io.vertx.core.http.HttpServerRequest in project java-chassis by ServiceComb.
the class TestVertxToServletMockRequest method testGetLocalAddr.
@Test
public void testGetLocalAddr() {
try {
init();
HttpServerRequest httpServerRequest = Mockito.mock(HttpServerRequest.class);
Deencapsulation.setField(instance, "vertxRequest", httpServerRequest);
SocketAddress socketAddress = Mockito.mock(SocketAddress.class);
Mockito.when(httpServerRequest.localAddress()).thenReturn(socketAddress);
Mockito.when(socketAddress.host()).thenReturn("localhost");
Assert.assertEquals("localhost", instance.getLocalAddr());
} catch (Exception e) {
Assert.assertNotNull(e);
} catch (Error e) {
Assert.assertNotNull(e);
}
}
use of io.vertx.core.http.HttpServerRequest in project java-chassis by ServiceComb.
the class TestVertxToServletMockRequest method testGetRemoteHost.
@Test
public void testGetRemoteHost() {
try {
init();
HttpServerRequest httpServerRequest = Mockito.mock(HttpServerRequest.class);
Deencapsulation.setField(instance, "vertxRequest", httpServerRequest);
SocketAddress socketAddress = Mockito.mock(SocketAddress.class);
Mockito.when(httpServerRequest.remoteAddress()).thenReturn(socketAddress);
Mockito.when(socketAddress.host()).thenReturn("localhost");
Assert.assertEquals("localhost", instance.getRemoteHost());
} catch (Exception e) {
Assert.assertNotNull(e);
} catch (Error e) {
Assert.assertNotNull(e);
}
}
use of io.vertx.core.http.HttpServerRequest in project vert.x by eclipse.
the class Http2ServerTest method testStreamWritability.
private void testStreamWritability(Function<HttpServerRequest, WriteStream<Buffer>> streamProvider) throws Exception {
Context ctx = vertx.getOrCreateContext();
String content = TestUtils.randomAlphaString(1024);
StringBuilder expected = new StringBuilder();
Future<Void> whenFull = Future.future();
AtomicBoolean drain = new AtomicBoolean();
server.requestHandler(req -> {
WriteStream<Buffer> stream = streamProvider.apply(req);
vertx.setPeriodic(1, timerID -> {
if (stream.writeQueueFull()) {
stream.drainHandler(v -> {
assertOnIOContext(ctx);
expected.append("last");
stream.end(Buffer.buffer("last"));
});
vertx.cancelTimer(timerID);
drain.set(true);
whenFull.complete();
} else {
expected.append(content);
Buffer buf = Buffer.buffer(content);
stream.write(buf);
}
});
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
AtomicInteger toAck = new AtomicInteger();
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.decoder.frameListener(new Http2FrameAdapter() {
StringBuilder received = new StringBuilder();
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
received.append(data.toString(StandardCharsets.UTF_8));
int delta = super.onDataRead(ctx, streamId, data, padding, endOfStream);
if (endOfStream) {
vertx.runOnContext(v -> {
assertEquals(expected.toString(), received.toString());
testComplete();
});
return delta;
} else {
if (drain.get()) {
return delta;
} else {
toAck.getAndAdd(delta);
return 0;
}
}
}
});
whenFull.setHandler(ar -> {
request.context.executor().execute(() -> {
try {
request.decoder.flowController().consumeBytes(request.connection.stream(id), toAck.intValue());
request.context.flush();
} catch (Http2Exception e) {
e.printStackTrace();
fail(e);
}
});
});
});
fut.sync();
await();
}
Aggregations