[go: nahoru, domu]

Skip to content

Commit

Permalink
Preserve lowLevelHttpRequest, so that it's available to tests.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pawelz authored and mattwhisenhunt committed Apr 11, 2018
1 parent 8fda4a1 commit 8b59272
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
5 changes: 5 additions & 0 deletions google-http-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ public LowLevelHttpRequest buildRequest(String method, String url) throws IOExce
if (lowLevelHttpRequest != null) {
return lowLevelHttpRequest;
}
MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url);
lowLevelHttpRequest = new MockLowLevelHttpRequest(url);
if (lowLevelHttpResponse != null) {
request.setResponse(lowLevelHttpResponse);
lowLevelHttpRequest.setResponse(lowLevelHttpResponse);
}
return request;
return lowLevelHttpRequest;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.api.client.testing.http;

import static com.google.common.truth.Truth.assertThat;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;

import junit.framework.TestCase;

/**
* Tests {@link MockHttpTransport}.
*
* @author Paweł Zuzelski
*/
public final class MockHttpTransportTest extends TestCase {
// The purpose of this test is to verify, that the actual lowLevelHttpRequest used is preserved
// so that the content of the request can be verified in tests.
public void testBuildGetRequest_preservesLoLevelHttpRequest() throws Exception {
MockHttpTransport httpTransport = new MockHttpTransport();
GenericUrl url = new GenericUrl("http://example.org");
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
HttpRequest request = requestFactory.buildGetRequest(url);
request.getHeaders().set("foo", "bar");
Object unusedOnlyInspectingSideEffects = request.execute();
MockLowLevelHttpRequest actualRequest = httpTransport.getLowLevelHttpRequest();
assertThat(actualRequest.getHeaders()).containsKey("foo");
assertThat(actualRequest.getHeaders().get("foo")).containsExactly("bar");
}
}

6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.40</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
Expand Down

0 comments on commit 8b59272

Please sign in to comment.