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

TestRestTemplate does the url (percent) encoding twice #8163

Closed
jaroslav-muller opened this issue Feb 1, 2017 · 4 comments
Closed

TestRestTemplate does the url (percent) encoding twice #8163

jaroslav-muller opened this issue Feb 1, 2017 · 4 comments
Assignees
Labels
type: bug A general bug type: regression A regression from a previous release
Milestone

Comments

@jaroslav-muller
Copy link

When sending request to url that contains characters that need to be encoded TestRestTemplate does the encoding twice.
For example when sending GET to http://localhost?param={something}
the correct url string should be http://localhost?param=%7Bsomething%7D
but is http://localhost?param=%257Bsomething%257D

This is my typical usage in tests (groovy):

def entity = RequestEntity
  .get(UriComponentsBuilder
    .fromHttpUrl("http://localhost")
    .queryParam("param", "{something}")
    .build()
    .toUri()
  )
  .build()
ResponseEntity<Object> resp = rest.exchange(entity, Object)

This bug is introduced by #7891. (Also it works OK in 1.4.3 and is broken in 1.4.4 and 1.5.1)
Method createRequestEntityWithExpandedUri first transforms the url to string and then parses it again.

This code mimics what TestRestTemplate does and you can check the difference between expected and actual result:

def uri = UriComponentsBuilder
  .fromHttpUrl("http://localhost")
  .queryParam("param", "{something}")
  .build()
  .toUri()

def e1 = RequestEntity
  .get(uri)
  .build()

def handler = new LocalHostUriTemplateHandler(new MockEnvironment())

def expandedUri = handler.expand(e1.url.toString())

def e2 = new RequestEntity(
  e1.body, e1.headers, e1.method, expandedUri, e1.type
)

println uri       // expected
println e2.url  // actual
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 1, 2017
@wilkinsona wilkinsona added priority: high type: bug A general bug type: regression A regression from a previous release and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 1, 2017
@wilkinsona wilkinsona added this to the 1.4.5 milestone Feb 1, 2017
@wilkinsona wilkinsona self-assigned this Feb 1, 2017
@Selindek
Copy link

I'm not sure whether I should create a new issue, but I want to point out that all exchange methods that accept String instead of Uri are still affected by this bug.

@wilkinsona
Copy link
Member

wilkinsona commented Apr 10, 2017

@Selindek This bug was specifically for methods that took a URI so if you have a problem with methods that take a String then you are experiencing a different problem. Perhaps you're passing in a string that has already been encoded? If that's not the case and you believe that an unencoded string is being double-encoded, please open a new issue with a sample that reproduces the problem.

@Selindek
Copy link

Selindek commented Apr 11, 2017

I think this is the similar issue because it happens if I pass a relative url in the string. (like /test?param="qqq"). And when I checked the fix it turned out that it basically fixes the relative URIs by adding the root to them. So I thought that maybe the same conversion is needed for the url-Strings too...

@snicoll
Copy link
Member

snicoll commented Apr 11, 2017

@Selindek as Andy asked, please open a separate issue with a sample that reproduces the issue you're experiencing. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

5 participants