|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 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 | + * https://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.tools.remoteapi; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertThrows; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 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 | +import com.google.appengine.tools.remoteapi.AppEngineClient.Response; |
| 29 | +import com.google.appengine.tools.remoteapi.testing.StubCredential; |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Arrays; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.junit.runners.JUnit4; |
| 35 | + |
| 36 | +/** |
| 37 | + * Test for {@link OAuthClient}. |
| 38 | + */ |
| 39 | +@RunWith(JUnit4.class) |
| 40 | +public class OAuthClientTest { |
| 41 | + |
| 42 | + private static final String APP_ID = "appid"; |
| 43 | + |
| 44 | + private static class SimpleHttpTransport extends MockHttpTransport { |
| 45 | + private final byte[] responseBytes; |
| 46 | + |
| 47 | + private String lastMethod = null; |
| 48 | + private String lastUrl = null; |
| 49 | + |
| 50 | + SimpleHttpTransport(byte[] responseBytes) { |
| 51 | + this.responseBytes = responseBytes; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { |
| 56 | + lastMethod = method; |
| 57 | + lastUrl = url; |
| 58 | + return new MockLowLevelHttpRequest() { |
| 59 | + @Override |
| 60 | + public LowLevelHttpResponse execute() throws IOException { |
| 61 | + MockLowLevelHttpResponse response = new MockLowLevelHttpResponse(); |
| 62 | + response.setContent(Arrays.copyOf(responseBytes, responseBytes.length)); |
| 63 | + return response; |
| 64 | + } |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + String getLastMethod() { |
| 69 | + return lastMethod; |
| 70 | + } |
| 71 | + |
| 72 | + String getLastUrl() { |
| 73 | + return lastUrl; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testConstructor() { |
| 79 | + RemoteApiOptions noOAuthCredential = new RemoteApiOptions() |
| 80 | + .credentials("email", "password") |
| 81 | + .httpTransport(new SimpleHttpTransport(new byte[] {})); |
| 82 | + assertThrows(IllegalArgumentException.class, () -> new OAuthClient(noOAuthCredential, APP_ID)); |
| 83 | + |
| 84 | + RemoteApiOptions noHttpTransport = new RemoteApiOptions() |
| 85 | + .oauthCredential(new StubCredential()); |
| 86 | + assertThrows(IllegalStateException.class, () -> new OAuthClient(noHttpTransport, APP_ID)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testGet() throws Exception { |
| 91 | + byte[] expectedResponseBytes = new byte[] {42}; |
| 92 | + |
| 93 | + SimpleHttpTransport transport = new SimpleHttpTransport(expectedResponseBytes); |
| 94 | + RemoteApiOptions options = new RemoteApiOptions() |
| 95 | + .server("example.com", 8080) |
| 96 | + .oauthCredential(new StubCredential()) |
| 97 | + .httpTransport(transport); |
| 98 | + |
| 99 | + Response response = new OAuthClient(options, APP_ID) |
| 100 | + .get("/foo"); |
| 101 | + assertTrue(Arrays.equals(expectedResponseBytes, response.getBodyAsBytes())); |
| 102 | + assertEquals("GET", transport.getLastMethod()); |
| 103 | + assertEquals("http://example.com:8080/foo", transport.getLastUrl()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testPost() throws Exception { |
| 108 | + byte[] expectedResponseBytes = new byte[] {42}; |
| 109 | + |
| 110 | + SimpleHttpTransport transport = new SimpleHttpTransport(expectedResponseBytes); |
| 111 | + RemoteApiOptions options = new RemoteApiOptions() |
| 112 | + .server("example.com", 443) |
| 113 | + .oauthCredential(new StubCredential()) |
| 114 | + .httpTransport(transport); |
| 115 | + |
| 116 | + Response response = new OAuthClient(options, APP_ID) |
| 117 | + .post("/foo", "application/json", new byte[]{1}); |
| 118 | + assertTrue(Arrays.equals(expectedResponseBytes, response.getBodyAsBytes())); |
| 119 | + assertEquals("POST", transport.getLastMethod()); |
| 120 | + assertEquals("https://example.com:443/foo", transport.getLastUrl()); |
| 121 | + } |
| 122 | +} |
0 commit comments