Skip to content

Commit 8b59272

Browse files
pawelzmattwhisenhunt
authored andcommitted
Preserve lowLevelHttpRequest, so that it's available to tests.
When lowLevelHttpRequest is not set via MockHttpTransport.Builder, buildRequest() creates a new instance of MockLowLevelHttpTransport. Due to what seems to be a bug, the newly created MLLHT wasn't saved to MHT instance. That means, getLowLevelHttpRequest() called on such instance returns null, making it impossible to inspect actual request being sent. For example, this bug made it impossible to verify headers of outbound HTTP requests.
1 parent 8fda4a1 commit 8b59272

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

google-http-client/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@
150150
<artifactId>junit</artifactId>
151151
<scope>test</scope>
152152
</dependency>
153+
<dependency>
154+
<groupId>com.google.truth</groupId>
155+
<artifactId>truth</artifactId>
156+
<scope>test</scope>
157+
</dependency>
153158
<dependency>
154159
<groupId>org.mockito</groupId>
155160
<artifactId>mockito-all</artifactId>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ public LowLevelHttpRequest buildRequest(String method, String url) throws IOExce
8181
if (lowLevelHttpRequest != null) {
8282
return lowLevelHttpRequest;
8383
}
84-
MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url);
84+
lowLevelHttpRequest = new MockLowLevelHttpRequest(url);
8585
if (lowLevelHttpResponse != null) {
86-
request.setResponse(lowLevelHttpResponse);
86+
lowLevelHttpRequest.setResponse(lowLevelHttpResponse);
8787
}
88-
return request;
88+
return lowLevelHttpRequest;
8989
}
9090

9191
/**
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2013 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.testing.http;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
19+
import com.google.api.client.http.GenericUrl;
20+
import com.google.api.client.http.HttpRequest;
21+
import com.google.api.client.http.HttpRequestFactory;
22+
23+
import junit.framework.TestCase;
24+
25+
/**
26+
* Tests {@link MockHttpTransport}.
27+
*
28+
* @author Paweł Zuzelski
29+
*/
30+
public final class MockHttpTransportTest extends TestCase {
31+
// The purpose of this test is to verify, that the actual lowLevelHttpRequest used is preserved
32+
// so that the content of the request can be verified in tests.
33+
public void testBuildGetRequest_preservesLoLevelHttpRequest() throws Exception {
34+
MockHttpTransport httpTransport = new MockHttpTransport();
35+
GenericUrl url = new GenericUrl("http://example.org");
36+
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
37+
HttpRequest request = requestFactory.buildGetRequest(url);
38+
request.getHeaders().set("foo", "bar");
39+
Object unusedOnlyInspectingSideEffects = request.execute();
40+
MockLowLevelHttpRequest actualRequest = httpTransport.getLowLevelHttpRequest();
41+
assertThat(actualRequest.getHeaders()).containsKey("foo");
42+
assertThat(actualRequest.getHeaders().get("foo")).containsExactly("bar");
43+
}
44+
}
45+

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@
133133
<artifactId>junit</artifactId>
134134
<version>4.8.2</version>
135135
</dependency>
136+
<dependency>
137+
<groupId>com.google.truth</groupId>
138+
<artifactId>truth</artifactId>
139+
<version>0.40</version>
140+
<scope>test</scope>
141+
</dependency>
136142
<dependency>
137143
<groupId>com.google.appengine</groupId>
138144
<artifactId>appengine-api-1.0-sdk</artifactId>

0 commit comments

Comments
 (0)