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

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

Closed
spring-projects-issues opened this issue Feb 1, 2018 · 4 comments
Assignees
Labels
in: web type: regression
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Feb 1, 2018

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:

  • #20162 Spring WebFlux fails with 500 if Accept-Language is set to an empty string
  • #19591 Add HttpHeaders getter and setter for Accept-Language
  • #21315 Spring MVC in runtime fails when the locale is changed according with BCP 47
  • #22098 MockHttpServletRequest changes Accept-Language header values

Referenced from: commits b3e21ec

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 1, 2018

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.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 1, 2018

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

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 1, 2018

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).

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 2, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web type: regression
Projects
None yet
Development

No branches or pull requests

2 participants