Merge pull request #852 from eugenp/ivanp81-master

Ivanp81 master
This commit is contained in:
Jim Kerak 2016-11-21 08:11:20 -05:00 committed by GitHub
commit 2f0768153a
5 changed files with 74 additions and 24 deletions

View File

@ -8,6 +8,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import org.baeldung.okhttp.ProgressRequestWrapper; import org.baeldung.okhttp.ProgressRequestWrapper;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import okhttp3.Call; import okhttp3.Call;
@ -22,10 +23,18 @@ public class OkHttpFileUploadingLiveTest {
private static final String BASE_URL = "http://localhost:8080/spring-rest"; private static final String BASE_URL = "http://localhost:8080/spring-rest";
OkHttpClient client;
@Before
public void init() {
client = new OkHttpClient();
}
@Test @Test
public void whenUploadFile_thenCorrect() throws IOException { public void whenUploadFile_thenCorrect() throws IOException {
OkHttpClient client = new OkHttpClient(); client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder() RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM) .setType(MultipartBody.FORM)
@ -47,7 +56,7 @@ public class OkHttpFileUploadingLiveTest {
@Test @Test
public void whenGetUploadFileProgress_thenCorrect() throws IOException { public void whenGetUploadFileProgress_thenCorrect() throws IOException {
OkHttpClient client = new OkHttpClient(); client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder() RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM) .setType(MultipartBody.FORM)
@ -55,14 +64,11 @@ public class OkHttpFileUploadingLiveTest {
RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))) RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt")))
.build(); .build();
ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, (long bytesWritten, long contentLength) -> {
ProgressRequestWrapper.ProgressListener listener = (bytesWritten, contentLength) -> { float percentage = 100f * bytesWritten / contentLength;
float percentage = 100f * bytesWritten / contentLength;
assertFalse(Float.compare(percentage, 100) > 0); assertFalse(Float.compare(percentage, 100) > 0);
}; });
ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, listener);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(BASE_URL + "/users/upload") .url(BASE_URL + "/users/upload")

View File

@ -4,19 +4,31 @@ import okhttp3.*;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import org.junit.Before;
import static org.hamcrest.Matchers.equalTo; import org.junit.Test;
import static org.junit.Assert.assertThat; import okhttp3.Call;
import static org.junit.Assert.fail; import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkHttpGetLiveTest { public class OkHttpGetLiveTest {
private static final String BASE_URL = "http://localhost:8080/spring-rest"; private static final String BASE_URL = "http://localhost:8080/spring-rest";
OkHttpClient client;
@Before
public void init() {
client = new OkHttpClient();
}
@Test @Test
public void whenGetRequest_thenCorrect() throws IOException { public void whenGetRequest_thenCorrect() throws IOException {
OkHttpClient client = new OkHttpClient(); client = new OkHttpClient();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(BASE_URL + "/date") .url(BASE_URL + "/date")
@ -31,7 +43,7 @@ public class OkHttpGetLiveTest {
@Test @Test
public void whenGetRequestWithQueryParameter_thenCorrect() throws IOException { public void whenGetRequestWithQueryParameter_thenCorrect() throws IOException {
OkHttpClient client = new OkHttpClient(); client = new OkHttpClient();
HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/ex/bars").newBuilder(); HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/ex/bars").newBuilder();
urlBuilder.addQueryParameter("id", "1"); urlBuilder.addQueryParameter("id", "1");
@ -51,7 +63,7 @@ public class OkHttpGetLiveTest {
@Test @Test
public void whenAsynchronousGetRequest_thenCorrect() throws InterruptedException { public void whenAsynchronousGetRequest_thenCorrect() throws InterruptedException {
OkHttpClient client = new OkHttpClient(); client = new OkHttpClient();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(BASE_URL + "/date") .url(BASE_URL + "/date")

View File

@ -2,6 +2,7 @@ package org.baeldung.okhttp;
import java.io.IOException; import java.io.IOException;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import okhttp3.Call; import okhttp3.Call;
@ -13,10 +14,18 @@ public class OkHttpHeaderLiveTest {
private static final String SAMPLE_URL = "http://www.github.com"; private static final String SAMPLE_URL = "http://www.github.com";
OkHttpClient client;
@Before
public void init() {
client = new OkHttpClient();
}
@Test @Test
public void whenSetHeader_thenCorrect() throws IOException { public void whenSetHeader_thenCorrect() throws IOException {
OkHttpClient client = new OkHttpClient(); client = new OkHttpClient();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(SAMPLE_URL) .url(SAMPLE_URL)
@ -43,6 +52,4 @@ public class OkHttpHeaderLiveTest {
Response response = call.execute(); Response response = call.execute();
response.close(); response.close();
} }
} }

View File

@ -4,18 +4,34 @@ import okhttp3.*;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkHttpMiscLiveTest { public class OkHttpMiscLiveTest {
private static final String BASE_URL = "http://localhost:8080/spring-rest"; private static final String BASE_URL = "http://localhost:8080/spring-rest";
private static Logger logger = LoggerFactory.getLogger(OkHttpMiscLiveTest.class); private static Logger logger = LoggerFactory.getLogger(OkHttpMiscLiveTest.class);
OkHttpClient client;
@Before
public void init() {
client = new OkHttpClient();
}
@Test @Test
public void whenSetRequestTimeout_thenFail() throws IOException { public void whenSetRequestTimeout_thenFail() throws IOException {
@ -37,7 +53,7 @@ public class OkHttpMiscLiveTest {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
OkHttpClient client = new OkHttpClient(); client = new OkHttpClient();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay. .url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay.

View File

@ -6,6 +6,7 @@ import static org.junit.Assert.assertThat;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import okhttp3.Call; import okhttp3.Call;
@ -23,10 +24,18 @@ public class OkHttpPostingLiveTest {
private static final String BASE_URL = "http://localhost:8080/spring-rest"; private static final String BASE_URL = "http://localhost:8080/spring-rest";
private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php"; private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php";
OkHttpClient client;
@Before
public void init() {
client = new OkHttpClient();
}
@Test @Test
public void whenSendPostRequest_thenCorrect() throws IOException { public void whenSendPostRequest_thenCorrect() throws IOException {
OkHttpClient client = new OkHttpClient(); client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder() RequestBody formBody = new FormBody.Builder()
.add("username", "test") .add("username", "test")
@ -49,7 +58,7 @@ public class OkHttpPostingLiveTest {
String postBody = "test post"; String postBody = "test post";
OkHttpClient client = new OkHttpClient(); client = new OkHttpClient();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(URL_SECURED_BY_BASIC_AUTHENTICATION) .url(URL_SECURED_BY_BASIC_AUTHENTICATION)
@ -66,7 +75,7 @@ public class OkHttpPostingLiveTest {
@Test @Test
public void whenPostJson_thenCorrect() throws IOException { public void whenPostJson_thenCorrect() throws IOException {
OkHttpClient client = new OkHttpClient(); client = new OkHttpClient();
String json = "{\"id\":1,\"name\":\"John\"}"; String json = "{\"id\":1,\"name\":\"John\"}";
@ -86,7 +95,7 @@ public class OkHttpPostingLiveTest {
@Test @Test
public void whenSendMultipartRequest_thenCorrect() throws IOException { public void whenSendMultipartRequest_thenCorrect() throws IOException {
OkHttpClient client = new OkHttpClient(); client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder() RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM) .setType(MultipartBody.FORM)