Skip to content

Commit

Permalink
Merge pull request quarkusio#36228 from nderwin-forks/bugfix/36227
Browse files Browse the repository at this point in the history
Use java.util.Locale to parse the languages from the Accept-Language header
  • Loading branch information
geoand authored Oct 2, 2024
2 parents 594eb38 + 633a16c commit 1365271
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 14 deletions.
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();
}

}
}

0 comments on commit 1365271

Please sign in to comment.