use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestBaseRedirectClient method testBaseNotAutoRedirect_302.
@Test
public void testBaseNotAutoRedirect_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 = baseRedirectClient.testNotAutoRedirect(((redirectReq, prevReq, prevRes) -> {
atomicReq.set(redirectReq);
}));
assertThat(response).isNotNull();
assertThat(response.getStatusCode()).isEqualTo(302);
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);
mockRequest(server).assertPathEquals("/");
mockRequest(server).assertPathEquals("/b").assertBodyEquals("body=" + RedirectInterceptor.BODY);
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestBaseRedirectClient method testAsyncAutoRedirect.
/**
* ====================================================== 测试异步自动重定向 ======================================================
*/
@Test
public void testAsyncAutoRedirect() throws InterruptedException {
server.enqueue(new MockResponse().addHeader("Location", "http://localhost:" + server.getPort() + "/b").setResponseCode(301));
server.enqueue(new MockResponse().setBody(EXPECTED));
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<ForestRequest> atomicReq = new AtomicReference<>(null);
AtomicReference<ForestResponse> atomicRes = new AtomicReference<>(null);
redirectClient.testAutoRedirect_async(((redirectReq, prevReq, prevRes) -> {
atomicReq.set(redirectReq);
}), ((data, req, res) -> {
atomicRes.set(res);
latch.countDown();
}));
latch.await(10, TimeUnit.SECONDS);
assertThat(atomicReq.get()).isNotNull();
assertThat(atomicReq.get().path()).isEqualTo("/b");
assertThat(atomicRes.get()).isNotNull();
assertThat(atomicRes.get().getStatusCode()).isEqualTo(200);
String result = atomicRes.get().getContent();
assertThat(result).isNotNull().isEqualTo(EXPECTED);
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestEncoderClient method testEncoder2.
@Test
public void testEncoder2() {
EncoderClient.Entry entry = new EncoderClient.Entry("AAA", "BBB");
server.enqueue(new MockResponse().setBody(EXPECTED));
ForestRequest request = encoderClient.testEncoder2("json", entry);
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\"}");
server.enqueue(new MockResponse().setBody(EXPECTED));
request = encoderClient.testEncoder2("xml", entry);
assertThat(request).isNotNull();
assertThat(request.bodyType()).isNotNull().isEqualTo(ForestDataType.XML);
request.execute();
mockRequest(server).assertHeaderEquals("Content-Type", ContentType.APPLICATION_X_WWW_FORM_URLENCODED).assertBodyEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<entry>\n" + " <name>AAA</name>\n" + " <value>BBB</value>\n" + "</entry>\n");
server.enqueue(new MockResponse().setBody(EXPECTED));
request = encoderClient.testEncoder2("form", entry);
assertThat(request).isNotNull();
assertThat(request.bodyType()).isNotNull().isEqualTo(ForestDataType.FORM);
request.execute();
mockRequest(server).assertHeaderEquals("Content-Type", ContentType.APPLICATION_X_WWW_FORM_URLENCODED).assertBodyEquals("name=AAA&value=BBB");
server.enqueue(new MockResponse().setBody(EXPECTED));
request = encoderClient.testEncoder2("text", entry);
assertThat(request).isNotNull();
assertThat(request.bodyType()).isNotNull().isEqualTo(ForestDataType.TEXT);
request.execute();
mockRequest(server).assertHeaderEquals("Content-Type", ContentType.APPLICATION_X_WWW_FORM_URLENCODED).assertBodyEquals("Entry{name='AAA', value='BBB'}");
server.enqueue(new MockResponse().setBody(EXPECTED));
request = encoderClient.testEncoder2("binary", entry);
assertThat(request).isNotNull();
assertThat(request.bodyType()).isNotNull().isEqualTo(ForestDataType.BINARY);
request.execute();
mockRequest(server).assertHeaderEquals("Content-Type", ContentType.APPLICATION_X_WWW_FORM_URLENCODED).assertBodyEquals("Entry{name='AAA', value='BBB'}".getBytes(StandardCharsets.UTF_8));
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestEncoderClient method testEncoder3.
@Test
public void testEncoder3() {
EncoderClient.Entry entry = new EncoderClient.Entry("AAA", "BBB");
server.enqueue(new MockResponse().setBody(EXPECTED));
ForestRequest request = encoderClient.testEncoder3(entry);
assertThat(request).isNotNull();
assertThat(request.bodyType()).isNotNull().isEqualTo(ForestDataType.JSON);
assertThat(request.getEncoder()).isNotNull().isInstanceOf(MyEncoder.class);
request.execute();
mockRequest(server).assertHeaderEquals("Content-Type", ContentType.APPLICATION_X_WWW_FORM_URLENCODED).assertBodyEquals("Data: " + entry);
}
use of com.dtflys.forest.http.ForestRequest in project forest by dromara.
the class TestEncoderClient method testFastjson.
@Test
public void testFastjson() {
EncoderClient.Entry entry = new EncoderClient.Entry("AAA", "BBB");
server.enqueue(new MockResponse().setBody(EXPECTED));
ForestRequest request = encoderClient.testFastjson(entry);
assertThat(request).isNotNull();
assertThat(request.bodyType()).isNotNull().isEqualTo(ForestDataType.JSON);
assertThat(request.getEncoder()).isNotNull().isInstanceOf(ForestFastjsonConverter.class);
request.execute();
mockRequest(server).assertBodyEquals("{\"name\":\"AAA\",\"value\":\"BBB\"}");
}
Aggregations