forked from camilor67/collect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include User-Agent header in HTTP requests (getodk#800)
Closes getodk#167
- Loading branch information
1 parent
5c7febb
commit 5aff853
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
collect_app/src/androidTest/java/org/odk/collect/android/utilities/WebUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.odk.collect.android.utilities; | ||
|
||
import android.test.*; | ||
|
||
import okhttp3.mockwebserver.*; | ||
|
||
import java.net.*; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.*; | ||
import org.odk.collect.android.application.*; | ||
import org.opendatakit.httpclientandroidlib.*; | ||
import org.opendatakit.httpclientandroidlib.client.*; | ||
import org.opendatakit.httpclientandroidlib.client.methods.*; | ||
import org.opendatakit.httpclientandroidlib.protocol.*; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class WebUtilsTest { | ||
private MockWebServer server; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
server = new MockWebServer(); | ||
server.start(); | ||
// server hangs without a response queued: | ||
server.enqueue(new MockResponse()); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
server.shutdown(); | ||
} | ||
|
||
@Test | ||
public void httpRequests_shouldHaveUseragentHeader() throws Exception { | ||
// given | ||
String url = String.format("http://uname:pword@localhost:%s/some-path", server.getPort()); | ||
|
||
// when | ||
doRequest(url); | ||
|
||
// then | ||
RecordedRequest r = server.takeRequest(1, TimeUnit.MILLISECONDS); | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters