From b2d6596901bc782a96685a0778d0da848e5755b6 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 14 Jan 2013 21:38:51 -0500 Subject: [PATCH] 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 --- .../client/match/ContentRequestMatchers.java | 27 +++++++++++++-- .../servlet/result/ContentResultMatchers.java | 33 +++++++++++++++++-- .../resultmatchers/ContentAssertionTests.java | 6 ++-- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java index 5b89cef1a7..1bcb49a5b9 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java @@ -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"); * you may not use this file except in compliance with the License. @@ -16,8 +16,8 @@ package org.springframework.test.web.client.match; 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.MatcherAssertionErrors.assertThat; 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}. */ diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java index b39875baaa..c71dc98b7f 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java @@ -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"); * 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) { return contentType(MediaType.parseMediaType(contentType)); @@ -62,6 +64,9 @@ public class ContentResultMatchers { /** * 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) { 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. * @see HttpServletResponse#getCharacterEncoding() diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java index 80a0d138f3..4777e5e5e4 100644 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -62,7 +62,9 @@ public class ContentAssertionTests { this.mockMvc.perform(get("/handleUtf8")) .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