1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
31
31
import org .springframework .mock .http .client .MockClientHttpResponse ;
32
32
import org .springframework .mock .web .MockHttpServletResponse ;
33
33
import org .springframework .test .web .servlet .MockMvc ;
34
- import org .springframework .test .web .servlet .MvcResult ;
35
- import org .springframework .test .web .servlet .request .MockHttpServletRequestBuilder ;
36
34
import org .springframework .util .Assert ;
37
35
38
- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
36
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .request ;
37
+
39
38
40
39
/**
41
40
* A {@link ClientHttpRequestFactory} for requests executed via {@link MockMvc}.
42
41
*
42
+ * <p>As of 5.0 this class also implements
43
+ * {@link org.springframework.http.client.AsyncClientHttpRequestFactory
44
+ * AsyncClientHttpRequestFactory}. However note that
45
+ * {@link org.springframework.web.client.AsyncRestTemplate} and related classes
46
+ * have been deprecated at the same time.
47
+ *
43
48
* @author Rossen Stoyanchev
44
49
* @since 3.2
45
50
*/
46
- public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory {
51
+ @ SuppressWarnings ("deprecation" )
52
+ public class MockMvcClientHttpRequestFactory
53
+ implements ClientHttpRequestFactory , org .springframework .http .client .AsyncClientHttpRequestFactory {
47
54
48
55
private final MockMvc mockMvc ;
49
56
@@ -55,31 +62,46 @@ public MockMvcClientHttpRequestFactory(MockMvc mockMvc) {
55
62
56
63
57
64
@ Override
58
- public ClientHttpRequest createRequest (final URI uri , final HttpMethod httpMethod ) throws IOException {
65
+ public ClientHttpRequest createRequest (final URI uri , final HttpMethod httpMethod ) {
59
66
return new MockClientHttpRequest (httpMethod , uri ) {
60
67
@ Override
61
68
public ClientHttpResponse executeInternal () throws IOException {
62
- try {
63
- MockHttpServletRequestBuilder requestBuilder = request (httpMethod , uri );
64
- requestBuilder .content (getBodyAsBytes ());
65
- requestBuilder .headers (getHeaders ());
66
- MvcResult mvcResult = MockMvcClientHttpRequestFactory .this .mockMvc .perform (requestBuilder ).andReturn ();
67
- MockHttpServletResponse servletResponse = mvcResult .getResponse ();
68
- HttpStatus status = HttpStatus .valueOf (servletResponse .getStatus ());
69
- byte [] body = servletResponse .getContentAsByteArray ();
70
- HttpHeaders headers = getResponseHeaders (servletResponse );
71
- MockClientHttpResponse clientResponse = new MockClientHttpResponse (body , status );
72
- clientResponse .getHeaders ().putAll (headers );
73
- return clientResponse ;
74
- }
75
- catch (Exception ex ) {
76
- byte [] body = ex .toString ().getBytes (StandardCharsets .UTF_8 );
77
- return new MockClientHttpResponse (body , HttpStatus .INTERNAL_SERVER_ERROR );
78
- }
69
+ return getClientHttpResponse (httpMethod , uri , getHeaders (), getBodyAsBytes ());
70
+ }
71
+ };
72
+ }
73
+
74
+ @ Override
75
+ public org .springframework .http .client .AsyncClientHttpRequest createAsyncRequest (URI uri , HttpMethod method ) {
76
+ return new org .springframework .mock .http .client .MockAsyncClientHttpRequest (method , uri ) {
77
+ @ Override
78
+ protected ClientHttpResponse executeInternal () throws IOException {
79
+ return getClientHttpResponse (method , uri , getHeaders (), getBodyAsBytes ());
79
80
}
80
81
};
81
82
}
82
83
84
+ private ClientHttpResponse getClientHttpResponse (
85
+ HttpMethod httpMethod , URI uri , HttpHeaders requestHeaders , byte [] requestBody ) {
86
+
87
+ try {
88
+ MockHttpServletResponse servletResponse = mockMvc
89
+ .perform (request (httpMethod , uri ).content (requestBody ).headers (requestHeaders ))
90
+ .andReturn ()
91
+ .getResponse ();
92
+
93
+ HttpStatus status = HttpStatus .valueOf (servletResponse .getStatus ());
94
+ byte [] body = servletResponse .getContentAsByteArray ();
95
+ MockClientHttpResponse clientResponse = new MockClientHttpResponse (body , status );
96
+ clientResponse .getHeaders ().putAll (getResponseHeaders (servletResponse ));
97
+ return clientResponse ;
98
+ }
99
+ catch (Exception ex ) {
100
+ byte [] body = ex .toString ().getBytes (StandardCharsets .UTF_8 );
101
+ return new MockClientHttpResponse (body , HttpStatus .INTERNAL_SERVER_ERROR );
102
+ }
103
+ }
104
+
83
105
private HttpHeaders getResponseHeaders (MockHttpServletResponse response ) {
84
106
HttpHeaders headers = new HttpHeaders ();
85
107
for (String name : response .getHeaderNames ()) {
0 commit comments