Search in sources :

Example 76 with ForestRequest

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

the class TestRequest method testUrl.

@Test
public void testUrl() {
    ForestRequest request = Forest.request();
    assertThat(request.getUrl()).isEqualTo("http://localhost/");
    assertThat(request.host()).isEqualTo("localhost");
    assertThat(request.port()).isEqualTo(80);
    assertThat(request.getPath()).isEqualTo("/");
    request.url("http://127.0.0.1:8080/xxx/yyy");
    assertThat(request.urlString()).isEqualTo("http://127.0.0.1:8080/xxx/yyy");
    assertThat(request.host()).isEqualTo("127.0.0.1");
    assertThat(request.port()).isEqualTo(8080);
    assertThat(request.getPath()).isEqualTo("/xxx/yyy");
}
Also used : ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test)

Example 77 with ForestRequest

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

the class TestGetClient method testPath3.

@Test
public void testPath3() {
    ForestRequest request = getClient.testPath3();
    assertThat(request).isNotNull();
    assertThat(request.urlString()).isEqualTo("https://localhost/xxx:yyy");
    System.out.println(request.urlString());
}
Also used : ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test)

Example 78 with ForestRequest

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

the class TestGetClient method testPath4.

@Test
public void testPath4() {
    ForestRequest request = getClient.testPath4();
    assertThat(request).isNotNull();
    assertThat(request.urlString()).isEqualTo("https://localhost/xxx:111");
    System.out.println(request.urlString());
}
Also used : ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test)

Example 79 with ForestRequest

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

the class TestGetClient method testPath_userInfo.

@Test
public void testPath_userInfo() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    ForestRequest request = getClient.testPath_userInfo();
    assertThat(request).isNotNull();
    assertThat(request.urlString()).isEqualTo("http://aaa%2Fbbb%2Fskip:123456@localhost:" + server.getPort());
    System.out.println(request.urlString());
    assertThat(request.executeAsString()).isNotNull().isEqualTo(EXPECTED);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestRequest(com.dtflys.forest.http.ForestRequest) Test(org.junit.Test)

Example 80 with ForestRequest

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

the class ForestMethod method invoke.

/**
 * 调用方法
 * @param args 调用本对象对应方法时传入的参数数组
 * @return 调用本对象对应方法结束后返回的值,任意类型的对象实例
 */
public Object invoke(Object[] args) {
    Type rType = this.getReturnType();
    ForestRequest request = makeRequest(args);
    MethodLifeCycleHandler<T> lifeCycleHandler = null;
    request.setBackend(configuration.getBackend());
    // 如果返回类型为ForestRequest,直接返回请求对象
    if (ForestRequest.class.isAssignableFrom(returnClass)) {
        Type retType = getReturnType();
        if (retType instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) retType;
            Type[] genTypes = parameterizedType.getActualTypeArguments();
            if (genTypes.length > 0) {
                Type targetType = genTypes[0];
                rType = targetType;
            } else {
                rType = String.class;
            }
            if (rType instanceof WildcardType) {
                WildcardType wildcardType = (WildcardType) rType;
                Type[] bounds = wildcardType.getUpperBounds();
                if (bounds.length > 0) {
                    rType = bounds[0];
                } else {
                    rType = String.class;
                }
            }
            Type successType = rType;
            if (onSuccessClassGenericType != null) {
                successType = onSuccessClassGenericType;
            }
            lifeCycleHandler = new MethodLifeCycleHandler<>(rType, successType);
            request.setLifeCycleHandler(lifeCycleHandler);
            lifeCycleHandler.handleInvokeMethod(request, this, args);
            return request;
        } else {
            lifeCycleHandler = new MethodLifeCycleHandler<>(rType, onSuccessClassGenericType);
        }
        request.setLifeCycleHandler(lifeCycleHandler);
        lifeCycleHandler.handleInvokeMethod(request, this, args);
        return request;
    }
    lifeCycleHandler = new MethodLifeCycleHandler<>(rType, onSuccessClassGenericType);
    request.setLifeCycleHandler(lifeCycleHandler);
    lifeCycleHandler.handleInvokeMethod(request, this, args);
    return request.execute();
}
Also used : ForestRequestType(com.dtflys.forest.http.ForestRequestType) ContentType(com.dtflys.forest.backend.ContentType) ForestDataType(com.dtflys.forest.utils.ForestDataType) ForestRequest(com.dtflys.forest.http.ForestRequest)

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