From 08a55012fbbf09a31d455e6445c35d118c13edca Mon Sep 17 00:00:00 2001 From: Jerjou Cheng Date: Wed, 19 Oct 2016 11:42:52 -0700 Subject: [PATCH] Add example test for UrlFetch --- unittests/pom.xml | 7 ++ .../appengine/samples/LocalUrlFetchTest.java | 77 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 unittests/src/test/java/com/google/appengine/samples/LocalUrlFetchTest.java diff --git a/unittests/pom.xml b/unittests/pom.xml index 7b59f008c5d..37ef803e780 100644 --- a/unittests/pom.xml +++ b/unittests/pom.xml @@ -22,6 +22,7 @@ UTF-8 3.0.0 2.5.1 + 1.22.0 @@ -66,6 +67,12 @@ ${appengine.sdk.version} test + + com.google.api-client + google-api-client-appengine + ${google-api-client.version} + test + diff --git a/unittests/src/test/java/com/google/appengine/samples/LocalUrlFetchTest.java b/unittests/src/test/java/com/google/appengine/samples/LocalUrlFetchTest.java new file mode 100644 index 00000000000..07a2306311a --- /dev/null +++ b/unittests/src/test/java/com/google/appengine/samples/LocalUrlFetchTest.java @@ -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); + } +}