forked from helidon-io/helidon
-
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.
Correctly handling custom reason phrase of status (helidon-io#3464)
* Correctly handling custom reason phrase of status in webserver and webclient. Signed-off-by: Tomas Langer <[email protected]> * Fix to use correct status phrases Signed-off-by: Tomas Langer <[email protected]> * Checkstyle fix. Signed-off-by: Tomas Langer <[email protected]>
- Loading branch information
1 parent
b04f7d9
commit 1ece4c1
Showing
6 changed files
with
139 additions
and
33 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
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
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
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
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
102 changes: 102 additions & 0 deletions
102
webserver/webserver/src/test/java/io/helidon/webserver/ReasonPhraseTest.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,102 @@ | ||
/* | ||
* Copyright (c) 2021 Oracle and/or its affiliates. | ||
* | ||
* Licensed 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 | ||
* | ||
* http://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 io.helidon.webserver; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.helidon.common.LogConfig; | ||
import io.helidon.common.http.Http; | ||
import io.helidon.webclient.WebClient; | ||
import io.helidon.webclient.WebClientResponse; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static io.helidon.common.http.Http.Status.BAD_REQUEST_400; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
class ReasonPhraseTest { | ||
public static final String CUSTOM_ERROR = "Custom error"; | ||
private static final Http.ResponseStatus CUSTOM_BAD_REQUEST = Http.ResponseStatus.create(BAD_REQUEST_400.code(), | ||
CUSTOM_ERROR); | ||
private static WebServer server; | ||
private static WebClient client; | ||
|
||
@BeforeAll | ||
static void createWebServer() { | ||
LogConfig.configureRuntime(); | ||
server = WebServer.builder() | ||
.routing(Routing.builder() | ||
.get("/default", ReasonPhraseTest::defaultCode) | ||
.get("/custom", ReasonPhraseTest::customCode)) | ||
.build() | ||
.start() | ||
.await(10, TimeUnit.SECONDS); | ||
|
||
client = WebClient.builder() | ||
.baseUri("http://localhost:" + server.port()) | ||
.build(); | ||
} | ||
|
||
@AfterAll | ||
static void stopWebServer() { | ||
if (server != null) { | ||
server.shutdown() | ||
.await(10, TimeUnit.SECONDS); | ||
} | ||
} | ||
|
||
@Test | ||
void testDefaultReasonPhrase() { | ||
WebClientResponse response = client.get() | ||
.path("/default") | ||
.request() | ||
.await(10, TimeUnit.SECONDS); | ||
|
||
Http.ResponseStatus status = response.status(); | ||
assertThat(status.code(), is(400)); | ||
assertThat(status.reasonPhrase(), is(BAD_REQUEST_400.reasonPhrase())); | ||
|
||
response.close(); | ||
} | ||
|
||
@Test | ||
void testCustomReasonPhrase() { | ||
WebClientResponse response = client.get() | ||
.path("/custom") | ||
.request() | ||
.await(10, TimeUnit.SECONDS); | ||
|
||
Http.ResponseStatus status = response.status(); | ||
assertThat(status.code(), is(400)); | ||
assertThat(status.reasonPhrase(), is(CUSTOM_ERROR)); | ||
|
||
response.close(); | ||
} | ||
|
||
private static void defaultCode(ServerRequest req, ServerResponse res) { | ||
res.status(BAD_REQUEST_400) | ||
.send(); | ||
} | ||
|
||
private static void customCode(ServerRequest req, ServerResponse res) { | ||
res.status(CUSTOM_BAD_REQUEST) | ||
.send(); | ||
} | ||
} |