2015-10-19 13:09:42 +00:00
|
|
|
|
/*
|
|
|
|
|
* 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 android.test.InstrumentationTestCase;
|
|
|
|
|
|
|
|
|
|
import com.squareup.okhttp.ConnectionSpec;
|
2015-11-08 17:51:19 +00:00
|
|
|
|
import com.squareup.okhttp.HttpUrl;
|
|
|
|
|
import com.squareup.okhttp.Request;
|
|
|
|
|
import com.squareup.okhttp.Response;
|
2015-10-19 13:09:42 +00:00
|
|
|
|
import com.squareup.okhttp.TlsVersion;
|
2015-11-08 17:51:19 +00:00
|
|
|
|
import com.squareup.okhttp.mockwebserver.MockResponse;
|
2015-10-19 13:09:42 +00:00
|
|
|
|
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2015-11-08 17:51:19 +00:00
|
|
|
|
import java.net.URISyntaxException;
|
2015-10-19 13:09:42 +00:00
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
|
|
public class HttpClientTest extends InstrumentationTestCase {
|
|
|
|
|
|
2015-11-08 17:51:19 +00:00
|
|
|
|
MockWebServer server;
|
2015-10-19 13:09:42 +00:00
|
|
|
|
HttpClient httpClient;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setUp() throws IOException {
|
|
|
|
|
httpClient = new HttpClient(null, getInstrumentation().getTargetContext().getApplicationContext());
|
|
|
|
|
|
2015-11-08 17:51:19 +00:00
|
|
|
|
server = new MockWebServer();
|
2015-10-19 13:09:42 +00:00
|
|
|
|
server.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void tearDown() throws IOException {
|
|
|
|
|
server.shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-08 17:51:19 +00:00
|
|
|
|
public void testCookies() throws IOException, InterruptedException, URISyntaxException {
|
|
|
|
|
HttpUrl url = server.url("/");
|
|
|
|
|
|
|
|
|
|
// set cookie in first response
|
|
|
|
|
server.enqueue(new MockResponse()
|
|
|
|
|
.setResponseCode(200)
|
|
|
|
|
.setHeader("Set-Cookie", "theme=light; path=/")
|
|
|
|
|
.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
|
|
|
|
|
server.enqueue(new MockResponse()
|
|
|
|
|
.setResponseCode(200));
|
|
|
|
|
httpClient.newCall(new Request.Builder()
|
|
|
|
|
.get().url(url)
|
|
|
|
|
.build()).execute();
|
|
|
|
|
//assertEquals("theme=light", server.takeRequest().getHeader("Cookie"));
|
|
|
|
|
|
|
|
|
|
// doesn't work for URLs with ports, see https://code.google.com/p/android/issues/detail?id=193475
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-19 13:09:42 +00:00
|
|
|
|
public void testTLSVersion() throws IOException {
|
2015-11-08 17:51:19 +00:00
|
|
|
|
server.useHttps(new SSLSocketFactoryCompat(null), false);
|
|
|
|
|
assertEquals("https", server.url("/").scheme());
|
|
|
|
|
|
|
|
|
|
httpClient.setConnectionSpecs(Collections.singletonList(new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
|
|
|
|
|
.tlsVersions(TlsVersion.TLS_1_2)
|
|
|
|
|
.build()));
|
|
|
|
|
|
2015-10-19 13:09:42 +00:00
|
|
|
|
// FIXME
|
|
|
|
|
/*server.enqueue(new MockResponse().setResponseCode(204));
|
|
|
|
|
Response response = httpClient.newCall(new Request.Builder()
|
|
|
|
|
.get().url(server.url("/"))
|
|
|
|
|
.build()).execute();
|
|
|
|
|
assertTrue(response.isSuccessful());*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|