LEFT | RIGHT |
| 1 /* |
| 2 * Copyright (c) 2010 Google Inc. |
| 3 * |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not u
se this file except |
| 5 * in compliance with the License. You may obtain a copy of the License at |
| 6 * |
| 7 * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 * |
| 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License |
| 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express |
| 11 * or implied. See the License for the specific language governing permissions a
nd limitations under |
| 12 * the License. |
| 13 */ |
| 14 |
| 15 package com.google.api.client.http; |
| 16 |
| 17 import com.google.api.client.http.LogContentTest.Recorder; |
| 18 import com.google.api.client.testing.http.HttpTesting; |
| 19 import com.google.api.client.testing.http.MockHttpTransport; |
1 import com.google.api.client.testing.http.MockLowLevelHttpRequest; | 20 import com.google.api.client.testing.http.MockLowLevelHttpRequest; |
2 import com.google.api.client.testing.http.MockLowLevelHttpResponse; | 21 import com.google.api.client.testing.http.MockLowLevelHttpResponse; |
3 import com.google.api.client.util.Key; | 22 import com.google.api.client.util.Key; |
| 23 import com.google.api.client.util.StringUtils; |
4 | 24 |
5 import junit.framework.TestCase; | 25 import junit.framework.TestCase; |
6 | 26 |
| 27 import java.io.ByteArrayInputStream; |
7 import java.io.ByteArrayOutputStream; | 28 import java.io.ByteArrayOutputStream; |
8 import java.io.IOException; | 29 import java.io.IOException; |
| 30 import java.io.InputStream; |
9 import java.util.Arrays; | 31 import java.util.Arrays; |
10 import java.util.logging.Level; | 32 import java.util.logging.Level; |
11 | 33 |
| 34 /** |
| 35 * Tests {@link HttpResponse}. |
| 36 * |
| 37 * @author Yaniv Inbar |
| 38 */ |
| 39 public class HttpResponseTest extends TestCase { |
| 40 |
| 41 public HttpResponseTest() { |
| 42 } |
| 43 |
| 44 public HttpResponseTest(String name) { |
| 45 super(name); |
| 46 } |
| 47 |
| 48 public void testParseAsString_none() throws IOException { |
| 49 HttpTransport transport = new MockHttpTransport(); |
| 50 HttpRequest request = |
| 51 transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENE
RIC_URL); |
| 52 HttpResponse response = request.execute(); |
| 53 assertEquals("", response.parseAsString()); |
| 54 } |
| 55 |
| 56 private static final String SAMPLE = "123\u05D9\u05e0\u05D9\u05D1"; |
| 57 private static final String SAMPLE2 = "123abc"; |
| 58 |
| 59 public void testParseAsString_utf8() throws IOException { |
| 60 HttpTransport transport = new MockHttpTransport() { |
| 61 @Override |
| 62 public LowLevelHttpRequest buildGetRequest(String url) throws IOException
{ |
| 63 return new MockLowLevelHttpRequest() { |
| 64 @Override |
| 65 public LowLevelHttpResponse execute() throws IOException { |
| 66 MockLowLevelHttpResponse result = new MockLowLevelHttpResponse(); |
| 67 result.setContentType("application/json; charset=UTF-8"); |
| 68 result.setContent(SAMPLE); |
| 69 return result; |
| 70 } |
| 71 }; |
| 72 } |
| 73 }; |
| 74 HttpRequest request = |
| 75 transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENE
RIC_URL); |
| 76 HttpResponse response = request.execute(); |
| 77 assertEquals(SAMPLE, response.parseAsString()); |
| 78 } |
| 79 |
| 80 public void testParseAsString_noContentType() throws IOException { |
| 81 HttpTransport transport = new MockHttpTransport() { |
| 82 @Override |
| 83 public LowLevelHttpRequest buildGetRequest(String url) throws IOException
{ |
| 84 return new MockLowLevelHttpRequest() { |
| 85 @Override |
| 86 public LowLevelHttpResponse execute() throws IOException { |
| 87 MockLowLevelHttpResponse result = new MockLowLevelHttpResponse(); |
| 88 result.setContent(SAMPLE2); |
| 89 return result; |
| 90 } |
| 91 }; |
| 92 } |
| 93 }; |
| 94 HttpRequest request = |
| 95 transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENE
RIC_URL); |
| 96 HttpResponse response = request.execute(); |
| 97 assertEquals(SAMPLE2, response.parseAsString()); |
| 98 } |
| 99 |
| 100 public void testParseCharset() { |
| 101 assertEquals("UTF-8", HttpResponse.parseCharset("application/json; charset=U
TF-8")); |
| 102 assertEquals( |
| 103 "a b c", HttpResponse.parseCharset("application/json;a=a; charset=a b c
; b=b")); |
| 104 assertEquals( |
| 105 "a b c", HttpResponse.parseCharset("application/json;a=a; cHaRsEt=a b c
; b=b")); |
| 106 } |
| 107 |
| 108 public static class MyHeaders extends HttpHeaders { |
| 109 |
| 110 @Key |
| 111 public String foo; |
| 112 |
| 113 @Key |
| 114 public Object obj; |
| 115 |
| 116 @Key |
| 117 String[] r; |
| 118 } |
| 119 |
| 120 static final String ETAG_VALUE = "\"abc\""; |
| 121 |
| 122 public void testHeaderParsing() throws IOException { |
| 123 HttpTransport transport = new MockHttpTransport() { |
| 124 @Override |
| 125 public LowLevelHttpRequest buildGetRequest(String url) throws IOException
{ |
| 126 return new MockLowLevelHttpRequest() { |
| 127 @Override |
| 128 public LowLevelHttpResponse execute() throws IOException { |
| 129 MockLowLevelHttpResponse result = new MockLowLevelHttpResponse(); |
| 130 result.addHeader("accept", "value"); |
| 131 result.addHeader("foo", "bar"); |
| 132 result.addHeader("goo", "car"); |
| 133 result.addHeader("hoo", "dar"); |
| 134 result.addHeader("hoo", "far"); |
| 135 result.addHeader("obj", "o"); |
| 136 result.addHeader("r", "a1"); |
| 137 result.addHeader("r", "a2"); |
| 138 result.addHeader("ETAG", ETAG_VALUE); |
| 139 return result; |
| 140 } |
| 141 }; |
| 142 } |
| 143 }; |
| 144 HttpRequest request = |
| 145 transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENE
RIC_URL); |
| 146 request.setResponseHeaders(new MyHeaders()); |
| 147 HttpResponse response = request.execute(); |
| 148 assertEquals("value", response.getHeaders().getAccept()); |
| 149 assertEquals("bar", ((MyHeaders) response.getHeaders()).foo); |
| 150 assertEquals(Arrays.asList("o"), ((MyHeaders) response.getHeaders()).obj); |
| 151 assertEquals(Arrays.asList("a1", "a2"), Arrays.asList(((MyHeaders) response.
getHeaders()).r)); |
| 152 assertEquals(Arrays.asList("car"), response.getHeaders().get("goo")); |
| 153 assertEquals(Arrays.asList("dar", "far"), response.getHeaders().get("hoo")); |
| 154 assertEquals(ETAG_VALUE, response.getHeaders().getETag()); |
| 155 } |
| 156 |
| 157 public void testParseAs_noParser() throws IOException { |
| 158 HttpTransport transport = new MockHttpTransport() { |
| 159 @Override |
| 160 public LowLevelHttpRequest buildGetRequest(final String url) throws IOExce
ption { |
| 161 return new MockLowLevelHttpRequest() { |
| 162 @Override |
| 163 public LowLevelHttpResponse execute() throws IOException { |
| 164 MockLowLevelHttpResponse result = new MockLowLevelHttpResponse(); |
| 165 if (!url.equals(HttpTesting.SIMPLE_URL)) { |
| 166 result.setContentType(url.substring(HttpTesting.SIMPLE_URL.length(
))); |
| 167 } |
| 168 return result; |
| 169 } |
| 170 }; |
| 171 } |
| 172 }; |
| 173 try { |
| 174 transport.createRequestFactory() |
| 175 .buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL).execute().parseAs(Obj
ect.class); |
| 176 fail("expected " + IllegalArgumentException.class); |
| 177 } catch (IllegalArgumentException e) { |
| 178 assertEquals(e.getMessage(), "Missing Content-Type header in response"); |
| 179 } |
| 180 try { |
| 181 transport.createRequestFactory() |
| 182 .buildGetRequest(new GenericUrl(HttpTesting.SIMPLE_URL + "something"))
.execute() |
| 183 .parseAs(Object.class); |
| 184 fail("expected " + IllegalArgumentException.class); |
| 185 } catch (IllegalArgumentException e) { |
| 186 assertEquals(e.getMessage(), "No parser defined for Content-Type: somethin
g"); |
| 187 } |
| 188 } |
| 189 |
| 190 public void testDownload() throws IOException { |
| 191 HttpTransport transport = new MockHttpTransport() { |
| 192 @Override |
| 193 public LowLevelHttpRequest buildGetRequest(String url) throws IOException
{ |
| 194 return new MockLowLevelHttpRequest() { |
| 195 @Override |
| 196 public LowLevelHttpResponse execute() throws IOException { |
| 197 MockLowLevelHttpResponse result = new MockLowLevelHttpResponse(); |
| 198 result.setContentType("application/json; charset=UTF-8"); |
| 199 result.setContent(SAMPLE); |
| 200 return result; |
| 201 } |
| 202 }; |
| 203 } |
| 204 }; |
| 205 HttpRequest request = |
| 206 transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENE
RIC_URL); |
| 207 HttpResponse response = request.execute(); |
| 208 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 209 response.download(outputStream); |
| 210 assertEquals(SAMPLE, outputStream.toString("UTF-8")); |
| 211 } |
| 212 |
| 213 class DisconnectLowLevelHttpResponse extends MockLowLevelHttpResponse { |
| 214 boolean disconnectCalled; |
| 215 DisconnectByteArrayInputStream content; |
| 216 |
| 217 @Override |
| 218 public MockLowLevelHttpResponse setContent(String stringContent) { |
| 219 content = stringContent == null ? null : new DisconnectByteArrayInputStrea
m(StringUtils |
| 220 .getBytesUtf8(stringContent)); |
| 221 return this; |
| 222 } |
| 223 |
| 224 @Override |
| 225 public InputStream getContent() throws IOException { |
| 226 return content; |
| 227 } |
| 228 |
| 229 @Override |
| 230 public void disconnect() { |
| 231 disconnectCalled = true; |
| 232 } |
| 233 } |
| 234 |
| 235 class DisconnectByteArrayInputStream extends ByteArrayInputStream { |
| 236 boolean closeCalled; |
| 237 |
| 238 public DisconnectByteArrayInputStream(byte[] buf) { |
| 239 super(buf); |
| 240 } |
| 241 |
| 242 @Override |
| 243 public void close() throws IOException { |
| 244 closeCalled = true; |
| 245 } |
| 246 } |
| 247 |
| 248 public void testDisconnectWithContent() throws IOException { |
| 249 final DisconnectLowLevelHttpResponse lowLevelHttpResponse = |
| 250 new DisconnectLowLevelHttpResponse(); |
| 251 |
| 252 HttpTransport transport = new MockHttpTransport() { |
| 253 @Override |
| 254 public LowLevelHttpRequest buildGetRequest(String url) throws IOException
{ |
| 255 return new MockLowLevelHttpRequest() { |
| 256 @Override |
| 257 public LowLevelHttpResponse execute() throws IOException { |
| 258 lowLevelHttpResponse.setContentType("application/json; charset=UTF-8
"); |
| 259 lowLevelHttpResponse.setContent(SAMPLE); |
| 260 return lowLevelHttpResponse; |
| 261 } |
| 262 }; |
| 263 } |
| 264 }; |
| 265 HttpRequest request = |
| 266 transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENE
RIC_URL); |
| 267 HttpResponse response = request.execute(); |
| 268 |
| 269 assertFalse(lowLevelHttpResponse.disconnectCalled); |
| 270 assertFalse(lowLevelHttpResponse.content.closeCalled); |
| 271 response.disconnect(); |
| 272 assertTrue(lowLevelHttpResponse.disconnectCalled); |
| 273 assertTrue(lowLevelHttpResponse.content.closeCalled); |
| 274 } |
| 275 |
| 276 public void testDisconnectWithNoContent() throws IOException { |
| 277 final DisconnectLowLevelHttpResponse lowLevelHttpResponse = |
| 278 new DisconnectLowLevelHttpResponse(); |
| 279 |
| 280 HttpTransport transport = new MockHttpTransport() { |
| 281 @Override |
| 282 public LowLevelHttpRequest buildGetRequest(String url) throws IOException
{ |
| 283 return new MockLowLevelHttpRequest() { |
| 284 @Override |
| 285 public LowLevelHttpResponse execute() throws IOException { |
| 286 return lowLevelHttpResponse; |
| 287 } |
| 288 }; |
| 289 } |
| 290 }; |
| 291 HttpRequest request = |
| 292 transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENE
RIC_URL); |
| 293 HttpResponse response = request.execute(); |
| 294 |
| 295 assertFalse(lowLevelHttpResponse.disconnectCalled); |
| 296 response.disconnect(); |
| 297 assertTrue(lowLevelHttpResponse.disconnectCalled); |
| 298 } |
| 299 |
| 300 public void testContentLoggingLimitWithLoggingEnabledAndDisabled() throws IOEx
ception { |
| 301 subtestContentLoggingLimit("", 2, false); |
| 302 subtestContentLoggingLimit("A", 2, false); |
| 303 subtestContentLoggingLimit("ABC" + '\0' + "DEF", 20, true, "Total: 7 bytes",
"ABC DEF"); |
| 304 subtestContentLoggingLimit("A", 2, true, "Total: 1 byte", "A"); |
| 305 try { |
| 306 subtestContentLoggingLimit("ABC", -1, true); |
| 307 fail("Expected: " + IllegalArgumentException.class); |
| 308 } catch (IllegalArgumentException e) { |
| 309 // Expected. |
| 310 } |
| 311 subtestContentLoggingLimit("ABC", 0, true, "Total: 3 bytes"); |
| 312 subtestContentLoggingLimit("ABC", 2, true, "Total: 3 bytes (logging first 2
bytes)", "AB"); |
| 313 subtestContentLoggingLimit("ABC", 3, true, "Total: 3 bytes", "ABC"); |
| 314 subtestContentLoggingLimit("ABC", 4, true, "Total: 3 bytes", "ABC"); |
| 315 char[] a = new char[18000]; |
| 316 Arrays.fill(a, 'x'); |
| 317 String big = new String(a); |
| 318 subtestContentLoggingLimit(big, Integer.MAX_VALUE, true, "Total: 18,000 byte
s", big); |
| 319 subtestContentLoggingLimit(big, 4, true, "Total: 18,000 bytes (logging first
4 bytes)", "xxxx"); |
| 320 } |
| 321 |
| 322 public void subtestContentLoggingLimit(final String content, int contentLoggin
gLimit, |
| 323 boolean loggingEnabled, String... expectedMessages) throws IOException { |
| 324 HttpTransport transport = new MockHttpTransport() { |
| 325 @Override |
| 326 public LowLevelHttpRequest buildGetRequest(final String url) throws IOExce
ption { |
| 327 return new MockLowLevelHttpRequest() { |
| 328 @Override |
| 329 public LowLevelHttpResponse execute() throws IOException { |
| 330 MockLowLevelHttpResponse result = new MockLowLevelHttpResponse(); |
| 331 result.setContent(content); |
| 332 result.setContentType("text/plain"); |
| 333 return result; |
| 334 } |
| 335 }; |
| 336 } |
| 337 }; |
| 338 HttpTransport.LOGGER.setLevel(Level.CONFIG); |
| 339 |
| 340 HttpRequest request = |
| 341 transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENE
RIC_URL); |
| 342 request.setLoggingEnabled(loggingEnabled); |
| 343 HttpResponse response = request.execute(); |
| 344 assertEquals(loggingEnabled, response.isLoggingEnabled()); |
| 345 |
| 346 response.setContentLoggingLimit(contentLoggingLimit); |
| 347 Recorder recorder = new Recorder(); |
| 348 HttpTransport.LOGGER.addHandler(recorder); |
| 349 response.parseAsString(); |
| 350 recorder.assertMessages(expectedMessages); |
| 351 } |
| 352 } |
LEFT | RIGHT |