Skip to content

Commit 8a50094

Browse files
author
Yuval Peress
committed
Add unit tests
1 parent 1209206 commit 8a50094

File tree

2 files changed

+128
-9
lines changed

2 files changed

+128
-9
lines changed

aws-android-sdk-core/src/main/java/com/amazonaws/http/UrlHttpClient.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public HttpResponse execute(final HttpRequest request) throws IOException {
6868

6969
if (curlBuilder != null) {
7070
if (curlBuilder.isValid()) {
71-
Log.v(TAG, curlBuilder.build());
71+
printToLog(Log.VERBOSE, curlBuilder.build());
7272
} else {
73-
Log.v(TAG, "Failed to create curl, content too long");
73+
printToLog(Log.VERBOSE, "Failed to create curl, content too long");
7474
}
7575
}
7676

@@ -216,6 +216,14 @@ HttpURLConnection applyHeadersAndMethod(final HttpRequest request,
216216
return connection;
217217
}
218218

219+
protected void printToLog(int priority, String message) {
220+
Log.println(priority, TAG, message);
221+
}
222+
223+
protected HttpURLConnection getUrlConnection(URL url) throws IOException {
224+
return (HttpURLConnection) url.openConnection();
225+
}
226+
219227
private void write(InputStream is, OutputStream os, ByteBuffer curlBuffer) throws IOException {
220228
final byte[] buf = new byte[1024 * 8];
221229
int len;

aws-android-sdk-core/src/test/java/com/amazonaws/http/UrlHttpClientTest.java

Lines changed: 118 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515

1616
package com.amazonaws.http;
1717

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

2421
import com.amazonaws.ClientConfiguration;
2522
import com.amazonaws.SDKGlobalConfiguration;
@@ -33,6 +30,7 @@
3330
import java.io.IOException;
3431
import java.io.InputStream;
3532
import java.io.OutputStream;
33+
import java.net.HttpURLConnection;
3634
import java.net.MalformedURLException;
3735
import java.net.ProtocolException;
3836
import java.net.URI;
@@ -41,22 +39,110 @@
4139
import java.security.cert.Certificate;
4240
import java.util.ArrayList;
4341
import java.util.HashMap;
42+
import java.util.HashSet;
4443
import java.util.List;
4544
import java.util.Map;
4645

4746
import javax.net.ssl.HttpsURLConnection;
4847
import javax.net.ssl.SSLPeerUnverifiedException;
4948

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+
5055
public class UrlHttpClientTest {
5156

5257
private ClientConfiguration conf;
53-
private UrlHttpClient client;
58+
private MockUrlHttpClient client;
5459

5560
@Before
5661
public void setup() {
5762
conf = new ClientConfiguration();
58-
client = new UrlHttpClient(conf);
63+
client = new MockUrlHttpClient(conf);
64+
}
5965

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");
60146
}
61147

62148
@Test
@@ -269,3 +355,28 @@ public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException {
269355
}
270356

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

Comments
 (0)