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

XML response content-type test fails incorrectly #367

Closed
johanhaleby opened this issue Jul 21, 2015 · 7 comments
Closed

XML response content-type test fails incorrectly #367

johanhaleby opened this issue Jul 21, 2015 · 7 comments

Comments

@johanhaleby
Copy link
Collaborator

From exsusc...@gmail.com on January 12, 2013 00:58:32

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\ResponseSpecificationImpl.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: http://code.google.com/p/rest-assured/issues/detail?id=213

@johanhaleby
Copy link
Collaborator Author

From exsusc...@gmail.com on January 11, 2013 15:59:08

CORRECTION: 1. Create a test that responds with content-type "text/xml"

@johanhaleby
Copy link
Collaborator Author

From exsusc...@gmail.com on January 11, 2013 16:01:11

CORRECTION 2:
Upon inspecting the source code at rest-assured-1.7.1-sources.jar!com\jayway\restassured\internal\ResponseSpecificationImpl.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));
}

@johanhaleby
Copy link
Collaborator Author

From exsusc...@gmail.com on January 11, 2013 16:33:41

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

@johanhaleby
Copy link
Collaborator Author

From johan.ha...@gmail.com on January 14, 2013 02:56:00

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?

Status: Accepted

@johanhaleby
Copy link
Collaborator Author

From exsusc...@gmail.com on January 16, 2013 17:42:05

Hi Johan, I'm not familiar with Github, how do I do that?

@johanhaleby
Copy link
Collaborator Author

From johan.ha...@gmail.com on January 23, 2013 06:07:18

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.

@johanhaleby
Copy link
Collaborator Author

From johan.ha...@gmail.com on February 02, 2013 04:24:26

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:

sonatype https://oss.sonatype.org/content/repositories/snapshots/

Thanks a for all your help!

Status: Fixed

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

No branches or pull requests

1 participant