Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use java.util.Locale to parse the languages from the Accept-Language header #36228

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@ public static Locale extractLocale(String lang) {
if (q > -1) {
lang = lang.substring(0, q);
}
String[] split = lang.trim().split("-");
if (split.length == 1) {
return new Locale(split[0].toLowerCase());
} else if (split.length == 2) {
return new Locale(split[0].toLowerCase(), split[1].toLowerCase());
} else if (split.length > 2) {
return new Locale(split[0], split[1], split[2]);
}
return null; // unreachable
return Locale.forLanguageTag(lang);
}

/**
Expand All @@ -30,10 +22,6 @@ public static Locale extractLocale(String lang) {
* @return converted language format string
*/
public static String toLanguageString(Locale value) {
StringBuffer buf = new StringBuffer(value.getLanguage().toLowerCase());
if (value.getCountry() != null && !value.getCountry().equals("")) {
buf.append("-").append(value.getCountry().toLowerCase());
}
return buf.toString();
return value.toLanguageTag();
}
}
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();
}

}
}
Loading