diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java index 4acbf82e810f868e8e1bd151afbe67f67cfa896b..c88a796aa4337c236808869d58dc671316209f7f 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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,22 +16,29 @@ package org.springframework.test.web.client.match; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import org.hamcrest.Matcher; import org.w3c.dom.Node; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpInputMessage; import org.springframework.http.MediaType; import org.springframework.http.client.ClientHttpRequest; +import org.springframework.http.converter.FormHttpMessageConverter; import org.springframework.mock.http.client.MockClientHttpRequest; import org.springframework.test.util.XmlExpectationsHelper; import org.springframework.test.web.client.RequestMatcher; +import org.springframework.util.MultiValueMap; import static org.hamcrest.MatcherAssert.*; import static org.springframework.test.util.AssertionErrors.*; + /** * Factory for request content {@code RequestMatcher}'s. An instance of this * class is typically accessed via {@link MockRestRequestMatchers#content()}. @@ -136,6 +143,30 @@ public class ContentRequestMatchers { }; } + /** + * Parse the body as form data and compare to the given {@code MultiValueMap}. + */ + public RequestMatcher formData(final MultiValueMap expectedContent) { + return new RequestMatcher() { + @Override + public void match(ClientHttpRequest request) throws IOException, AssertionError { + HttpInputMessage inputMessage = new HttpInputMessage() { + @Override + public InputStream getBody() throws IOException { + MockClientHttpRequest mockRequest = (MockClientHttpRequest) request; + return new ByteArrayInputStream(mockRequest.getBodyAsBytes()); + } + @Override + public HttpHeaders getHeaders() { + return request.getHeaders(); + } + }; + FormHttpMessageConverter converter = new FormHttpMessageConverter(); + assertEquals("Request content", expectedContent, converter.read(null, inputMessage)); + } + }; + } + /** * Parse the request body and the given String as XML and assert that the * two are "similar" - i.e. they contain the same elements and attributes diff --git a/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java index eada001d666a32f812d49d3418c99bbb87b8af48..08dc4c3323eaa0d43ea09cb9beb35bd1ca22d4d6 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 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. @@ -15,14 +15,19 @@ */ package org.springframework.test.web.client.match; +import java.nio.charset.Charset; + import org.junit.Before; import org.junit.Test; import org.springframework.http.MediaType; import org.springframework.mock.http.client.MockClientHttpRequest; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import static org.hamcrest.Matchers.*; + /** * Unit tests for {@link ContentRequestMatchers}. * @@ -32,11 +37,13 @@ public class ContentRequestMatchersTests { private MockClientHttpRequest request; + @Before public void setUp() { this.request = new MockClientHttpRequest(); } + @Test public void testContentType() throws Exception { this.request.getHeaders().setContentType(MediaType.APPLICATION_JSON); @@ -88,6 +95,22 @@ public class ContentRequestMatchersTests { MockRestRequestMatchers.content().bytes("Test".getBytes()).match(this.request); } + @Test + public void testFormData() throws Exception { + String contentType = "application/x-www-form-urlencoded;charset=UTF-8"; + String body = "name+1=value+1&name+2=value+A&name+2=value+B&name+3"; + + this.request.getHeaders().setContentType(MediaType.parseMediaType(contentType)); + this.request.getBody().write(body.getBytes(Charset.forName("UTF-8"))); + + MultiValueMap map = new LinkedMultiValueMap<>(); + map.add("name 1", "value 1"); + map.add("name 2", "value A"); + map.add("name 2", "value B"); + map.add("name 3", null); + MockRestRequestMatchers.content().formData(map).match(this.request); + } + @Test public void testXml() throws Exception { String content = "bazbazz"; diff --git a/src/asciidoc/whats-new.adoc b/src/asciidoc/whats-new.adoc index 003adb49c20e6ce5713b59380e251a09ae28c97d..4907766934d67251cae1da9d136045084b2e9956 100644 --- a/src/asciidoc/whats-new.adoc +++ b/src/asciidoc/whats-new.adoc @@ -672,4 +672,5 @@ Spring 4.3 also improves the caching abstraction as follows: === Testing Improvements * The JUnit support in the _Spring TestContext Framework_ now requires JUnit 4.12 or higher. -* HeaderResultMatchers in Spring MVC Test supports expectations on multiple header values \ No newline at end of file +* Client-side Spring MVC Test supports expectations for form data in the request body. +* Server-side Spring MVC Test supports expectations on response headers with multiple values. \ No newline at end of file