sahaya / rest-assured

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

XML response content-type test fails incorrectly #213

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.  Create a test that responds with content-type "application/xml"
2.  Use the response specification: expect().contentType(ContentType.XML)

What is the expected output? What do you see instead?
The test should pass but it fails with the following error message:

java.lang.AssertionError: Expected content-type "application/xml" doesn't match 
actual content-type "text/xml;charset=UTF-8".
    at sun.reflect.GeneratedConstructorAccessor48.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

It should pass because "text/xml" is XML.

What version of the product are you using? On what operating system?
rest-assured 1.7.1

Please provide any additional information below.

Upon inspecting the source code at 
rest-assured-1.7.1-sources.jar!com\jayway\restassured\internal\ResponseSpecifica
tionImpl.groovy:393 
(ResponseSpecificationImpl$HamcrestAssertionClosure.validateContentType(Response
)) I find that this line is incorrect:

if (!contentType.matches(actualContentType)) {
  throw new AssertionError(String.format("Expected content-type %s doesn't match actual content-type \"%s\".", contentType, actualContentType));
}

It should be something like:

Matcher m = Pattern.compile("(^[\w\d_\-]+/[\w\d_\-])\s*(?:;)");
if (m.find())
  actualContentType = m.group(1);

if (ContentType.fromContentType(actualContentType) != contentType)
  throw new AssertionError(...);

Original issue reported on code.google.com by exsusc...@gmail.com on 11 Jan 2013 at 11:58

GoogleCodeExporter commented 9 years ago
CORRECTION: 1.  Create a test that responds with content-type "text/xml"

Original comment by exsusc...@gmail.com on 11 Jan 2013 at 11:59

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
CORRECTION 2:
Upon inspecting the source code at 
rest-assured-1.7.1-sources.jar!com\jayway\restassured\internal\ResponseSpecifica
tionImpl.groovy:393 
(ResponseSpecificationImpl$HamcrestAssertionClosure.validateContentType(Response
)) I find that this line is incorrect:

} else if (!StringUtils.startsWith(actualContentType, contentType.toString())) {
  throw new AssertionError(String.format("Expected content-type \"%s\" doesn't match actual content-type \"%s\".", contentType, actualContentType));
}

Original comment by exsusc...@gmail.com on 12 Jan 2013 at 12:01

GoogleCodeExporter commented 9 years ago
CORRECTION 3:
Matcher m = Pattern.compile("(^[\w\d_\-]+/[\w\d_\-]+)\s*(?:;)");

Original comment by exsusc...@gmail.com on 12 Jan 2013 at 12:33

GoogleCodeExporter commented 9 years ago
Thanks for sharing and for investigating the issue. Could you send a pull 
request at github and I'll include the fix in the main branch?

Original comment by johan.ha...@gmail.com on 14 Jan 2013 at 10:56

GoogleCodeExporter commented 9 years ago
Hi Johan, I'm not familiar with Github, how do I do that?

Original comment by exsusc...@gmail.com on 17 Jan 2013 at 1:42

GoogleCodeExporter commented 9 years ago
Sorry for the late response, you can read about pull requests here: 
https://help.github.com/articles/using-pull-requests . That's one of the things 
that makes github so awesome.

Original comment by johan.ha...@gmail.com on 23 Jan 2013 at 2:07

GoogleCodeExporter commented 9 years ago
I've now applied your comments to master. If you want to try it out depend on 
version 1.7.3-SNAPSHOT after having added the following maven repo:

<repositories>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots />
        </repository>
</repositories>

Thanks a for all your help!

Original comment by johan.ha...@gmail.com on 2 Feb 2013 at 12:24