Skip to content

MockHttpServletRequest.addHeader fails for invalid LanguageRange in Accept-Language [SPR-16454] #20999

Closed

Description

@spring-projects-issues

Manuel Jordan opened SPR-16454 and commented

I have a Spring Project 4.3.10.RELEASE, MVC and Rest are tested (working with JUnit 4).
In some scenarios the Accept-Language header is applied. Until here all work fine.

I have created other project working with Spring Project 5.0.3.RELEASE, practically the same infrastructure is used, but all the Test classes have migrated to JUnit 5. Remember the business and infrastructure code remains the same.

When I test MVC it fails with the following error message:

Error Message
range=en_us
Stacktrace
java.lang.IllegalArgumentException: range=en_us
	at java.util.Locale$LanguageRange.<init>(Locale.java:2913)
	at sun.util.locale.LocaleMatcher.parse(LocaleMatcher.java:284)
	at java.util.Locale$LanguageRange.parse(Locale.java:3028)
	at org.springframework.http.HttpHeaders.getAcceptLanguage(HttpHeaders.java:479)
	at org.springframework.http.HttpHeaders.getAcceptLanguageAsLocales(HttpHeaders.java:499)
	at org.springframework.mock.web.MockHttpServletRequest.addHeader(MockHttpServletRequest.java:996)
	at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.buildRequest(MockHttpServletRequestBuilder.java:624)
	at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:141)
	at com.manuel.jordan.controller.persona.AbstractPersonaDeleteOneControllerTest.deleteOneDeleteTest(AbstractPersonaDeleteOneControllerTest.java:112)
	at com.manuel.jordan.controller

What is strange is range=en_us , the Locale is created in this way within an enum new Locale("en","US").

The Rest Test classes fails with:

Error Message
500 Internal Server Error
Stacktrace
org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:97)
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79)
	at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
	at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:773)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:726)
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:700)
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:670)
	at 

Sadly the error message is not useful

Note I get the same error messages if for the first project I create a new branch through Git and use Spring updated to 5.0.3.RELEASE.

Thus not sure if it is bug or something extra about configuration is mandatory now. I am confused

Thanks


Affects: 5.0 GA, 5.0.3

Issue Links:

Referenced from: commits b3e21ec

Activity

  1. spring-projects-issues commented on Feb 1, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    As far as I can tell, that Accept-Language header is invalid in HTTP: It should really say "en-US" in the BCP 47 language tag format instead of using Java's native Locale.toString() format.

    That said, MockHttpServletRequest.addHeader shouldn't fail in such a case. It needs to leniently accept invalid header values as well. We'll revise this for 5.0.4.

  2. spring-projects-issues commented on Feb 1, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Manuel Jordan commented

    I am confused why you say that Accept-Language is invalid if it appears in:

    I will do a research about BCP 47 in Tags for Identifying Languages

    That said, MockHttpServletRequest.addHeader shouldn't fail in such a case. It needs to leniently accept invalid header values as well. We'll revise this for 5.0.4.

    Thanks by the explanation

  3. spring-projects-issues commented on Feb 1, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    I didn't mean to imply that Accept-Language as a header was invalid. My point was that your value for it needs to say "en-US" according to the HTTP definitions, not "en_US" (which is just the default representation for a java.util.Locale).

  4. spring-projects-issues commented on Feb 2, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Manuel Jordan commented

    Oh I understand now, sorry by the confusion. Thanks a lot by the quick fix.

    Please share the pull request, I want see the code. Thank you.

  5. added
    type: regression
    in: web
    on Jan 11, 2019
  6. added this to the 5.0.4 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: regression

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions