Example usage for com.squareup.okhttp Request header

List of usage examples for com.squareup.okhttp Request header

Introduction

In this page you can find the example usage for com.squareup.okhttp Request header.

Prototype

public String header(String name) 

Source Link

Usage

From source file:at.bitfire.dav4android.BasicDigestAuthenticatorTest.java

License:Open Source License

public void testRFCExample() {
    // use cnonce from example
    BasicDigestAuthenticator authenticator = new BasicDigestAuthenticator(null, "Mufasa", "Circle Of Life",
            "0a4f113b");

    // construct WWW-Authenticate
    HttpUtils.AuthScheme authScheme = new HttpUtils.AuthScheme("Digest");
    authScheme.params.put("realm", "testrealm@host.com");
    authScheme.params.put("qop", "auth");
    authScheme.params.put("nonce", "dcd98b7102dd2f0e8b11d0f600bfb0c093");
    authScheme.params.put("opaque", "5ccc069c403ebaf9f0171e9517f40e41");

    Request original = new Request.Builder().get().url("http://www.nowhere.org/dir/index.html").build();
    Request request = authenticator.authorizationRequest(original, authScheme);
    String auth = request.header("Authorization");
    assertTrue(auth.contains("username=\"Mufasa\""));
    assertTrue(auth.contains("realm=\"testrealm@host.com\""));
    assertTrue(auth.contains("nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\""));
    assertTrue(auth.contains("uri=\"/dir/index.html\""));
    assertTrue(auth.contains("qop=auth"));
    assertTrue(auth.contains("nc=00000001"));
    assertTrue(auth.contains("cnonce=\"0a4f113b\""));
    assertTrue(auth.contains("response=\"6629fae49393a05397450978507c4ef1\""));
    assertTrue(auth.contains("opaque=\"5ccc069c403ebaf9f0171e9517f40e41\""));
}

From source file:at.bitfire.dav4android.BasicDigestAuthenticatorTest.java

License:Open Source License

public void testRealWorldExamples() {
    BasicDigestAuthenticator authenticator = new BasicDigestAuthenticator(null, "demo", "demo",
            "MDI0ZDgxYTNmZDk4MTA1ODM0NDNjNmJjNDllYjQ1ZTI=");

    // example 1//from  w w w  . j a  v a 2s.  c  om
    HttpUtils.AuthScheme authScheme = new HttpUtils.AuthScheme("Digest");
    authScheme.params.put("realm", "Group-Office");
    authScheme.params.put("qop", "auth");
    authScheme.params.put("nonce", "56212407212c8");
    authScheme.params.put("opaque", "df58bdff8cf60599c939187d0b5c54de");

    Request original = new Request.Builder().method("PROPFIND", null)
            .url("https://demo.group-office.eu/caldav/").build();
    Request request = authenticator.authorizationRequest(original, authScheme);
    String auth = request.header("Authorization");
    assertTrue(auth.contains("username=\"demo\""));
    assertTrue(auth.contains("realm=\"Group-Office\""));
    assertTrue(auth.contains("nonce=\"56212407212c8\""));
    assertTrue(auth.contains("uri=\"/caldav/\""));
    assertTrue(auth.contains("cnonce=\"MDI0ZDgxYTNmZDk4MTA1ODM0NDNjNmJjNDllYjQ1ZTI=\""));
    assertTrue(auth.contains("nc=00000001"));
    assertTrue(auth.contains("qop=auth"));
    assertTrue(auth.contains("response=\"de3b3b194d85ddc62537208c9c3637dc\""));
    assertTrue(auth.contains("opaque=\"df58bdff8cf60599c939187d0b5c54de\""));

    // example 2
    authenticator = new BasicDigestAuthenticator(null, "test", "test");
    authScheme = new HttpUtils.AuthScheme("digest"); // lower case
    authScheme.params.put("nonce", "87c4c2aceed9abf30dd68c71");
    authScheme.params.put("algorithm", "md5"); // note the (illegal) lower case!
    authScheme.params.put("opaque",
            "571609eb7058505d35c7bf7288fbbec4-ODdjNGMyYWNlZWQ5YWJmMzBkZDY4YzcxLDAuMC4wLjAsMTQ0NTM3NzE0Nw==");
    authScheme.params.put("realm", "ieddy.ru");
    original = new Request.Builder().method("OPTIONS", null).url("https://ieddy.ru/").build();
    request = authenticator.authorizationRequest(original, authScheme);
    auth = request.header("Authorization");
    assertTrue(auth.contains("algorithm=\"MD5\"")); // some servers require it
    assertTrue(auth.contains("username=\"test\""));
    assertTrue(auth.contains("realm=\"ieddy.ru\""));
    assertTrue(auth.contains("nonce=\"87c4c2aceed9abf30dd68c71\""));
    assertTrue(auth.contains("uri=\"/\""));
    assertFalse(auth.contains("cnonce="));
    assertFalse(auth.contains("nc="));
    assertFalse(auth.contains("qop="));
    assertTrue(auth.contains("response=\"d42a39f25f80b0d6907286a960ff9c7d\""));
    assertTrue(auth.contains(
            "opaque=\"571609eb7058505d35c7bf7288fbbec4-ODdjNGMyYWNlZWQ5YWJmMzBkZDY4YzcxLDAuMC4wLjAsMTQ0NTM3NzE0Nw==\""));
}

From source file:at.bitfire.dav4android.BasicDigestAuthenticatorTest.java

License:Open Source License

public void testMD5Sess() {
    BasicDigestAuthenticator authenticator = new BasicDigestAuthenticator(null, "admin", "12345",
            "hxk1lu63b6c7vhk");

    HttpUtils.AuthScheme authScheme = new HttpUtils.AuthScheme("Digest");
    authScheme.params.put("realm", "MD5-sess Example");
    authScheme.params.put("qop", "auth");
    authScheme.params.put("algorithm", "MD5-sess");
    authScheme.params.put("nonce", "dcd98b7102dd2f0e8b11d0f600bfb0c093");
    authScheme.params.put("opaque", "5ccc069c403ebaf9f0171e9517f40e41");

    /*  A1 = h("admin:MD5-sess Example:12345"):dcd98b7102dd2f0e8b11d0f600bfb0c093:hxk1lu63b6c7vhk =
          4eaed818bc587129e73b39c8d3e8425a:dcd98b7102dd2f0e8b11d0f600bfb0c093:hxk1lu63b6c7vhk       a994ee9d33e2f077d3a6e13e882f6686
    A2 = POST:/plain.txt                                                                            1b557703454e1aa1230c5523f54380ed
            /*ww  w .  j  a va 2  s .  c  o m*/
    h("a994ee9d33e2f077d3a6e13e882f6686:dcd98b7102dd2f0e8b11d0f600bfb0c093:00000001:hxk1lu63b6c7vhk:auth:1b557703454e1aa1230c5523f54380ed") =
    af2a72145775cfd08c36ad2676e89446
    */

    Request original = new Request.Builder()
            .method("POST", RequestBody.create(MediaType.parse("text/plain"), "PLAIN TEXT"))
            .url("http://example.com/plain.txt").build();
    Request request = authenticator.authorizationRequest(original, authScheme);
    String auth = request.header("Authorization");
    assertTrue(auth.contains("username=\"admin\""));
    assertTrue(auth.contains("realm=\"MD5-sess Example\""));
    assertTrue(auth.contains("nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\""));
    assertTrue(auth.contains("uri=\"/plain.txt\""));
    assertTrue(auth.contains("cnonce=\"hxk1lu63b6c7vhk\""));
    assertTrue(auth.contains("nc=00000001"));
    assertTrue(auth.contains("qop=auth"));
    assertTrue(auth.contains("response=\"af2a72145775cfd08c36ad2676e89446\""));
    assertTrue(auth.contains("opaque=\"5ccc069c403ebaf9f0171e9517f40e41\""));
}

