You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
etesync-android/app/src/test/java/at/bitfire/davdroid/HttpClientTest.java

74 lines
2.4 KiB

/*
* Copyright © 2013 2015 Ricki Hirner (bitfire web engineering).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package at.bitfire.davdroid;
import junit.framework.TestCase;
import java.io.IOException;
import java.net.URISyntaxException;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
public class HttpClientTest extends TestCase {
MockWebServer server;
OkHttpClient httpClient;
@Override
public void setUp() throws IOException {
httpClient = HttpClient.create();
server = new MockWebServer();
server.start(30000);
}
@Override
public void tearDown() throws IOException {
server.shutdown();
}
public void testCookies() throws IOException, InterruptedException, URISyntaxException {
HttpUrl url = server.url("/test");
// set cookie for root path (/) and /test path in first response
server.enqueue(new MockResponse()
.setResponseCode(200)
.addHeader("Set-Cookie", "cookie1=1; path=/")
.addHeader("Set-Cookie", "cookie2=2")
.setBody("Cookie set"));
httpClient.newCall(new Request.Builder()
.get().url(url)
.build()).execute();
assertNull(server.takeRequest().getHeader("Cookie"));
// cookie should be sent with second request
// second response lets first cookie expire and overwrites second cookie
server.enqueue(new MockResponse()
.addHeader("Set-Cookie", "cookie1=1a; path=/; Max-Age=0")
.addHeader("Set-Cookie", "cookie2=2a")
.setResponseCode(200));
httpClient.newCall(new Request.Builder()
.get().url(url)
.build()).execute();
assertEquals("cookie2=2; cookie1=1", server.takeRequest().getHeader("Cookie"));
server.enqueue(new MockResponse()
.setResponseCode(200));
httpClient.newCall(new Request.Builder()
.get().url(url)
.build()).execute();
assertEquals("cookie2=2a", server.takeRequest().getHeader("Cookie"));
}
}