|
| 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