From source file:at.bitfire.dav4android.BasicDigestAuthenticatorTest.java

License:Open Source License

public void testMD5AuthInt() {
    BasicDigestAuthenticator authenticator = new BasicDigestAuthenticator(null, "admin", "12435",
            "hxk1lu63b6c7vhk");

    HttpUtils.AuthScheme authScheme = new HttpUtils.AuthScheme("Digest");
    authScheme.params.put("realm", "AuthInt Example");
    authScheme.params.put("qop", "auth-int");
    authScheme.params.put("nonce", "367sj3265s5");
    authScheme.params.put("opaque", "87aaxcval4gba36");

    /*  A1 = admin:AuthInt Example:12345                            380dc3fc1305127cd2aa81ab68ef3f34
            /* w  ww.  j a  v a 2 s.  com*/
    h("PLAIN TEXT") = 20296edbd4c4275fb416b64e4be752f9
    A2 = POST:/plain.txt:20296edbd4c4275fb416b64e4be752f9       a71c4c86e18b3993ffc98c6e426fe4b0
            
    h(380dc3fc1305127cd2aa81ab68ef3f34:367sj3265s5:00000001:hxk1lu63b6c7vhk:auth-int:a71c4c86e18b3993ffc98c6e426fe4b0) =
    81d07cb3b8d412b34144164124c970cb
    */

    Request original = new Request.Builder()
            .method("POST", RequestBody.create(MediaType.parse("text/plain"), "PLAIN TEXT"))
            .url("http://example.com/plain.txt").build();
    Request request = authenticator.authorizationRequest(original, authScheme);
    String auth = request.header("Authorization");
    assertTrue(auth.contains("username=\"admin\""));
    assertTrue(auth.contains("realm=\"AuthInt Example\""));
    assertTrue(auth.contains("nonce=\"367sj3265s5\""));
    assertTrue(auth.contains("uri=\"/plain.txt\""));
    assertTrue(auth.contains("cnonce=\"hxk1lu63b6c7vhk\""));
    assertTrue(auth.contains("nc=00000001"));
    assertTrue(auth.contains("qop=auth-int"));
    assertTrue(auth.contains("response=\"5ab6822b9d906cc711760a7783b28dca\""));
    assertTrue(auth.contains("opaque=\"87aaxcval4gba36\""));
}

From source file:at.bitfire.dav4android.BasicDigestAuthenticatorTest.java

License:Open Source License

public void testLegacyDigest() {
    BasicDigestAuthenticator authenticator = new BasicDigestAuthenticator(null, "Mufasa", "CircleOfLife");

    // construct WWW-Authenticate
    HttpUtils.AuthScheme authScheme = new HttpUtils.AuthScheme("Digest");
    authScheme.params.put("realm", "testrealm@host.com");
    authScheme.params.put("nonce", "dcd98b7102dd2f0e8b11d0f600bfb0c093");
    authScheme.params.put("opaque", "5ccc069c403ebaf9f0171e9517f40e41");

    Request original = new Request.Builder().get().url("http://www.nowhere.org/dir/index.html").build();
    Request request = authenticator.authorizationRequest(original, authScheme);
    String auth = request.header("Authorization");
    assertTrue(auth.contains("username=\"Mufasa\""));
    assertTrue(auth.contains("realm=\"testrealm@host.com\""));
    assertTrue(auth.contains("nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\""));
    assertTrue(auth.contains("uri=\"/dir/index.html\""));
    assertFalse(auth.contains("qop="));
    assertFalse(auth.contains("nc="));
    assertFalse(auth.contains("cnonce="));
    assertTrue(auth.contains("response=\"1949323746fe6a43ef61f9606e7febea\""));
    assertTrue(auth.contains("opaque=\"5ccc069c403ebaf9f0171e9517f40e41\""));
}

From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java

License:Open Source License

