Search in sources :

Example 11 with ForestResponse

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

the class TestBaseRedirectClient method testNotAutoRedirect_301.

/**
 * ====================================================== 测试非自动重定向 ======================================================
 */
@Test
public void testNotAutoRedirect_301() {
    server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(301));
    server.enqueue(new MockResponse().setBody(EXPECTED));
    AtomicReference<ForestRequest> atomicReq = new AtomicReference<>(null);
    ForestResponse<String> response = redirectClient.testNotAutoRedirect(((redirectReq, prevReq, prevRes) -> {
        atomicReq.set(redirectReq);
    }));
    assertThat(atomicReq.get()).isNull();
    assertThat(response).isNotNull();
    assertThat(response.getStatusCode()).isEqualTo(301);
    assertThat(response.isRedirection()).isTrue();
    assertThat(response.getRedirectionLocation()).isEqualTo("http://localhost:" + server.getPort() + "/b");
    String result = response.redirectionRequest().execute(String.class);
    assertThat(result).isNotNull().isEqualTo(EXPECTED);
    assertThat(atomicReq.get()).isNotNull();
    assertThat(atomicReq.get().getPath()).isEqualTo("/b");
}
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 12 with ForestResponse

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

the class TestBaseRedirectClient method testBaseAutoRedirect_304.

@Test
public void testBaseAutoRedirect_304() {
    server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(304));
    server.enqueue(new MockResponse().setBody(EXPECTED));
    AtomicReference<ForestRequest> atomicReq = new AtomicReference<>(null);
    ForestResponse<String> response = baseRedirectClient.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);
    mockRequest(server).assertPathEquals("/");
    mockRequest(server).assertPathEquals("/b").assertBodyEquals("body=" + RedirectInterceptor.BODY);
}
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 13 with ForestResponse

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

the class TestBaseRedirectClient method testNotAutoRedirect_307.

@Test
public void testNotAutoRedirect_307() {
    server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(307));
    server.enqueue(new MockResponse().setBody(EXPECTED));
    AtomicReference<ForestRequest> atomicReq = new AtomicReference<>(null);
    ForestResponse<String> response = redirectClient.testNotAutoRedirect(((redirectReq, prevReq, prevRes) -> {
        atomicReq.set(redirectReq);
    }));
    assertThat(atomicReq.get()).isNull();
    assertThat(response).isNotNull();
    assertThat(response.getStatusCode()).isEqualTo(307);
    assertThat(response.isRedirection()).isTrue();
    assertThat(response.getRedirectionLocation()).isEqualTo("http://localhost:" + server.getPort() + "/b");
    String result = response.redirectionRequest().execute(String.class);
    assertThat(result).isNotNull().isEqualTo(EXPECTED);
    assertThat(atomicReq.get()).isNotNull();
    assertThat(atomicReq.get().path()).isEqualTo("/b");
}
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 14 with ForestResponse

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

the class TestBaseRedirectClient method testAutoRedirect_306.

@Test
public void testAutoRedirect_306() {
    server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(306));
    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 15 with ForestResponse

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

the class TestBaseRedirectClient method testAutoRedirect_302.

@Test
public void testAutoRedirect_302() {
    server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(302));
    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)

Aggregations

ForestResponse (com.dtflys.forest.http.ForestResponse)46 ForestRequest (com.dtflys.forest.http.ForestRequest)38 Test (org.junit.Test)37 MockResponse (okhttp3.mockwebserver.MockResponse)34 BaseClientTest (com.dtflys.test.http.BaseClientTest)31 HttpBackend (com.dtflys.forest.backend.HttpBackend)30 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)30 AtomicReference (java.util.concurrent.atomic.AtomicReference)30 MockWebServer (okhttp3.mockwebserver.MockWebServer)30 BeforeClass (org.junit.BeforeClass)30 Rule (org.junit.Rule)30 MockServerRequest.mockRequest (com.dtflys.forest.mock.MockServerRequest.mockRequest)29 CountDownLatch (java.util.concurrent.CountDownLatch)29 TimeUnit (java.util.concurrent.TimeUnit)29 AssertionsForClassTypes.assertThat (org.assertj.core.api.AssertionsForClassTypes.assertThat)29 ForestNetworkException (com.dtflys.forest.exceptions.ForestNetworkException)3 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)3 HttpclientForestResponseFactory (com.dtflys.forest.backend.httpclient.response.HttpclientForestResponseFactory)2 ForestRetryException (com.dtflys.forest.exceptions.ForestRetryException)2 Date (java.util.Date)2