1
1
/*
2
- * Copyright 2011 the original author or authors.
2
+ * Copyright 2011-2012 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.
29
29
import org .springframework .util .Assert ;
30
30
31
31
/**
32
- * Mock implementation of {@link ClientHttpRequest}. Implements {@link ResponseActions} to form a fluent API.
33
- *
34
- * @author Arjen Poutsma
35
- * @author Lukas Krecan
32
+ * Mock implementation of {@code ClientHttpRequest} that maintains a list of
33
+ * request expectations, in the form of {@link RequestMatcher}'s, as well as one
34
+ * {@link ResponseCreator}. When {@link #execute()} is invoked, each request
35
+ * matcher is invoked to verify the expectations. If all expectations are met,
36
+ * a response is created with {@code ResponseCreator} and is then returned.
37
+ *
38
+ * <p>This class is also an implementation of {@link ResponseActions} to form a
39
+ * fluent API for adding {@link RequestMatcher}'s and a {@code ResponseCreator}.
40
+ *
36
41
* @author Craig Walls
42
+ * @author Rossen Stoyanchev
37
43
*/
38
44
public class MockClientHttpRequest implements ClientHttpRequest , ResponseActions {
39
45
40
- private final List <RequestMatcher > requestMatchers = new LinkedList <RequestMatcher >();
41
-
42
- private ResponseCreator responseCreator ;
43
-
44
46
private URI uri ;
45
47
46
48
private HttpMethod httpMethod ;
@@ -49,65 +51,95 @@ public class MockClientHttpRequest implements ClientHttpRequest, ResponseActions
49
51
50
52
private ByteArrayOutputStream bodyStream = new ByteArrayOutputStream ();
51
53
54
+ private final List <RequestMatcher > requestMatchers = new LinkedList <RequestMatcher >();
55
+
56
+ private ResponseCreator responseCreator ;
57
+
58
+
59
+ public MockClientHttpRequest (RequestMatcher requestMatcher ) {
60
+ Assert .notNull (requestMatcher , "RequestMatcher is required" );
61
+ this .requestMatchers .add (requestMatcher );
62
+ }
63
+
52
64
public void setUri (URI uri ) {
53
65
this .uri = uri ;
54
66
}
55
67
56
- public void setHttpMethod ( HttpMethod httpMethod ) {
57
- this .httpMethod = httpMethod ;
68
+ public URI getURI ( ) {
69
+ return this .uri ;
58
70
}
59
71
60
- void addRequestMatcher (RequestMatcher requestMatcher ) {
61
- Assert .notNull (requestMatcher , "'requestMatcher' must not be null" );
62
- requestMatchers .add (requestMatcher );
72
+ public void setMethod (HttpMethod httpMethod ) {
73
+ this .httpMethod = httpMethod ;
63
74
}
64
75
65
- // ResponseActions implementation
76
+ public HttpMethod getMethod () {
77
+ return this .httpMethod ;
78
+ }
66
79
67
- public ResponseActions andExpect (RequestMatcher requestMatcher ) {
68
- addRequestMatcher (requestMatcher );
69
- return this ;
80
+ public HttpHeaders getHeaders () {
81
+ return this .httpHeaders ;
70
82
}
71
83
72
- public void andRespond (ResponseCreator responseCreator ) {
73
- Assert .notNull (responseCreator , "'responseCreator' must not be null" );
74
- this .responseCreator = responseCreator ;
84
+ public OutputStream getBody () throws IOException {
85
+ return this .bodyStream ;
75
86
}
76
87
77
- public HttpMethod getMethod () {
78
- return httpMethod ;
88
+ public String getBodyAsString () throws IOException {
89
+ return this . bodyStream . toString ( "UTF-8" ) ;
79
90
}
80
91
81
- public URI getURI () {
82
- return uri ;
92
+ public byte [] getBodyAsByteArray () throws IOException {
93
+ return this . bodyStream . toByteArray () ;
83
94
}
84
95
85
- public HttpHeaders getHeaders () {
86
- return httpHeaders ;
96
+ public ClientHttpResponse execute () throws IOException {
97
+
98
+ if (this .requestMatchers .isEmpty ()) {
99
+ throw new AssertionError ("No request expectations to execute" );
100
+ }
101
+
102
+ if (this .responseCreator == null ) {
103
+ throw new AssertionError ("No ResponseCreator was set up. Add it after request expectations, "
104
+ + "e.g. MockRestServiceServer.expect(requestTo(\" /foo\" )).andRespond(withSuccess())" );
105
+ }
106
+
107
+ for (RequestMatcher requestMatcher : this .requestMatchers ) {
108
+ requestMatcher .match (this );
109
+ }
110
+
111
+ return this .responseCreator .createResponse (this );
87
112
}
88
113
89
- public OutputStream getBody () throws IOException {
90
- return bodyStream ;
114
+ // ResponseActions implementation
115
+
116
+ public ResponseActions andExpect (RequestMatcher requestMatcher ) {
117
+ Assert .notNull (requestMatcher , "RequestMatcher is required" );
118
+ this .requestMatchers .add (requestMatcher );
119
+ return this ;
91
120
}
92
121
93
- public String getBodyContent () throws IOException {
94
- return bodyStream .toString ("UTF-8" );
122
+ public void andRespond (ResponseCreator responseCreator ) {
123
+ Assert .notNull (responseCreator , "ResponseCreator is required" );
124
+ this .responseCreator = responseCreator ;
95
125
}
96
126
97
- public ClientHttpResponse execute () throws IOException {
98
- if (!requestMatchers .isEmpty ()) {
99
- for (RequestMatcher requestMatcher : requestMatchers ) {
100
- requestMatcher .match (this );
101
- }
102
- } else {
103
- throw new AssertionError ("Unexpected execute()" );
127
+ @ Override
128
+ public String toString () {
129
+ StringBuilder sb = new StringBuilder ();
130
+ if (this .httpMethod != null ) {
131
+ sb .append (this .httpMethod );
104
132
}
105
-
106
- if (responseCreator != null ) {
107
- return responseCreator .createResponse (this );
108
- } else {
109
- return null ;
133
+ if (this .uri != null ) {
134
+ sb .append (" " ).append (this .uri );
135
+ }
136
+ if (!this .httpHeaders .isEmpty ()) {
137
+ sb .append (", headers : " ).append (this .httpHeaders );
138
+ }
139
+ if (sb .length () == 0 ) {
140
+ sb .append ("Not yet initialized" );
110
141
}
142
+ return sb .toString ();
111
143
}
112
144
113
145
}
0 commit comments