Skip to content

Commit

Permalink
Add a few more "unit" level tests for WebUtils
Browse files Browse the repository at this point in the history
Issue: getodk#620
  • Loading branch information
alxndrsn committed Apr 18, 2017
1 parent 8e1bddb commit be15b17
Showing 1 changed file with 82 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.odk.collect.android.utilities;

import java.net.URI;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
Expand All @@ -18,15 +17,19 @@
import okhttp3.mockwebserver.RecordedRequest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.odk.collect.android.test.MockedServerTestUtils.firstRequestFor;
import static org.odk.collect.android.test.MockedServerTestUtils.mockWebServer;
import static org.odk.collect.android.test.TestUtils.assertMatches;

public class WebUtilsTest {
private MockWebServer server;

@Before
public void setUp() throws Exception {
server = new MockWebServer();
server.start();
server = mockWebServer();

// server hangs without a response queued:
server.enqueue(new MockResponse());
}
Expand All @@ -38,22 +41,88 @@ public void tearDown() throws Exception {

@Test
public void httpRequests_shouldHaveUseragentHeader() throws Exception {
// given
String url = String.format("http://uname:pword@localhost:%s/some-path", server.getPort());

// when
doRequest(url);
doRequest("/some-path");

// then
RecordedRequest r = server.takeRequest(1, TimeUnit.MILLISECONDS);
RecordedRequest r = firstRequestFor(server);
assertEquals("GET /some-path HTTP/1.1", r.getRequestLine());
assertTrue(r.getHeader("User-Agent").matches("Dalvik/.* org.odk.collect.android/.*"));
}

private static void doRequest(String url) throws Exception {
HttpContext localContext = Collect.getInstance().getHttpContext();
HttpClient httpclient = WebUtils.createHttpClient(WebUtils.CONNECTION_TIMEOUT);
HttpGet req = WebUtils.createOpenRosaHttpGet(new URI(url));
HttpResponse response = httpclient.execute(req, localContext);
@Test
public void getXmlDocument_request_shouldSupplyHeader_UserAgent() throws Exception {
// when
WebUtils.getXmlDocument(url("/list-forms"), httpContext(), httpClient());

// then
assertMatches("Dalvik/.* org.odk.collect.android/.*",
firstRequestFor(server).getHeader("User-Agent"));
}

@Test
public void getXmlDocument_request_shouldSupplyHeader_X_OpenRosa_Version() throws Exception {
// when
WebUtils.getXmlDocument(url("/list-forms"), httpContext(), httpClient());

// then
assertEquals("1.0",
firstRequestFor(server).getHeader("X-OpenRosa-Version"));
}

@Test
public void getXmlDocument_request_shouldSupplyHeader_AcceptEncoding_gzip() throws Exception {
// when
WebUtils.getXmlDocument(url("/list-forms"), httpContext(), httpClient());

// then
assertEquals("gzip",
firstRequestFor(server).getHeader("Accept-Encoding"));
}

@Test
public void getXmlDocument_request_shouldNotSupplyHeader_Authorization_forHttpRequest() throws Exception {
// when
WebUtils.getXmlDocument(url("/list-forms"), httpContext(), httpClient());

// then
assertNull(firstRequestFor(server).getHeader("Authorization"));
}

@Test
public void getXmlDocument_request_shouldReportInvalidUrl() throws Exception {
// when
DocumentFetchResult res = WebUtils.getXmlDocument("NOT_A_URL", httpContext(), httpClient());

// then
assertEquals(0, res.responseCode);
assertMatches(".*while accessingNOT_A_URL", res.errorMessage);
}

@Test
public void getXmlDocument_request_shouldReportInvalidHost() throws Exception {
// when
DocumentFetchResult res = WebUtils.getXmlDocument("file:/some/path", httpContext(), httpClient());

// then
assertEquals(0, res.responseCode);
assertEquals("Invalid server URL (no hostname): file:/some/path", res.errorMessage);
}

private String url(String path) {
return server.url(path).toString();
}

private void doRequest(String path) throws Exception {
HttpGet req = WebUtils.createOpenRosaHttpGet(new URI(url(path)));
HttpResponse response = httpClient().execute(req, httpContext());
}

private static HttpClient httpClient() {
return WebUtils.createHttpClient(WebUtils.CONNECTION_TIMEOUT);
}

private static HttpContext httpContext() {
return Collect.getInstance().getHttpContext();
}
}

0 comments on commit be15b17

Please sign in to comment.