Add contentTypeCompatibleWith option to Spring MVC Test
An expectation such as content().contentType(MediaType.TEXT_PLAIN) fails if the actual media type contains a charset or another parameter. A new method allows comparing the media type and subtype only via content().contentTypeCompatibleWith(MediaType.TEXT_PLAIN). Issue: SPR-10165
This commit is contained in:
parent
9dc7b5feef
commit
b2d6596901
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2013 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -16,8 +16,8 @@
|
||||||
package org.springframework.test.web.client.match;
|
package org.springframework.test.web.client.match;
|
||||||
|
|
||||||
import static org.springframework.test.util.AssertionErrors.assertEquals;
|
import static org.springframework.test.util.AssertionErrors.assertEquals;
|
||||||
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
|
|
||||||
import static org.springframework.test.util.AssertionErrors.assertTrue;
|
import static org.springframework.test.util.AssertionErrors.assertTrue;
|
||||||
|
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
@ -72,6 +72,29 @@ public class ContentRequestMatchers {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert the request content type is compatible with the given
|
||||||
|
* content type as defined by {@link MediaType#isCompatibleWith(MediaType)}.
|
||||||
|
*/
|
||||||
|
public RequestMatcher contentTypeCompatibleWith(String contentType) {
|
||||||
|
return contentTypeCompatibleWith(MediaType.parseMediaType(contentType));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert the request content type is compatible with the given
|
||||||
|
* content type as defined by {@link MediaType#isCompatibleWith(MediaType)}.
|
||||||
|
*/
|
||||||
|
public RequestMatcher contentTypeCompatibleWith(final MediaType contentType) {
|
||||||
|
return new RequestMatcher() {
|
||||||
|
public void match(ClientHttpRequest request) throws IOException, AssertionError {
|
||||||
|
MediaType actualContentType = request.getHeaders().getContentType();
|
||||||
|
assertTrue("Content type not set", actualContentType != null);
|
||||||
|
assertTrue("Content type [" + actualContentType + "] is not compatible with [" + contentType + "]",
|
||||||
|
actualContentType.isCompatibleWith(contentType));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the body of the request as a UTF-8 string and appply the given {@link Matcher}.
|
* Get the body of the request as a UTF-8 string and appply the given {@link Matcher}.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2013 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -54,7 +54,9 @@ public class ContentResultMatchers {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assert the ServletResponse content type.
|
* Assert the ServletResponse content type. The given content type must
|
||||||
|
* fully match including type, sub-type, and parameters. For checking
|
||||||
|
* only the type and sub-type see {@link #contentTypeCompatibleWith(String)}.
|
||||||
*/
|
*/
|
||||||
public ResultMatcher contentType(String contentType) {
|
public ResultMatcher contentType(String contentType) {
|
||||||
return contentType(MediaType.parseMediaType(contentType));
|
return contentType(MediaType.parseMediaType(contentType));
|
||||||
|
|
@ -62,6 +64,9 @@ public class ContentResultMatchers {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assert the ServletResponse content type after parsing it as a MediaType.
|
* Assert the ServletResponse content type after parsing it as a MediaType.
|
||||||
|
* The given content type must fully match including type, sub-type, and
|
||||||
|
* parameters. For checking only the type and sub-type see
|
||||||
|
* {@link #contentTypeCompatibleWith(MediaType)}.
|
||||||
*/
|
*/
|
||||||
public ResultMatcher contentType(final MediaType contentType) {
|
public ResultMatcher contentType(final MediaType contentType) {
|
||||||
return new ResultMatcher() {
|
return new ResultMatcher() {
|
||||||
|
|
@ -73,6 +78,30 @@ public class ContentResultMatchers {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert the ServletResponse content type is compatible with the given
|
||||||
|
* content type as defined by {@link MediaType#isCompatibleWith(MediaType)}.
|
||||||
|
*/
|
||||||
|
public ResultMatcher contentTypeCompatibleWith(String contentType) {
|
||||||
|
return contentTypeCompatibleWith(MediaType.parseMediaType(contentType));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert the ServletResponse content type is compatible with the given
|
||||||
|
* content type as defined by {@link MediaType#isCompatibleWith(MediaType)}.
|
||||||
|
*/
|
||||||
|
public ResultMatcher contentTypeCompatibleWith(final MediaType contentType) {
|
||||||
|
return new ResultMatcher() {
|
||||||
|
public void match(MvcResult result) throws Exception {
|
||||||
|
String actual = result.getResponse().getContentType();
|
||||||
|
assertTrue("Content type not set", actual != null);
|
||||||
|
MediaType actualContentType = MediaType.parseMediaType(actual);
|
||||||
|
assertTrue("Content type [" + actual + "] is not compatible with [" + contentType + "]",
|
||||||
|
actualContentType.isCompatibleWith(contentType));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assert the character encoding in the ServletResponse.
|
* Assert the character encoding in the ServletResponse.
|
||||||
* @see HttpServletResponse#getCharacterEncoding()
|
* @see HttpServletResponse#getCharacterEncoding()
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2013 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -62,7 +62,9 @@ public class ContentAssertionTests {
|
||||||
|
|
||||||
this.mockMvc.perform(get("/handleUtf8"))
|
this.mockMvc.perform(get("/handleUtf8"))
|
||||||
.andExpect(content().contentType(MediaType.valueOf("text/plain;charset=UTF-8")))
|
.andExpect(content().contentType(MediaType.valueOf("text/plain;charset=UTF-8")))
|
||||||
.andExpect(content().contentType("text/plain;charset=UTF-8"));
|
.andExpect(content().contentType("text/plain;charset=UTF-8"))
|
||||||
|
.andExpect(content().contentTypeCompatibleWith("text/plan"))
|
||||||
|
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_PLAIN));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue