@@ -41,6 +41,13 @@ public class MockHttpTransport extends HttpTransport {
41
41
/** Supported HTTP methods or {@code null} to specify that all methods are supported. */
42
42
private Set <String > supportedMethods ;
43
43
44
+ /**
45
+ * The {@link MockLowLevelHttpRequest} to be returned by {@link #buildRequest}. If
46
+ * this field is {@code null}, {@link #buildRequest} will create a new instance
47
+ * from its arguments.
48
+ * */
49
+ private MockLowLevelHttpRequest lowLevelHttpRequest ;
50
+
44
51
public MockHttpTransport () {
45
52
}
46
53
@@ -51,6 +58,7 @@ public MockHttpTransport() {
51
58
*/
52
59
protected MockHttpTransport (Builder builder ) {
53
60
supportedMethods = builder .supportedMethods ;
61
+ lowLevelHttpRequest = builder .lowLevelHttpRequest ;
54
62
}
55
63
56
64
@ Override
@@ -61,7 +69,9 @@ public boolean supportsMethod(String method) throws IOException {
61
69
@ Override
62
70
public LowLevelHttpRequest buildRequest (String method , String url ) throws IOException {
63
71
Preconditions .checkArgument (supportsMethod (method ), "HTTP method %s not supported" , method );
64
- return new MockLowLevelHttpRequest (url );
72
+ return lowLevelHttpRequest == null
73
+ ? new MockLowLevelHttpRequest (url )
74
+ : lowLevelHttpRequest ;
65
75
}
66
76
67
77
/**
@@ -72,6 +82,16 @@ public final Set<String> getSupportedMethods() {
72
82
return supportedMethods == null ? null : Collections .unmodifiableSet (supportedMethods );
73
83
}
74
84
85
+ /**
86
+ * Returns the {@link MockLowLevelHttpRequest} that is associated with this {@link Builder},
87
+ * or {@code null} if no such instance exists.
88
+ *
89
+ * @since 1.18
90
+ */
91
+ public final MockLowLevelHttpRequest getLowLevelHttpRequest () {
92
+ return lowLevelHttpRequest ;
93
+ }
94
+
75
95
/**
76
96
* Returns an instance of a new builder.
77
97
*
@@ -97,6 +117,13 @@ public static class Builder {
97
117
/** Supported HTTP methods or {@code null} to specify that all methods are supported. */
98
118
Set <String > supportedMethods ;
99
119
120
+ /**
121
+ * The {@link MockLowLevelHttpRequest} to be returned by {@link #buildRequest}. If
122
+ * this field is {@code null}, {@link #buildRequest} will create a new instance
123
+ * from its arguments.
124
+ * */
125
+ MockLowLevelHttpRequest lowLevelHttpRequest ;
126
+
100
127
protected Builder () {
101
128
}
102
129
@@ -119,5 +146,27 @@ public final Builder setSupportedMethods(Set<String> supportedMethods) {
119
146
this .supportedMethods = supportedMethods ;
120
147
return this ;
121
148
}
149
+
150
+ /**
151
+ * Sets the {@link MockLowLevelHttpRequest} that will be returned by {@link #buildRequest}, if
152
+ * non-{@code null}. If {@code null}, {@link #buildRequest} will create a new
153
+ * {@link MockLowLevelHttpRequest} arguments.
154
+ *
155
+ * @since 1.18
156
+ */
157
+ public final Builder setLowLevelHttpRequest (MockLowLevelHttpRequest lowLevelHttpRequest ) {
158
+ this .lowLevelHttpRequest = lowLevelHttpRequest ;
159
+ return this ;
160
+ }
161
+
162
+ /**
163
+ * Returns the {@link MockLowLevelHttpRequest} that is associated with this {@link Builder},
164
+ * or {@code null} if no such instance exists.
165
+ *
166
+ * @since 1.18
167
+ */
168
+ public final MockLowLevelHttpRequest getLowLevelHttpRequest () {
169
+ return lowLevelHttpRequest ;
170
+ }
122
171
}
123
172
}
0 commit comments