-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use java.util.Locale to parse the languages from the Accept-Language …
…header * added some tests with various language tag scenarios * Fixes #36227 Signed-off-by:Nathan Erwin <nathan.d.erwin@gmail.com>
- Loading branch information
Showing
2 changed files
with
174 additions
and
14 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
172 changes: 172 additions & 0 deletions
172
...rc/test/java/org/jboss/resteasy/reactive/server/vertx/test/headers/RequestHeaderTest.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,172 @@ | ||
package org.jboss.resteasy.reactive.server.vertx.test.headers; | ||
|
||
import static org.hamcrest.Matchers.equalToIgnoringCase; | ||
|
||
import java.util.Locale; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.HttpHeaders; | ||
|
||
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.restassured.RestAssured; | ||
|
||
public class RequestHeaderTest { | ||
|
||
private static final String BASE_PATH = "/test"; | ||
|
||
@RegisterExtension | ||
static ResteasyReactiveUnitTest TEST = new ResteasyReactiveUnitTest() | ||
.addScanCustomizer(scanStep -> scanStep.setSingleDefaultProduces(true)) | ||
.withApplicationRoot((jar) -> jar.addClasses(TestResource.class)); | ||
|
||
@Test | ||
public void testISO2Language() { | ||
String expected = "en"; | ||
|
||
RestAssured | ||
.given() | ||
.header(HttpHeaders.ACCEPT_LANGUAGE, expected) | ||
.get(BASE_PATH) | ||
.then() | ||
.statusCode(200) | ||
.assertThat().body(equalToIgnoringCase(expected)); | ||
} | ||
|
||
@Test | ||
public void testISO3Language() { | ||
String expected = "tlh"; | ||
|
||
RestAssured | ||
.given() | ||
.header(HttpHeaders.ACCEPT_LANGUAGE, expected) | ||
.get(BASE_PATH) | ||
.then() | ||
.statusCode(200) | ||
.assertThat().body(equalToIgnoringCase(expected)); | ||
} | ||
|
||
@Test | ||
public void testScriptSubtag() { | ||
String expected = "zh-Hans"; | ||
|
||
RestAssured | ||
.given() | ||
.header(HttpHeaders.ACCEPT_LANGUAGE, expected) | ||
.get(BASE_PATH) | ||
.then() | ||
.statusCode(200) | ||
.assertThat().body(equalToIgnoringCase(expected)); | ||
} | ||
|
||
@Test | ||
public void testRegionSubtag() { | ||
String expected = "en-GB"; | ||
|
||
RestAssured | ||
.given() | ||
.header(HttpHeaders.ACCEPT_LANGUAGE, expected) | ||
.get(BASE_PATH) | ||
.then() | ||
.statusCode(200) | ||
.assertThat().body(equalToIgnoringCase(expected)); | ||
} | ||
|
||
@Test | ||
public void testRegionSubtag2() { | ||
String expected = "es-005"; | ||
|
||
RestAssured | ||
.given() | ||
.header(HttpHeaders.ACCEPT_LANGUAGE, expected) | ||
.get(BASE_PATH) | ||
.then() | ||
.statusCode(200) | ||
.assertThat().body(equalToIgnoringCase(expected)); | ||
} | ||
|
||
@Test | ||
public void testRegionSubtag3() { | ||
String expected = "zh-Hant-HK"; | ||
|
||
RestAssured | ||
.given() | ||
.header(HttpHeaders.ACCEPT_LANGUAGE, expected) | ||
.get(BASE_PATH) | ||
.then() | ||
.statusCode(200) | ||
.assertThat().body(equalToIgnoringCase(expected)); | ||
} | ||
|
||
@Test | ||
public void testVariantSubtag() { | ||
String expected = "sl-nedis"; | ||
|
||
RestAssured | ||
.given() | ||
.header(HttpHeaders.ACCEPT_LANGUAGE, expected) | ||
.get(BASE_PATH) | ||
.then() | ||
.statusCode(200) | ||
.assertThat().body(equalToIgnoringCase(expected)); | ||
} | ||
|
||
@Test | ||
public void testVariantSubtag2() { | ||
String expected = "sl-IT-nedis"; | ||
|
||
RestAssured | ||
.given() | ||
.header(HttpHeaders.ACCEPT_LANGUAGE, expected) | ||
.get(BASE_PATH) | ||
.then() | ||
.statusCode(200) | ||
.assertThat().body(equalToIgnoringCase(expected)); | ||
} | ||
|
||
@Test | ||
public void testExtensionSubtag() { | ||
String expected = "de-DE-u-co-phonebk"; | ||
|
||
RestAssured | ||
.given() | ||
.header(HttpHeaders.ACCEPT_LANGUAGE, expected) | ||
.get(BASE_PATH) | ||
.then() | ||
.statusCode(200) | ||
.assertThat().body(equalToIgnoringCase(expected)); | ||
} | ||
|
||
@Test | ||
public void testPrivateUseSubtag() { | ||
String expected = "en-US-x-twain"; | ||
|
||
RestAssured | ||
.given() | ||
.header(HttpHeaders.ACCEPT_LANGUAGE, expected) | ||
.get(BASE_PATH) | ||
.then() | ||
.statusCode(200) | ||
.assertThat().body(equalToIgnoringCase(expected)); | ||
} | ||
|
||
@Path(BASE_PATH) | ||
public static class TestResource { | ||
|
||
@Context | ||
HttpHeaders headers; | ||
|
||
@GET | ||
public String echo() { | ||
final Locale locale = headers.getAcceptableLanguages().isEmpty() ? Locale.ENGLISH | ||
: headers.getAcceptableLanguages().get(0); | ||
|
||
return locale.toLanguageTag(); | ||
} | ||
|
||
} | ||
} |