|
15 | 15 |
|
16 | 16 | package com.amazonaws.http; |
17 | 17 |
|
18 | | -import static org.junit.Assert.assertEquals; |
19 | | -import static org.junit.Assert.assertFalse; |
20 | | -import static org.junit.Assert.assertNull; |
21 | | -import static org.junit.Assert.assertSame; |
22 | | -import static org.junit.Assert.assertTrue; |
| 18 | +import android.util.Log; |
| 19 | +import android.util.Pair; |
23 | 20 |
|
24 | 21 | import com.amazonaws.ClientConfiguration; |
25 | 22 | import com.amazonaws.SDKGlobalConfiguration; |
|
33 | 30 | import java.io.IOException; |
34 | 31 | import java.io.InputStream; |
35 | 32 | import java.io.OutputStream; |
| 33 | +import java.net.HttpURLConnection; |
36 | 34 | import java.net.MalformedURLException; |
37 | 35 | import java.net.ProtocolException; |
38 | 36 | import java.net.URI; |
|
41 | 39 | import java.security.cert.Certificate; |
42 | 40 | import java.util.ArrayList; |
43 | 41 | import java.util.HashMap; |
| 42 | +import java.util.HashSet; |
44 | 43 | import java.util.List; |
45 | 44 | import java.util.Map; |
46 | 45 |
|
47 | 46 | import javax.net.ssl.HttpsURLConnection; |
48 | 47 | import javax.net.ssl.SSLPeerUnverifiedException; |
49 | 48 |
|
| 49 | +import static org.junit.Assert.assertEquals; |
| 50 | +import static org.junit.Assert.assertFalse; |
| 51 | +import static org.junit.Assert.assertNull; |
| 52 | +import static org.junit.Assert.assertSame; |
| 53 | +import static org.junit.Assert.assertTrue; |
| 54 | + |
50 | 55 | public class UrlHttpClientTest { |
51 | 56 |
|
52 | 57 | private ClientConfiguration conf; |
53 | | - private UrlHttpClient client; |
| 58 | + private MockUrlHttpClient client; |
54 | 59 |
|
55 | 60 | @Before |
56 | 61 | public void setup() { |
57 | 62 | conf = new ClientConfiguration(); |
58 | | - client = new UrlHttpClient(conf); |
| 63 | + client = new MockUrlHttpClient(conf); |
| 64 | + } |
59 | 65 |
|
| 66 | + @Test |
| 67 | + public void testBasicCurlBuilder() throws URISyntaxException, IOException { |
| 68 | + conf.setLogging(true); |
| 69 | + HttpRequest request = new HttpRequest("POST", new URI("https://www.test.com")); |
| 70 | + client.execute(request); |
| 71 | + |
| 72 | + assertEquals(client.getLogList().size(), 1); |
| 73 | + assertEquals(client.getLogList().get(0).first.intValue(), Log.VERBOSE); |
| 74 | + assertEquals(client.getLogList().get(0).second, "curl -X POST https://www.test.com"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testCurlBuilderWithHeaders() throws URISyntaxException, IOException { |
| 79 | + conf.setLogging(true); |
| 80 | + Map<String, String> headers = new HashMap<String, String>(); |
| 81 | + headers.put("key1", "value1"); |
| 82 | + headers.put("key2", "value2"); |
| 83 | + |
| 84 | + HashSet<String> expectedCurlHeaders = new HashSet<String>(); |
| 85 | + for (Map.Entry<String, String> entry : headers.entrySet()) { |
| 86 | + expectedCurlHeaders.add("\"" + entry.getKey() + ":" + entry.getValue() + "\""); |
| 87 | + } |
| 88 | + |
| 89 | + HttpRequest request = new HttpRequest("POST", new URI("https://www.test.com"), headers, |
| 90 | + null /* stream */); |
| 91 | + client.execute(request); |
| 92 | + |
| 93 | + assertEquals(client.getLogList().size(), 1); |
| 94 | + assertEquals(client.getLogList().get(0).first.intValue(), Log.VERBOSE); |
| 95 | + |
| 96 | + String[] parts = client.getLogList().get(0).second.split(" "); |
| 97 | + assertEquals(parts.length, 8); |
| 98 | + assertEquals(parts[0], "curl"); |
| 99 | + assertEquals(parts[1], "-X"); |
| 100 | + assertEquals(parts[2], "POST"); |
| 101 | + assertEquals(parts[3], "-H"); |
| 102 | + assertTrue(expectedCurlHeaders.contains(parts[4])); |
| 103 | + expectedCurlHeaders.remove(parts[4]); |
| 104 | + assertEquals(parts[5], "-H"); |
| 105 | + assertTrue(expectedCurlHeaders.contains(parts[6])); |
| 106 | + expectedCurlHeaders.remove(parts[6]); |
| 107 | + assertTrue(expectedCurlHeaders.isEmpty()); |
| 108 | + assertEquals(parts[7], "https://www.test.com"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testCurlBuilderWithData() throws URISyntaxException, IOException { |
| 113 | + conf.setLogging(true); |
| 114 | + String dataString = "content"; |
| 115 | + byte[] data = dataString.getBytes("UTF-8"); |
| 116 | + |
| 117 | + Map<String, String> headers = new HashMap<String, String>(); |
| 118 | + headers.put(HttpHeader.CONTENT_LENGTH, String.valueOf(data.length)); |
| 119 | + |
| 120 | + InputStream stream = new ByteArrayInputStream(data); |
| 121 | + HttpRequest request = new HttpRequest("POST", new URI("https://www.test.com"), |
| 122 | + headers, stream); |
| 123 | + client.execute(request); |
| 124 | + |
| 125 | + assertEquals(client.getLogList().size(), 1); |
| 126 | + assertEquals(client.getLogList().get(0).first.intValue(), Log.VERBOSE); |
| 127 | + assertEquals(client.getLogList().get(0).second, |
| 128 | + "curl -X POST -d '" + dataString + "' https://www.test.com"); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testOverflowInCurl() throws URISyntaxException, IOException { |
| 133 | + conf.setLogging(true); |
| 134 | + final long tooManyBytes = Integer.MAX_VALUE + 1L; |
| 135 | + InputStream stream = new ByteArrayInputStream("content".getBytes("UTF-8")); |
| 136 | + Map<String, String> headers = new HashMap<String, String>(); |
| 137 | + headers.put(HttpHeader.CONTENT_LENGTH, String.valueOf(tooManyBytes)); |
| 138 | + HttpRequest request = new HttpRequest("POST", new URI("https://www.test.com"), |
| 139 | + null /* headers */, stream); |
| 140 | + client.execute(request); |
| 141 | + |
| 142 | + assertEquals(client.getLogList().size(), 1); |
| 143 | + assertEquals(client.getLogList().get(0).first.intValue(), Log.VERBOSE); |
| 144 | + assertEquals(client.getLogList().get(0).second, |
| 145 | + "Failed to create curl, content too long"); |
60 | 146 | } |
61 | 147 |
|
62 | 148 | @Test |
@@ -269,3 +355,28 @@ public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException { |
269 | 355 | } |
270 | 356 |
|
271 | 357 | } |
| 358 | + |
| 359 | +class MockUrlHttpClient extends UrlHttpClient { |
| 360 | + |
| 361 | + private ArrayList<Pair<Integer, String>> mLogList = new ArrayList<Pair<Integer, String>>(); |
| 362 | + |
| 363 | + public MockUrlHttpClient(ClientConfiguration config) { |
| 364 | + super(config); |
| 365 | + } |
| 366 | + |
| 367 | + public ArrayList<Pair<Integer, String>> getLogList() { |
| 368 | + return mLogList; |
| 369 | + } |
| 370 | + |
| 371 | + @Override |
| 372 | + protected void printToLog(int priority, String message) { |
| 373 | + mLogList.add(new Pair<Integer, String>(priority, message)); |
| 374 | + } |
| 375 | + |
| 376 | + @Override |
| 377 | + protected HttpURLConnection getUrlConnection(URL url) throws IOException { |
| 378 | + MockHttpURLConnection connection = new MockHttpURLConnection(url); |
| 379 | + connection.setOutputStream(new ByteArrayOutputStream()); |
| 380 | + return connection; |
| 381 | + } |
| 382 | +} |
0 commit comments