forked from line/armeria
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix where access log is not written when the method of a reques… (lin…
…e#2159) …t is not allowed Motivation: There's some cases that we don't call `RequestLogBuilder.endRequest()`. - When the method of the request is not allowed - When there's no services that match the path of the request - and so on Because the `RequestLog` in never complete, `AccessLogWriter` is not called so we should fix it. Modifications: - Call `RequestLogBuilder.endRequest()` even when `service.serve()` is not invoked - Do not log HttpStatusException as requestCause Result: - You can now see the access logs when the method of a request is not allowed and there are no services that match the path.
- Loading branch information
Showing
2 changed files
with
120 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
core/src/test/java/com/linecorp/armeria/server/HttpServerHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright 2019 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.server; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.linecorp.armeria.client.HttpClient; | ||
import com.linecorp.armeria.common.AggregatedHttpResponse; | ||
import com.linecorp.armeria.common.HttpResponse; | ||
import com.linecorp.armeria.common.HttpStatus; | ||
import com.linecorp.armeria.common.logging.RequestLog; | ||
import com.linecorp.armeria.testing.junit.server.ServerExtension; | ||
|
||
class HttpServerHandlerTest { | ||
|
||
private static final AtomicReference<RequestLog> logHolder = new AtomicReference<>(); | ||
|
||
@RegisterExtension | ||
static final ServerExtension server = new ServerExtension() { | ||
@Override | ||
protected void configure(ServerBuilder sb) throws Exception { | ||
sb.accessLogWriter(logHolder::set, true); | ||
sb.route() | ||
.get("/hello") | ||
.build((ctx, req) -> HttpResponse.of(200)); | ||
sb.route() | ||
.get("/httpStatusException") | ||
.build((ctx, req) -> { | ||
throw HttpStatusException.of(201); | ||
}); | ||
sb.route() | ||
.get("/httpResponseException") | ||
.build((ctx, req) -> { | ||
throw HttpResponseException.of(201); | ||
}); | ||
} | ||
}; | ||
|
||
@BeforeEach | ||
void clearLog() { | ||
logHolder.set(null); | ||
} | ||
|
||
@Test | ||
void methodNotAllowed() { | ||
final HttpClient client = HttpClient.of(server.uri("/")); | ||
final AggregatedHttpResponse res = client.delete("/hello").aggregate().join(); | ||
assertThat(res.status()).isSameAs(HttpStatus.METHOD_NOT_ALLOWED); | ||
await().untilAsserted(() -> assertThat(logHolder.get().path()).isEqualTo("/hello")); | ||
assertThat(logHolder.get().requestCause()).isNull(); | ||
} | ||
|
||
@Test | ||
void handleNonExistentMapping() { | ||
final HttpClient client = HttpClient.of(server.uri("/")); | ||
final AggregatedHttpResponse res = client.get("/non_existent").aggregate().join(); | ||
assertThat(res.status()).isSameAs(HttpStatus.NOT_FOUND); | ||
await().untilAsserted(() -> assertThat(logHolder.get().path()).isEqualTo("/non_existent")); | ||
assertThat(logHolder.get().requestCause()).isNull(); | ||
} | ||
|
||
@Test | ||
void httpStatusExceptionIsNotLoggedAsRequestCause() { | ||
final HttpClient client = HttpClient.of(server.uri("/")); | ||
final AggregatedHttpResponse res = client.get("/httpStatusException").aggregate().join(); | ||
assertThat(res.status()).isSameAs(HttpStatus.CREATED); | ||
await().untilAsserted(() -> assertThat(logHolder.get().path()).isEqualTo("/httpStatusException")); | ||
assertThat(logHolder.get().requestCause()).isNull(); | ||
} | ||
|
||
@Test | ||
void httpResponseExceptionIsNotLoggedAsRequestCause() { | ||
final HttpClient client = HttpClient.of(server.uri("/")); | ||
final AggregatedHttpResponse res = client.get("/httpResponseException").aggregate().join(); | ||
assertThat(res.status()).isSameAs(HttpStatus.CREATED); | ||
await().untilAsserted(() -> assertThat(logHolder.get().path()).isEqualTo("/httpResponseException")); | ||
assertThat(logHolder.get().requestCause()).isNull(); | ||
} | ||
} |