Search in sources :

Example 11 with JavaNetCookieJar

use of okhttp3.JavaNetCookieJar in project okhttp by square.

the class CookiesTest method cookieHandlerLikeAndroid.

@Test
public void cookieHandlerLikeAndroid() throws Exception {
    final MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse());
    server.start();
    final HttpUrl serverUrl = urlWithIpAddress(server, "/");
    CookieHandler androidCookieHandler = new CookieHandler() {

        @Override
        public Map<String, List<String>> get(URI uri, Map<String, List<String>> map) throws IOException {
            return Collections.singletonMap("Cookie", Collections.singletonList("$Version=\"1\"; " + "a=\"android\";$Path=\"/\";$Domain=\"" + serverUrl.host() + "\"; " + "b=\"banana\";$Path=\"/\";$Domain=\"" + serverUrl.host() + "\""));
        }

        @Override
        public void put(URI uri, Map<String, List<String>> map) throws IOException {
        }
    };
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(androidCookieHandler)).build();
    get(serverUrl);
    RecordedRequest request = server.takeRequest();
    assertEquals("a=android; b=banana", request.getHeader("Cookie"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) List(java.util.List) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CookieHandler(java.net.CookieHandler) Test(org.junit.Test)

Example 12 with JavaNetCookieJar

use of okhttp3.JavaNetCookieJar in project okhttp by square.

the class CallTest method redirectsDoNotIncludeTooManyCookies.

@Test
public void redirectsDoNotIncludeTooManyCookies() throws Exception {
    server2.enqueue(new MockResponse().setBody("Page 2"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.url("/")));
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    HttpCookie cookie = new HttpCookie("c", "cookie");
    cookie.setDomain(server.getHostName());
    cookie.setPath("/");
    String portList = Integer.toString(server.getPort());
    cookie.setPortlist(portList);
    cookieManager.getCookieStore().add(server.url("/").uri(), cookie);
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
    Response response = client.newCall(new Request.Builder().url(server.url("/page1")).build()).execute();
    assertEquals("Page 2", response.body().string());
    RecordedRequest request1 = server.takeRequest();
    assertEquals("c=cookie", request1.getHeader("Cookie"));
    RecordedRequest request2 = server2.takeRequest();
    assertNull(request2.getHeader("Cookie"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 13 with JavaNetCookieJar

use of okhttp3.JavaNetCookieJar in project okhttp by square.

the class URLConnectionTest method connectViaHttpsReusingConnections.

private void connectViaHttpsReusingConnections(boolean rebuildClient) throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    server.enqueue(new MockResponse().setBody("another response via HTTPS"));
    // The pool will only reuse sockets if the SSL socket factories are the same.
    SSLSocketFactory clientSocketFactory = sslClient.socketFactory;
    RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
    CookieJar cookieJar = new JavaNetCookieJar(new CookieManager());
    ConnectionPool connectionPool = new ConnectionPool();
    urlFactory.setClient(new OkHttpClient.Builder().cache(cache).connectionPool(connectionPool).cookieJar(cookieJar).sslSocketFactory(clientSocketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
    connection = urlFactory.open(server.url("/").url());
    assertContent("this response comes via HTTPS", connection);
    if (rebuildClient) {
        urlFactory.setClient(new OkHttpClient.Builder().cache(cache).connectionPool(connectionPool).cookieJar(cookieJar).sslSocketFactory(clientSocketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
    }
    connection = urlFactory.open(server.url("/").url());
    assertContent("another response via HTTPS", connection);
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) CookieManager(java.net.CookieManager)

Example 14 with JavaNetCookieJar

use of okhttp3.JavaNetCookieJar in project 2017-01-HUDI-MAC-CHAR by NHNNEXT.

the class ServiceGenerator method createService.

public static <S> S createService(Class<S> serviceClass, Context context) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    if (BuildConfig.DEBUG) {
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    } else {
        logging.setLevel(HttpLoggingInterceptor.Level.NONE);
    }
    PersistentCookieStore cookieStore = new PersistentCookieStore(context);
    java.net.CookieManager cookieManager = new java.net.CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder().cookieJar(new JavaNetCookieJar(cookieManager)).addInterceptor(logging).addNetworkInterceptor(new StethoInterceptor());
    Gson gson = new GsonBuilder().setLenient().create();
    Retrofit retorfit = new Retrofit.Builder().baseUrl(MafiaRemoteService.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).client(httpClient.build()).build();
    return retorfit.create(serviceClass);
}
Also used : PersistentCookieStore(com.zimincom.mafiaonline.PersistentCookieStore) JavaNetCookieJar(okhttp3.internal.JavaNetCookieJar) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Retrofit(retrofit2.Retrofit) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Aggregations

CookieManager (java.net.CookieManager)11 MockResponse (okhttp3.mockwebserver.MockResponse)10 Test (org.junit.Test)9 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 HttpCookie (java.net.HttpCookie)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 OkHttpClient (okhttp3.OkHttpClient)4 JavaNetCookieJar (okhttp3.JavaNetCookieJar)3 URI (java.net.URI)2 KeyManagementException (java.security.KeyManagementException)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 SSLContext (javax.net.ssl.SSLContext)2 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)2 NonNull (android.support.annotation.NonNull)1 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)1 Gson (com.google.gson.Gson)1