Skip to content

Commit f2a628f

Browse files
author
Jerjou Cheng
committed
Add example test for UrlFetch
1 parent 8539683 commit f2a628f

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

unittests/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<maven-war-plugin-version>3.0.0</maven-war-plugin-version>
2424
<maven-compiler-plugin-version>2.5.1</maven-compiler-plugin-version>
25+
<google-api-client.version>1.22.0</google-api-client.version>
2526
</properties>
2627

2728
<dependencies>
@@ -66,6 +67,12 @@
6667
<version>${appengine.sdk.version}</version>
6768
<scope>test</scope>
6869
</dependency>
70+
<dependency>
71+
<groupId>com.google.api-client</groupId>
72+
<artifactId>google-api-client-appengine</artifactId>
73+
<version>${google-api-client.version}</version>
74+
<scope>test</scope>
75+
</dependency>
6976
</dependencies>
7077

7178
<build>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.appengine.samples;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
22+
import com.google.appengine.tools.development.testing.LocalURLFetchServiceTestConfig;
23+
import com.google.api.client.http.LowLevelHttpRequest;
24+
import com.google.api.client.http.LowLevelHttpResponse;
25+
import com.google.api.client.testing.http.MockHttpTransport;
26+
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
27+
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
28+
29+
import java.io.IOException;
30+
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
import com.google.api.client.http.HttpRequestFactory;
35+
import com.google.api.client.http.GenericUrl;
36+
import com.google.api.client.http.HttpResponse;
37+
38+
public class LocalUrlFetchTest {
39+
private final LocalServiceTestHelper helper =
40+
new LocalServiceTestHelper(new LocalURLFetchServiceTestConfig());
41+
42+
@Before
43+
public void setUp() {
44+
helper.setUp();
45+
}
46+
47+
@After
48+
public void tearDown() {
49+
helper.tearDown();
50+
}
51+
52+
@Test
53+
public void testMockUrlFetch() throws IOException {
54+
// See http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
55+
MockHttpTransport mockHttpTransport = new MockHttpTransport() {
56+
@Override
57+
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
58+
assertEquals(method, "GET");
59+
assertEquals(url, "http://foo.bar");
60+
61+
return new MockLowLevelHttpRequest() {
62+
@Override
63+
public LowLevelHttpResponse execute() throws IOException {
64+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
65+
response.setStatusCode(234);
66+
return response;
67+
}
68+
};
69+
}
70+
};
71+
72+
HttpRequestFactory requestFactory = mockHttpTransport.createRequestFactory();
73+
HttpResponse response = requestFactory.buildGetRequest(new GenericUrl("http://foo.bar"))
74+
.execute();
75+
assertEquals(response.getStatusCode(), 234);
76+
}
77+
}

0 commit comments

Comments
 (0)