-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jerjou Cheng
committed
Oct 19, 2016
1 parent
2216cd8
commit 08a5501
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
unittests/src/test/java/com/google/appengine/samples/LocalUrlFetchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* 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.appengine.samples; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import com.google.appengine.tools.development.testing.LocalServiceTestHelper; | ||
import com.google.appengine.tools.development.testing.LocalURLFetchServiceTestConfig; | ||
import com.google.api.client.http.LowLevelHttpRequest; | ||
import com.google.api.client.http.LowLevelHttpResponse; | ||
import com.google.api.client.testing.http.MockHttpTransport; | ||
import com.google.api.client.testing.http.MockLowLevelHttpRequest; | ||
import com.google.api.client.testing.http.MockLowLevelHttpResponse; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import com.google.api.client.http.HttpRequestFactory; | ||
import com.google.api.client.http.GenericUrl; | ||
import com.google.api.client.http.HttpResponse; | ||
|
||
public class LocalUrlFetchTest { | ||
private final LocalServiceTestHelper helper = | ||
new LocalServiceTestHelper(new LocalURLFetchServiceTestConfig()); | ||
|
||
@Before | ||
public void setUp() { | ||
helper.setUp(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
helper.tearDown(); | ||
} | ||
|
||
@Test | ||
public void testMockUrlFetch() throws IOException { | ||
// See http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing | ||
MockHttpTransport mockHttpTransport = new MockHttpTransport() { | ||
@Override | ||
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { | ||
assertEquals(method, "GET"); | ||
assertEquals(url, "http://foo.bar"); | ||
|
||
return new MockLowLevelHttpRequest() { | ||
@Override | ||
public LowLevelHttpResponse execute() throws IOException { | ||
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse(); | ||
response.setStatusCode(234); | ||
return response; | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
HttpRequestFactory requestFactory = mockHttpTransport.createRequestFactory(); | ||
HttpResponse response = requestFactory.buildGetRequest(new GenericUrl("http://foo.bar")) | ||
.execute(); | ||
assertEquals(response.getStatusCode(), 234); | ||
} | ||
} |