summaryrefslogtreecommitdiffstats
path: root/luni
diff options
context:
space:
mode:
authorJason Parks <jparks@google.com>2014-12-04 08:42:10 -0600
committerNarayan Kamath <narayan@google.com>2014-12-04 17:17:07 +0000
commit003933aeb83184baa291b0f29a9a9357fd667ba7 (patch)
tree5d4bc1a71549b878b6a4359edc945819c336e983 /luni
parent04704e37a20e6992e64583822b91d325d83aee48 (diff)
downloadlibcore-003933aeb83184baa291b0f29a9a9357fd667ba7.tar.gz
libcore-003933aeb83184baa291b0f29a9a9357fd667ba7.tar.bz2
libcore-003933aeb83184baa291b0f29a9a9357fd667ba7.zip
Use a MockWebServer instead of hitting the network.
Bug: 18619181 Change-Id: I477efa1d0105732fe8b10ba83808af69421ea8e0
Diffstat (limited to 'luni')
-rw-r--r--luni/src/test/java/libcore/java/net/OldCookieHandlerTest.java73
1 files changed, 34 insertions, 39 deletions
diff --git a/luni/src/test/java/libcore/java/net/OldCookieHandlerTest.java b/luni/src/test/java/libcore/java/net/OldCookieHandlerTest.java
index 249c32660..6992cae8b 100644
--- a/luni/src/test/java/libcore/java/net/OldCookieHandlerTest.java
+++ b/luni/src/test/java/libcore/java/net/OldCookieHandlerTest.java
@@ -16,6 +16,9 @@
package libcore.java.net;
+import com.google.mockwebserver.MockResponse;
+import com.google.mockwebserver.MockWebServer;
+
import java.io.IOException;
import java.net.CookieHandler;
import java.net.URI;
@@ -27,65 +30,57 @@ import tests.support.Support_Configuration;
public class OldCookieHandlerTest extends TestCase {
- URI getURI, putURI;
- String link = "http://" + Support_Configuration.SpecialInetTestAddress + "/";
- boolean isGetCalled = false;
- boolean isPutCalled = false;
- boolean completedSuccessfully = false;
-
public void test_CookieHandler() {
assertNull(CookieHandler.getDefault());
}
- public void test_get_put() {
+ public void test_get_put() throws Exception {
MockCookieHandler mch = new MockCookieHandler();
CookieHandler defaultHandler = CookieHandler.getDefault();
- CookieHandler.setDefault(mch);
-
- class TestThread extends Thread {
- public void run() {
- try {
- URL url = new URL(link);
- URLConnection conn = url.openConnection();
- conn.getContent();
- url = new URL(link);
- conn = url.openConnection();
- conn.getContent();
- completedSuccessfully = true;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
try {
- TestThread thread = new TestThread();
+ CookieHandler.setDefault(mch);
- thread.start();
- try {
- thread.join();
- } catch (InterruptedException e) {
- fail("InterruptedException was thrown.");
- }
+ MockWebServer server = new MockWebServer();
+ server.enqueue(new MockResponse().addHeader("Set-Cookie2: a=\"android\"; "
+ + "Comment=\"this cookie is delicious\"; "
+ + "CommentURL=\"http://google.com/\"; "
+ + "Discard; "
+ + "Domain=\"" + server.getCookieDomain() + "\"; "
+ + "Max-Age=\"60\"; "
+ + "Path=\"/path\"; "
+ + "Port=\"80,443," + server.getPort() + "\"; "
+ + "Secure; "
+ + "Version=\"1\""));
+ server.play();
- assertTrue(isGetCalled);
- assertTrue(isPutCalled);
- assertTrue(completedSuccessfully);
+ URLConnection connection = server.getUrl("/path/foo").openConnection();
+ connection.getContent();
+
+ assertTrue(mch.wasGetCalled());
+ assertTrue(mch.wasPutCalled());
} finally {
CookieHandler.setDefault(defaultHandler);
}
}
- class MockCookieHandler extends CookieHandler {
+ private static class MockCookieHandler extends CookieHandler {
+ private boolean getCalled = false;
+ private boolean putCalled = false;
public Map get(URI uri, Map requestHeaders) throws IOException {
- getURI = uri;
- isGetCalled = true;
+ getCalled = true;
return requestHeaders;
}
public void put(URI uri, Map responseHeaders) throws IOException {
- putURI = uri;
- isPutCalled = true;
+ putCalled = true;
+ }
+
+ public boolean wasGetCalled() {
+ return getCalled;
+ }
+ public boolean wasPutCalled() {
+ return putCalled;
}
}
}