Skip to content

Commit

Permalink
Merge pull request #3 from swankjesse/jwilson_0104_upgrade_okhttp
Browse files Browse the repository at this point in the history
Upgrade OkHttp.
  • Loading branch information
mrmike committed Jan 7, 2016
2 parents 85ac06f + 8009f03 commit 79eb834
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 69 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ dependencies {
}
```

Then call Ok2Curl set method with OkHttpClient as an argument.
Then install the Ok2Curl interceptor.
```java
OkHttpClient client = new OkHttpClient();
Ok2Curl.set(client);
OkHttpClient okHttp = new OkHttpClient.Builder()
.addInterceptor(new CurlInterceptor())
.build();
```

By default Ok2Curl generates logs with `Ok2Curl` tag and log level set to`Log.DEBUG`. You can easily change this by calling
```java
Ok2Curl.set(client, "MyTag", Log.DEBUG);
OkHttpClient okHttp = new OkHttpClient.Builder()
.addInterceptor(new CurlInterceptor("MyTag", Log.DEBUG))
.build();
```

## Result
Expand All @@ -36,8 +39,9 @@ curl -X GET -H "Cache-Control:max-stale=2147483647, only-if-cached" https://api.
By default Ok2Curl uses application interceptors from OkHttp which is adequate for most cases. But sometimes you may want to use network interceptor e.g. to log Cookies set via [CookieHandler](http://docs.oracle.com/javase/6/docs/api/java/net/CookieHandler.html). In such a case add interceptor the same way as below:

```
OkHttpClient okHttp = new OkHttpClient();
okHttp.networkInterceptors().add(new CurlInterceptor());
OkHttpClient okHttp = new OkHttpClient.Builder()
.addNetworkInterceptor(new CurlInterceptor())
.build();
```

To get know more about Interceptor in OkHttp take a look here: https://github.com/square/okhttp/wiki/Interceptors
Expand Down
4 changes: 2 additions & 2 deletions ok2curl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ android {
}

dependencies {
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'com.squareup.okhttp:mockwebserver:2.5.0'
testCompile 'com.squareup.okhttp3:mockwebserver:3.0.0-RC1'
}
10 changes: 5 additions & 5 deletions ok2curl/src/main/java/com/moczul/ok2curl/CurlBuilder.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.moczul.ok2curl;

import com.squareup.okhttp.Headers;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.io.IOException;
import java.nio.charset.Charset;
Expand All @@ -29,7 +29,7 @@
private Map<String, String> headers = new HashMap<>();

public CurlBuilder(Request request) {
this.url = request.urlString();
this.url = request.url().toString();
this.method = request.method();
final RequestBody body = request.body();
if (body != null) {
Expand Down
6 changes: 3 additions & 3 deletions ok2curl/src/main/java/com/moczul/ok2curl/CurlInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import com.moczul.ok2curl.logger.AndroidLogger;
import com.moczul.ok2curl.logger.Loggable;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

Expand Down
16 changes: 0 additions & 16 deletions ok2curl/src/main/java/com/moczul/ok2curl/Ok2Curl.java

This file was deleted.

58 changes: 35 additions & 23 deletions ok2curl/src/test/java/com/moczul/ok2curl/CookieHandlerTest.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
package com.moczul.ok2curl;

import com.moczul.ok2curl.util.FakeLogger;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.CookieHandler;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;

import static org.mockito.Matchers.contains;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class CookieHandlerTest {

private OkHttpClient okHttpClient;
private FakeLogger logger;
private MockWebServer server;
private String url;

@Before
public void setUpOkHttp() throws IOException {
okHttpClient = new OkHttpClient();
okHttpClient.setCookieHandler(getCookieHandler());
logger = mock(FakeLogger.class);

server = new MockWebServer();
Expand All @@ -49,7 +46,11 @@ public void tearDown() throws IOException {
@Test
public void testApplicationInterceptor() throws IOException {
final Request request = new Request.Builder().url(url).build();
okHttpClient.interceptors().add(new CurlInterceptor(logger));

OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new CurlInterceptor(logger))
.cookieJar(getCookieJar())
.build();

okHttpClient.newCall(request).execute();

Expand All @@ -60,25 +61,36 @@ public void testApplicationInterceptor() throws IOException {
@Test
public void testNetworkInterceptor() throws IOException {
final Request request = new Request.Builder().url(url).build();
okHttpClient.networkInterceptors().add(new CurlInterceptor(logger));

OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(new CurlInterceptor(logger))
.cookieJar(getCookieJar())
.build();

okHttpClient.newCall(request).execute();

verify(logger).log(contains("-H \"Cookie:foo=bar; banana=rama;\""));
verify(logger).log(contains("-H \"Cookie:foo=bar; banana=rama\""));
}

private CookieHandler getCookieHandler() {
return new CookieHandler() {
private CookieJar getCookieJar() {
return new CookieJar() {
@Override
public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException {
final Map<String, List<String>> result = new HashMap<>();
result.put("Cookie", Collections.singletonList("foo=bar; banana=rama;"));
return result;
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
}

@Override
public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException {

public List<Cookie> loadForRequest(HttpUrl url) {
Cookie foo = new Cookie.Builder()
.name("foo")
.value("bar")
.domain(url.host())
.build();
Cookie banana = new Cookie.Builder()
.name("banana")
.value("rama")
.domain(url.host())
.build();
return Arrays.asList(foo, banana);
}
};
}
Expand Down
12 changes: 6 additions & 6 deletions ok2curl/src/test/java/com/moczul/ok2curl/CurlBuilderTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.moczul.ok2curl;

import com.squareup.okhttp.CacheControl;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

import okhttp3.CacheControl;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;

import static org.junit.Assert.assertEquals;

public class CurlBuilderTest {
Expand Down Expand Up @@ -69,6 +69,6 @@ public void postRequest_bodyWithNullMediaType() {
}

private RequestBody body() {
return new FormEncodingBuilder().add("key1", "value1").build();
return new FormBody.Builder().add("key1", "value1").build();
}
}
19 changes: 11 additions & 8 deletions sample/src/main/java/com/moczul/sample/RequestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import android.app.IntentService;
import android.content.Intent;

import com.moczul.ok2curl.Ok2Curl;
import com.squareup.okhttp.CacheControl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.moczul.ok2curl.CurlInterceptor;

import okhttp3.CacheControl;
import okhttp3.OkHttpClient;
import okhttp3.Request;

import java.io.IOException;

Expand All @@ -18,13 +19,15 @@ public RequestService() {

@Override
protected void onHandleIntent(Intent intent) {
OkHttpClient client = new OkHttpClient();

/**
* Alternatively you can specify tag and log level
* Ok2Curl.set(client, "MyTag", Log.DEBUG);
* CurlInterceptor curlInterceptor = new CurlInterceptor("MyTag", Log.DEBUG);
*/
Ok2Curl.set(client);
CurlInterceptor curlInterceptor = new CurlInterceptor();

OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(curlInterceptor)
.build();

Request request = new Request.Builder()
.url("https://api.github.com/repos/vmg/redcarpet/issues?state=closed")
Expand Down

0 comments on commit 79eb834

Please sign in to comment.