提交 1416c2a0 编写于 作者: R Rossen Stoyanchev

ContentRequestMatchers asserts request with form data

Issue: SPR-13044
上级 2f6d86b7
/*
* 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<String, String> 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
......
/*
* 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<String, String> 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 = "<foo><bar>baz</bar><bar>bazz</bar></foo>";
......
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册