use of java.util.Base64.Encoder in project vert.x by eclipse.
the class Http2ServerTest method testServerClose.
@Test
public void testServerClose() throws Exception {
waitFor(2);
AtomicInteger status = new AtomicInteger();
Handler<HttpServerRequest> requestHandler = req -> {
HttpConnection conn = req.connection();
conn.shutdownHandler(v -> {
assertEquals(0, status.getAndIncrement());
});
conn.closeHandler(v -> {
assertEquals(1, status.getAndIncrement());
complete();
});
conn.close();
};
server.requestHandler(requestHandler);
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
request.channel.closeFuture().addListener(v1 -> {
vertx.runOnContext(v2 -> {
complete();
});
});
request.decoder.frameListener(new Http2EventAdapter() {
@Override
public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData) throws Http2Exception {
vertx.runOnContext(v -> {
assertEquals(0, errorCode);
});
}
});
Http2ConnectionEncoder encoder = request.encoder;
int id = request.nextStreamId();
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of java.util.Base64.Encoder in project vert.x by eclipse.
the class Http2ServerTest method testPushPromise.
private void testPushPromise(Http2Headers requestHeaders, BiConsumer<HttpServerResponse, Handler<AsyncResult<HttpServerResponse>>> pusher, Consumer<Http2Headers> headerChecker) throws Exception {
Context ctx = vertx.getOrCreateContext();
server.requestHandler(req -> {
Handler<AsyncResult<HttpServerResponse>> handler = ar -> {
assertSame(ctx, Vertx.currentContext());
assertTrue(ar.succeeded());
HttpServerResponse response = ar.result();
response.end("the_content");
assertIllegalStateException(() -> response.push(HttpMethod.GET, "/wibble2", resp -> {
}));
};
pusher.accept(req.response(), handler);
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, requestHeaders, 0, true, request.context.newPromise());
Map<Integer, Http2Headers> pushed = new HashMap<>();
request.decoder.frameListener(new Http2FrameAdapter() {
@Override
public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
pushed.put(promisedStreamId, headers);
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
int delta = super.onDataRead(ctx, streamId, data, padding, endOfStream);
String content = data.toString(StandardCharsets.UTF_8);
vertx.runOnContext(v -> {
assertEquals(Collections.singleton(streamId), pushed.keySet());
assertEquals("the_content", content);
Http2Headers pushedHeaders = pushed.get(streamId);
headerChecker.accept(pushedHeaders);
testComplete();
});
return delta;
}
});
});
fut.sync();
await();
}
use of java.util.Base64.Encoder in project vert.x by eclipse.
the class Http2ServerTest method testClientSendGoAwayNoError.
@Test
public void testClientSendGoAwayNoError() throws Exception {
Future<Void> abc = Future.future();
Context ctx = vertx.getOrCreateContext();
Handler<HttpServerRequest> requestHandler = req -> {
HttpConnection conn = req.connection();
AtomicInteger numShutdown = new AtomicInteger();
AtomicBoolean completed = new AtomicBoolean();
conn.shutdownHandler(v -> {
assertOnIOContext(ctx);
numShutdown.getAndIncrement();
vertx.setTimer(100, timerID -> {
completed.set(true);
testComplete();
});
});
conn.goAwayHandler(ga -> {
assertOnIOContext(ctx);
assertEquals(0, numShutdown.get());
req.response().end();
});
conn.closeHandler(v -> {
assertTrue(completed.get());
});
abc.complete();
};
server.requestHandler(requestHandler);
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
Http2ConnectionEncoder encoder = request.encoder;
int id = request.nextStreamId();
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
abc.setHandler(ar -> {
encoder.writeGoAway(request.context, id, 0, Unpooled.EMPTY_BUFFER, request.context.newPromise());
request.context.flush();
});
});
fut.sync();
await();
}
use of java.util.Base64.Encoder in project j2objc by google.
the class Base64Test method testEncoder_encodeByteBuffer.
public void testEncoder_encodeByteBuffer() {
Encoder encoder = Base64.getEncoder();
byte[] emptyByteArray = new byte[0];
ByteBuffer emptyByteBuffer = ByteBuffer.wrap(emptyByteArray);
ByteBuffer emptyEncodedBuffer = encoder.encode(emptyByteBuffer);
assertEquals(emptyByteBuffer, emptyEncodedBuffer);
assertNotSame(emptyByteArray, emptyEncodedBuffer);
// Test the two types of byte buffer.
byte[] input = "abcefghi".getBytes(US_ASCII);
String expectedString = "YWJjZWZnaGk=";
byte[] expectedBytes = expectedString.getBytes(US_ASCII);
ByteBuffer inputBuffer = ByteBuffer.allocate(input.length);
inputBuffer.put(input);
inputBuffer.position(0);
testEncoder_encodeByteBuffer(encoder, inputBuffer, expectedBytes);
inputBuffer = ByteBuffer.allocateDirect(input.length);
inputBuffer.put(input);
inputBuffer.position(0);
testEncoder_encodeByteBuffer(encoder, inputBuffer, expectedBytes);
}
use of java.util.Base64.Encoder in project j2objc by google.
the class Base64Test method testEncoder_encodeArrayToArray.
/**
* Tests {@link Decoder#decode(byte[], byte[])} for correctness.
*/
public void testEncoder_encodeArrayToArray() {
Encoder encoder = Base64.getEncoder();
// Empty input
assertEquals(0, encoder.encode(new byte[0], new byte[0]));
// Test data for non-empty input
byte[] input = "abcefghi".getBytes(US_ASCII);
String expectedString = "YWJjZWZnaGk=";
byte[] encodedBytes = expectedString.getBytes(US_ASCII);
// Non-empty input: output array too short
byte[] tooShort = new byte[encodedBytes.length - 1];
try {
encoder.encode(input, tooShort);
fail();
} catch (IllegalArgumentException expected) {
}
// Non-empty input: output array longer than required
byte[] tooLong = new byte[encodedBytes.length + 1];
int tooLongBytesEncoded = encoder.encode(input, tooLong);
assertEquals(encodedBytes.length, tooLongBytesEncoded);
assertEquals(0, tooLong[tooLong.length - 1]);
assertArrayPrefixEquals(tooLong, encodedBytes.length, encodedBytes);
// Non-empty input: output array has exact minimum required size
byte[] justRight = new byte[encodedBytes.length];
int justRightBytesEncoded = encoder.encode(input, justRight);
assertEquals(encodedBytes.length, justRightBytesEncoded);
assertArrayEquals(encodedBytes, justRight);
}
Aggregations