Search in sources :

Example 26 with ForestRequest

use of com.dtflys.forest.http.ForestRequest in project forest by dromara.

the class TestBaseRedirectClient method testAutoRedirect_303.

@Test
public void testAutoRedirect_303() {
    server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(303));
    server.enqueue(new MockResponse().setBody(EXPECTED));
    AtomicReference<ForestRequest> atomicReq = new AtomicReference<>(null);
    ForestResponse<String> response = redirectClient.testAutoRedirect(((redirectReq, prevReq, prevRes) -> {
        atomicReq.set(redirectReq);
    }));
    assertThat(atomicReq.get()).isNotNull();
    assertThat(atomicReq.get().path()).isEqualTo("/b");
    assertThat(response).isNotNull();
    assertThat(response.getStatusCode()).isEqualTo(200);
    String result = response.getResult();
    assertThat(result).isNotNull().isEqualTo(EXPECTED);
}
Also used : BeforeClass(org.junit.BeforeClass) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ForestResponse(com.dtflys.forest.http.ForestResponse) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) Rule(org.junit.Rule) MockWebServer(okhttp3.mockwebserver.MockWebServer) MockResponse(okhttp3.mockwebserver.MockResponse) MockServerRequest.mockRequest(com.dtflys.forest.mock.MockServerRequest.mockRequest) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) HttpBackend(com.dtflys.forest.backend.HttpBackend) BaseClientTest(com.dtflys.test.http.BaseClientTest) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 27 with ForestRequest

use of com.dtflys.forest.http.ForestRequest in project forest by dromara.

the class TestTimeoutClient method testConnectTimeout.

@Test
public void testConnectTimeout() {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START));
    ForestRequest request = timeoutClient.testConnectTimeout();
    assertThat(request).isNotNull();
    assertThat(request.getConnectTimeout()).isEqualTo(10);
    ForestResponse response = (ForestResponse) request.execute(ForestResponse.class);
    assertThat(response).isNotNull();
    assertThat(response.isTimeout()).isTrue();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestResponse(com.dtflys.forest.http.ForestResponse) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 28 with ForestRequest

use of com.dtflys.forest.http.ForestRequest in project forest by dromara.

the class TestEncoderClient method testJackson.

@Test
public void testJackson() {
    EncoderClient.Entry entry = new EncoderClient.Entry("AAA", "BBB");
    server.enqueue(new MockResponse().setBody(EXPECTED));
    ForestRequest request = encoderClient.testJackson(entry);
    assertThat(request).isNotNull();
    assertThat(request.bodyType()).isNotNull().isEqualTo(ForestDataType.JSON);
    assertThat(request.getEncoder()).isNotNull().isInstanceOf(ForestJacksonConverter.class);
    request.execute();
    mockRequest(server).assertBodyEquals("{\"name\":\"AAA\",\"value\":\"BBB\"}");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 29 with ForestRequest

use of com.dtflys.forest.http.ForestRequest in project forest by dromara.

the class TestEncoderClient method testEncoder.

@Test
public void testEncoder() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    ForestRequest request = encoderClient.testEncoder("AAA", "BBB");
    assertThat(request).isNotNull();
    assertThat(request.bodyType()).isNotNull().isEqualTo(ForestDataType.JSON);
    request.execute();
    mockRequest(server).assertHeaderEquals("Content-Type", ContentType.APPLICATION_X_WWW_FORM_URLENCODED).assertBodyEquals("{\"name\":\"AAA\",\"value\":\"BBB\"}");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Example 30 with ForestRequest

use of com.dtflys.forest.http.ForestRequest in project forest by dromara.

the class TestEncoderClient method testMultipart.

@Test
public void testMultipart() {
    EncoderClient.Entry entry = new EncoderClient.Entry("AAA", "BBB");
    server.enqueue(new MockResponse().setBody(EXPECTED));
    ForestRequest request = encoderClient.testMutlipart(entry);
    assertThat(request).isNotNull();
    assertThat(request.bodyType()).isNotNull().isEqualTo(ForestDataType.MULTIPART);
    request.execute();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test) BaseClientTest(com.dtflys.test.http.BaseClientTest)

Aggregations

ForestRequest (com.dtflys.forest.http.ForestRequest)85 Test (org.junit.Test)75 MockResponse (okhttp3.mockwebserver.MockResponse)59 BaseClientTest (com.dtflys.test.http.BaseClientTest)48 HttpBackend (com.dtflys.forest.backend.HttpBackend)42 MockWebServer (okhttp3.mockwebserver.MockWebServer)42 Rule (org.junit.Rule)42 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)39 ForestResponse (com.dtflys.forest.http.ForestResponse)37 CountDownLatch (java.util.concurrent.CountDownLatch)36 BeforeClass (org.junit.BeforeClass)36 MockServerRequest.mockRequest (com.dtflys.forest.mock.MockServerRequest.mockRequest)35 AssertionsForClassTypes.assertThat (org.assertj.core.api.AssertionsForClassTypes.assertThat)35 TimeUnit (java.util.concurrent.TimeUnit)29 AtomicReference (java.util.concurrent.atomic.AtomicReference)29 ContentType (com.dtflys.forest.backend.ContentType)15 File (java.io.File)14 URL (java.net.URL)14 HashMap (java.util.HashMap)14 LinkedHashMap (java.util.LinkedHashMap)14