swangkkb / rest-assured

Automatically exported from code.google.com/p/rest-assured
0 stars 0 forks source link

Bug in ResponseSpecification com.jayway.restassured.specification.ResponseSpecification.body(String path, Matcher<?> matcher, Object... additionalKeyMatcherPairs) with special characters #339

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Expose a web service that returns a JSON with special characters (charset 
should be UTF-8) like £
2. Call RestAssured.given().header("Accept", 
"application/json").body("lotto.amountStr", 
org.hamcrest.Matchers.equalTo("£100")).when().get("lotto");

What is the expected output? 
The test should pass.

What do you see instead?
JSON path lotto.amountStr doesn't match.
Expected: £100
  Actual: £100

What version of the product are you using?
From rest-assured 2.3.1 upgraded to 2.3.2 but the error persists

On what operating system?
Windows 8

Please provide any additional information below.

I noticed that this only happens when the RestAssured.given().header("Accept", 
"application/json").body(String path, Matcher<?> matcher, Object... 
additionalKeyMatcherPairs) is called because when I tried to print the Response 
string using RestAssured.given().header("Accept", 
"application/json").get("lotto").body().path("lotto.amountStr"), the correct 
amount string is being printed which is £100.
I think this has something to do with the .body(String path, Matcher<?> 
matcher, Object... additionalKeyMatcherPairs) implementation not taking into 
consideration the encoding.

In addition, my colleague who uses MAC laptop, does not encounter this unit 
test error.

Original issue reported on code.google.com by arnel.hemady on 24 Jun 2014 at 10:06

GoogleCodeExporter commented 9 years ago
Thanks for reporting, looks like an encoding issue.

Original comment by johan.ha...@gmail.com on 5 Jul 2014 at 5:02

GoogleCodeExporter commented 9 years ago
If it helps, I am getting the same problem with the following code :

final String url = "http://localhost:80/api/movies/one/abc123;
final String expectedTitle = "La confrérie des larmes";

// Act
final RequestSpecBuilder builder = new RequestSpecBuilder();
builder.addHeader("Accept", "application/json");
final RequestSpecification requestSpec = builder.build();

given() 
  .spec(requestSpec) 
  .get(url)
  .then()
  .body("content.title", is(expectedTitle));

The Stack trace says: JSON path content.title doesn't match. Expected: is "La 
confrérie des larmes"   Actual: La confrérie des larmes

And if I try the following code :

final ResponseBody body = given() //
  .spec(requestSpec) //
  .get(url)//
  .body();

Assert.assertEquals(expectedTitle, body.path("content.title"));

The stack trace says: expected:<La confr[é]rie des larmes> but was:<La 
confr[é]rie des larmes>

Original comment by thierry...@gmail.com on 22 Sep 2014 at 1:39

GoogleCodeExporter commented 9 years ago
Tested with 2.3.3

Original comment by thierry...@gmail.com on 22 Sep 2014 at 1:41

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Are you providing any data to the server as well? In that case have you tried 
specifying a charset with the content-type? 

If not, do you know what charset the server returns? Does the server specify 
the charset in the content-type header? If I remember things correctly REST 
Assured uses default system charset if a charset is not specified in the 
response. You can change that to another charset using the DecoderConfig 
(https://code.google.com/p/rest-assured/wiki/Usage#Decoder_Config). For example:

RestAssured.config = 
RestAssured.config().decoderConfig(decoderConfig().defaultContentCharset("UTF-8"
));

Original comment by johan.ha...@gmail.com on 23 Sep 2014 at 6:51