@@ -48,6 +48,14 @@ public class MockHttpTransport extends HttpTransport {
48
48
* */
49
49
private MockLowLevelHttpRequest lowLevelHttpRequest ;
50
50
51
+ /**
52
+ * The {@link MockLowLevelHttpResponse} to be returned when this {@link MockHttpTransport}
53
+ * executes the associated request. Note that this field is ignored if the caller provided
54
+ * a non-{@code null} {@link MockLowLevelHttpRequest} with this {@link MockHttpTransport}
55
+ * was built.
56
+ */
57
+ private MockLowLevelHttpResponse lowLevelHttpResponse ;
58
+
51
59
public MockHttpTransport () {
52
60
}
53
61
@@ -59,6 +67,7 @@ public MockHttpTransport() {
59
67
protected MockHttpTransport (Builder builder ) {
60
68
supportedMethods = builder .supportedMethods ;
61
69
lowLevelHttpRequest = builder .lowLevelHttpRequest ;
70
+ lowLevelHttpResponse = builder .lowLevelHttpResponse ;
62
71
}
63
72
64
73
@ Override
@@ -69,9 +78,14 @@ public boolean supportsMethod(String method) throws IOException {
69
78
@ Override
70
79
public LowLevelHttpRequest buildRequest (String method , String url ) throws IOException {
71
80
Preconditions .checkArgument (supportsMethod (method ), "HTTP method %s not supported" , method );
72
- return lowLevelHttpRequest == null
73
- ? new MockLowLevelHttpRequest (url )
74
- : lowLevelHttpRequest ;
81
+ if (lowLevelHttpRequest != null ) {
82
+ return lowLevelHttpRequest ;
83
+ }
84
+ MockLowLevelHttpRequest request = new MockLowLevelHttpRequest (url );
85
+ if (lowLevelHttpResponse != null ) {
86
+ request .setResponse (lowLevelHttpResponse );
87
+ }
88
+ return request ;
75
89
}
76
90
77
91
/**
@@ -95,8 +109,13 @@ public final MockLowLevelHttpRequest getLowLevelHttpRequest() {
95
109
/**
96
110
* Returns an instance of a new builder.
97
111
*
112
+ * <p>
113
+ * @deprecated (to be removed in the future) Use {@link Builder#Builder()} instead.
114
+ * </p>
115
+ *
98
116
* @since 1.5
99
117
*/
118
+ @ Deprecated
100
119
public static Builder builder () {
101
120
return new Builder ();
102
121
}
@@ -124,7 +143,20 @@ public static class Builder {
124
143
* */
125
144
MockLowLevelHttpRequest lowLevelHttpRequest ;
126
145
127
- protected Builder () {
146
+ /**
147
+ * The {@link MockLowLevelHttpResponse} that should be the result of the
148
+ * {@link MockLowLevelHttpRequest} to be returned by {@link #buildRequest}. Note
149
+ * that this field is ignored if the caller provides a {@link MockLowLevelHttpRequest}
150
+ * via {@link #setLowLevelHttpRequest}.
151
+ */
152
+ MockLowLevelHttpResponse lowLevelHttpResponse ;
153
+
154
+ /**
155
+ * Constructs a new {@link Builder}. Note that this constructor was {@code protected}
156
+ * in version 1.17 and its predecessors, and was made {@code public} in version
157
+ * 1.18.
158
+ */
159
+ public Builder () {
128
160
}
129
161
130
162
/** Builds a new instance of {@link MockHttpTransport}. */
@@ -152,9 +184,14 @@ public final Builder setSupportedMethods(Set<String> supportedMethods) {
152
184
* non-{@code null}. If {@code null}, {@link #buildRequest} will create a new
153
185
* {@link MockLowLevelHttpRequest} arguments.
154
186
*
187
+ * <p>Note that the user can set a low level HTTP Request only if a low level HTTP response
188
+ * has not been set on this instance.
189
+ *
155
190
* @since 1.18
156
191
*/
157
192
public final Builder setLowLevelHttpRequest (MockLowLevelHttpRequest lowLevelHttpRequest ) {
193
+ Preconditions .checkState (lowLevelHttpResponse == null ,
194
+ "Cannnot set a low level HTTP request when a low level HTTP response has been set." );
158
195
this .lowLevelHttpRequest = lowLevelHttpRequest ;
159
196
return this ;
160
197
}
@@ -168,5 +205,35 @@ public final Builder setLowLevelHttpRequest(MockLowLevelHttpRequest lowLevelHttp
168
205
public final MockLowLevelHttpRequest getLowLevelHttpRequest () {
169
206
return lowLevelHttpRequest ;
170
207
}
208
+
209
+
210
+ /**
211
+ * Sets the {@link MockLowLevelHttpResponse} that will be the result when the
212
+ * {@link MockLowLevelHttpRequest} returned by {@link #buildRequest} is executed.
213
+ * Note that the response can be set only the caller has not provided a
214
+ * {@link MockLowLevelHttpRequest} via {@link #setLowLevelHttpRequest}.
215
+ *
216
+ * @throws IllegalStateException if the caller has already set a {@link LowLevelHttpRequest}
217
+ * in this instance
218
+ *
219
+ * @since 1.18
220
+ */
221
+ public final Builder setLowLevelHttpResponse (MockLowLevelHttpResponse lowLevelHttpResponse ) {
222
+ Preconditions .checkState (lowLevelHttpRequest == null ,
223
+ "Cannot set a low level HTTP response when a low level HTTP request has been set." );
224
+ this .lowLevelHttpResponse = lowLevelHttpResponse ;
225
+ return this ;
226
+ }
227
+
228
+
229
+ /**
230
+ * Returns the {@link MockLowLevelHttpResponse} that is associated with this {@link Builder},
231
+ * or {@code null} if no such instance exists.
232
+ *
233
+ * @since 1.18
234
+ */
235
+ MockLowLevelHttpResponse getLowLevelHttpResponse () {
236
+ return this .lowLevelHttpResponse ;
237
+ }
171
238
}
172
239
}
0 commit comments