Skip to content

Commit

Permalink
Add example test for UrlFetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerjou Cheng committed Oct 19, 2016
1 parent 2216cd8 commit 08a5501
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
7 changes: 7 additions & 0 deletions unittests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-war-plugin-version>3.0.0</maven-war-plugin-version>
<maven-compiler-plugin-version>2.5.1</maven-compiler-plugin-version>
<google-api-client.version>1.22.0</google-api-client.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -66,6 +67,12 @@
<version>${appengine.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-appengine</artifactId>
<version>${google-api-client.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
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);
}
}

0 comments on commit 08a5501

Please sign in to comment.