use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class StaticContentHandlerTest method handleValid.
@Test
void handleValid() {
ServerRequest request = mockRequestWithPath("/foo/some.txt");
ServerResponse response = mock(ServerResponse.class);
TestContentHandler handler = TestContentHandler.create(true);
handler.handle(Http.Method.GET, request, response);
verify(request, never()).next();
assertThat(handler.path, is(Paths.get("foo/some.txt").toAbsolutePath().normalize()));
}
use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class StaticContentHandlerTest method handleNextOnFalse.
@Test
void handleNextOnFalse() {
ServerRequest request = mockRequestWithPath("/");
ServerResponse response = mock(ServerResponse.class);
TestContentHandler handler = TestContentHandler.create(false);
handler.handle(Http.Method.GET, request, response);
verify(request).next();
assertThat(handler.counter.get(), is(1));
}
use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class AccessLogSupportTest method testCustomFormat.
@Test
void testCustomFormat() {
AccessLogSupport accessLog = AccessLogSupport.builder().add(TimestampLogEntry.builder().formatter(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")).build()).add(HeaderLogEntry.create("Referer")).build();
ServerRequest request = mock(ServerRequest.class);
Context context = Context.create();
when(request.remoteAddress()).thenReturn(REMOTE_IP);
when(request.context()).thenReturn(context);
when(request.method()).thenReturn(Http.Method.PUT);
HttpRequest.Path path = mock(HttpRequest.Path.class);
when(path.toRawString()).thenReturn(PATH);
when(request.path()).thenReturn(path);
when(request.version()).thenReturn(Http.Version.V1_1);
RequestHeaders headers = mock(RequestHeaders.class);
when(headers.all("Referer")).thenReturn(Arrays.asList("first", "second"));
when(request.headers()).thenReturn(headers);
ServerResponse response = mock(ServerResponse.class);
when(response.status()).thenReturn(Http.Status.I_AM_A_TEAPOT);
String logRecord = accessLog.createLogRecord(request, response, BEGIN_TIME, 0L, END_TIME, TIME_TAKEN_MICROS * 1000);
String expected = "20071203101530 \"first,second\"";
assertThat(logRecord, is(expected));
}
use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class AccessLogSupportTest method testCommonFormat.
@Test
void testCommonFormat() {
AccessLogSupport accessLog = AccessLogSupport.builder().commonLogFormat().build();
ServerRequest request = mock(ServerRequest.class);
Context context = Context.create();
when(request.remoteAddress()).thenReturn(REMOTE_IP);
when(request.context()).thenReturn(context);
when(request.method()).thenReturn(Http.Method.PUT);
HttpRequest.Path path = mock(HttpRequest.Path.class);
when(path.toRawString()).thenReturn(PATH);
when(request.path()).thenReturn(path);
when(request.version()).thenReturn(Http.Version.V1_1);
ServerResponse response = mock(ServerResponse.class);
when(response.status()).thenReturn(Http.Status.I_AM_A_TEAPOT);
AccessLogContext accessLogContext = mock(AccessLogContext.class);
when(accessLogContext.requestDateTime()).thenReturn(BEGIN_TIME);
String expectedTimestamp = TimestampLogEntry.create().doApply(accessLogContext);
String logRecord = accessLog.createLogRecord(request, response, BEGIN_TIME, 0L, END_TIME, TIME_TAKEN_MICROS * 1000);
// 192.168.0.104 - [18/Jun/2019:23:10:44 +0200] "GET /greet/test HTTP/1.1" 200 55 2248
String expected = REMOTE_IP + " - - " + expectedTimestamp + " \"" + METHOD + " " + PATH + " " + HTTP_VERSION + "\" " + STATUS_CODE + " " + CONTENT_LENGTH;
assertThat(logRecord, is(expected));
}
use of io.helidon.webserver.ServerRequest in project helidon by oracle.
the class AccessLogSupport method handle.
private void handle(ServerRequest req, ServerResponse res) {
// Check if this path should be excluded from access log
if (excludePaths.size() > 0) {
for (PathMatcher matcher : excludePaths) {
PathMatcher.Result r = matcher.match(req.path().toString());
if (r.matches()) {
req.next();
return;
}
}
}
ZonedDateTime now = ZonedDateTime.now(clock);
long nanoNow = System.nanoTime();
logFormat.forEach(entry -> entry.accept(req, res));
res.whenSent().thenAccept(aResponse -> log(req, aResponse, now, nanoNow)).exceptionally(throwable -> {
log(req, res, now, nanoNow);
return null;
});
req.next();
}
Aggregations