@Test
public void networkInterceptorsObserveNetworkHeaders() throws Exception {
    server.enqueue(new MockResponse().setBody(gzip("abcabcabc")).addHeader("Content-Encoding: gzip"));

    client.networkInterceptors().add(new Interceptor() {
        @Override/*from  w  w  w.  j a  v a  2s .  c  o  m*/
        public Response intercept(Chain chain) throws IOException {
            // The network request has everything: User-Agent, Host, Accept-Encoding.
            Request networkRequest = chain.request();
            assertNotNull(networkRequest.header("User-Agent"));
            assertEquals(server.get().getHostName() + ":" + server.get().getPort(),
                    networkRequest.header("Host"));
            assertNotNull(networkRequest.header("Accept-Encoding"));

            // The network response also has everything, including the raw gzipped content.
            Response networkResponse = chain.proceed(networkRequest);
            assertEquals("gzip", networkResponse.header("Content-Encoding"));
            return networkResponse;
        }
    });

    Request request = new Request.Builder().url(server.getUrl("/")).build();

    // No extra headers in the application's request.
    assertNull(request.header("User-Agent"));
    assertNull(request.header("Host"));
    assertNull(request.header("Accept-Encoding"));

    // No extra headers in the application's response.
    Response response = FiberOkHttpUtil.executeInFiber(client, request);
    assertNull(request.header("Content-Encoding"));
    assertEquals("abcabcabc", response.body().string());
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

@Test
public void networkInterceptorsObserveNetworkHeaders() throws Exception {
    server.enqueue(new MockResponse().setBody(gzip("abcabcabc")).addHeader("Content-Encoding: gzip"));

    client.networkInterceptors().add(new Interceptor() {
        @Override//from ww  w . j av  a2 s. c  o m
        public Response intercept(Chain chain) throws IOException {
            // The network request has everything: User-Agent, Host, Accept-Encoding.
            Request networkRequest = chain.request();
            assertNotNull(networkRequest.header("User-Agent"));
            assertEquals(server.getHostName() + ":" + server.getPort(), networkRequest.header("Host"));
            assertNotNull(networkRequest.header("Accept-Encoding"));

            // The network response also has everything, including the raw gzipped content.
            Response networkResponse = chain.proceed(networkRequest);
            assertEquals("gzip", networkResponse.header("Content-Encoding"));
            return networkResponse;
        }
    });

    Request request = new Request.Builder().url(server.url("/")).build();

    // No extra headers in the application's request.
    assertNull(request.header("User-Agent"));
    assertNull(request.header("Host"));
    assertNull(request.header("Accept-Encoding"));

    // No extra headers in the application's response.
    Response response = client.newCall(request).execute();
    assertNull(request.header("Content-Encoding"));
    assertEquals("abcabcabc", response.body().string());
}

From source file:com.heroiclabs.sdk.android.util.http.GzipRequestInterceptor.java

License:Apache License

/** {@inheritDoc} */
@Override//from  w ww  .  j  ava 2s  .  c om
public Response intercept(final Chain chain) throws IOException {
    final Request originalRequest = chain.request();

    // Do not compress requests already encoded, or that are too small.
    if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null
            || originalRequest.body().contentLength() < 300) {
        return chain.proceed(originalRequest);
    }

    final Request compressedRequest = originalRequest.newBuilder().header("Content-Encoding", "gzip")
            .method(originalRequest.method(), forceContentLength(gzip(originalRequest.body()))).build();
    return chain.proceed(compressedRequest);
}

From source file:com.ibm.watson.developer_cloud.service.RequestBuilderTest.java

License:Open Source License

/**
 * Test build./*  ww w.j a  v a2s.c  o m*/
 */
@Test
public void testBuild() {
    final String xToken = "x-token";
    final Request request = RequestBuilder.post(urlWithQuery).withBodyContent("body1", HttpMediaType.TEXT_PLAIN)
            .withHeader("x-token", "token1").build();

    assertEquals("POST", request.method());
    assertEquals("token1", request.header(xToken));
}

From source file:com.kubeiwu.easyandroid.easyhttp.core.retrofit.KOkHttpCall.java

License:Apache License

private String getCacheMode(Request request) {
    return request.header("Cache-Mode");
}