Skip to content

Commit 8cb9ad7

Browse files
committed
MOE sync - Test HttpContent
1 parent a302a0d commit 8cb9ad7

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

google-http-client/src/main/java/com/google/api/client/testing/http/MockHttpTransport.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public class MockHttpTransport extends HttpTransport {
4141
/** Supported HTTP methods or {@code null} to specify that all methods are supported. */
4242
private Set<String> supportedMethods;
4343

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+
4451
public MockHttpTransport() {
4552
}
4653

@@ -51,6 +58,7 @@ public MockHttpTransport() {
5158
*/
5259
protected MockHttpTransport(Builder builder) {
5360
supportedMethods = builder.supportedMethods;
61+
lowLevelHttpRequest = builder.lowLevelHttpRequest;
5462
}
5563

5664
@Override
@@ -61,7 +69,9 @@ public boolean supportsMethod(String method) throws IOException {
6169
@Override
6270
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
6371
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;
6575
}
6676

6777
/**
@@ -72,6 +82,16 @@ public final Set<String> getSupportedMethods() {
7282
return supportedMethods == null ? null : Collections.unmodifiableSet(supportedMethods);
7383
}
7484

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+
7595
/**
7696
* Returns an instance of a new builder.
7797
*
@@ -97,6 +117,13 @@ public static class Builder {
97117
/** Supported HTTP methods or {@code null} to specify that all methods are supported. */
98118
Set<String> supportedMethods;
99119

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+
100127
protected Builder() {
101128
}
102129

@@ -119,5 +146,27 @@ public final Builder setSupportedMethods(Set<String> supportedMethods) {
119146
this.supportedMethods = supportedMethods;
120147
return this;
121148
}
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+
}
122171
}
123172
}

google-http-client/src/main/java/com/google/api/client/testing/http/MockLowLevelHttpResponse.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,42 @@ public MockLowLevelHttpResponse addHeader(String name, String value) {
8989
* @param stringContent content string or {@code null} for none
9090
*/
9191
public MockLowLevelHttpResponse setContent(String stringContent) {
92-
if (stringContent == null) {
93-
content = null;
94-
setContentLength(0);
95-
} else {
96-
byte[] bytes = StringUtils.getBytesUtf8(stringContent);
97-
content = new TestableByteArrayInputStream(bytes);
98-
setContentLength(bytes.length);
92+
return stringContent == null
93+
? setZeroContent()
94+
: setContent(StringUtils.getBytesUtf8(stringContent));
95+
}
96+
97+
/**
98+
* Sets the response content to the given byte array.
99+
*
100+
* @param byteContent content byte array, or {@code null} for none.
101+
*
102+
* <p>
103+
* If the byte array is {@code null}, the method invokes {@link #setZeroContent}.
104+
* Otherwise, {@code byteContent} is wrapped in a {@link TestableByteArrayInputStream}
105+
* and becomes this {@link MockLowLevelHttpResponse}'s contents.
106+
* </p>
107+
*
108+
* @since 1.18
109+
*/
110+
public MockLowLevelHttpResponse setContent(byte[] byteContent) {
111+
if (byteContent == null) {
112+
return setZeroContent();
99113
}
114+
content = new TestableByteArrayInputStream(byteContent);
115+
setContentLength(byteContent.length);
116+
return this;
117+
}
118+
119+
/**
120+
* Sets the content to {@code null} and the content length to 0. Note that
121+
* the result will have a content length header whose value is 0.
122+
*
123+
* @since 1.18
124+
*/
125+
public MockLowLevelHttpResponse setZeroContent() {
126+
content = null;
127+
setContentLength(0);
100128
return this;
101129
}
102130

0 commit comments

Comments
 (0)