Search in sources :

Example 6 with Request

use of feign.Request in project feign by OpenFeign.

the class LBClientTest method testRibbonRequest.

@Test
public void testRibbonRequest() throws URISyntaxException {
    // test for RibbonRequest.toRequest()
    // the url has a query whose value is an encoded json string
    String urlWithEncodedJson = "http://test.feign.com/p?q=%7b%22a%22%3a1%7d";
    HttpMethod method = HttpMethod.GET;
    URI uri = new URI(urlWithEncodedJson);
    Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
    // create a Request for recreating another Request by toRequest()
    Request requestOrigin = Request.create(method, uri.toASCIIString(), headers, null, Charset.forName("utf-8"));
    RibbonRequest ribbonRequest = new RibbonRequest(null, requestOrigin, uri);
    // use toRequest() recreate a Request
    Request requestRecreate = ribbonRequest.toRequest();
    // test that requestOrigin and requestRecreate are same except the header 'Content-Length'
    // ps, requestOrigin and requestRecreate won't be null
    assertThat(requestOrigin.toString()).contains(String.format("%s %s HTTP/1.1\n", method, urlWithEncodedJson));
    assertThat(requestRecreate.toString()).contains(String.format("%s %s HTTP/1.1\nContent-Length: 0\n", method, urlWithEncodedJson));
}
Also used : Request(feign.Request) RibbonRequest(feign.ribbon.LBClient.RibbonRequest) Collection(java.util.Collection) RibbonRequest(feign.ribbon.LBClient.RibbonRequest) URI(java.net.URI) HttpMethod(feign.Request.HttpMethod) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 7 with Request

use of feign.Request in project incubator-skywalking by apache.

the class DefaultHttpClientInterceptor method beforeMethod.

/**
 * Get the {@link feign.Request} from {@link EnhancedInstance}, then create {@link AbstractSpan} and set host, port,
 * kind, component, url from {@link feign.Request}. Through the reflection of the way, set the http header of
 * context data into {@link feign.Request#headers}.
 *
 * @param method
 * @param result change this result, if you want to truncate the method.
 * @throws Throwable
 */
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    Request request = (Request) allArguments[0];
    URL url = new URL(request.url());
    ContextCarrier contextCarrier = new ContextCarrier();
    int port = url.getPort() == -1 ? 80 : url.getPort();
    String remotePeer = url.getHost() + ":" + port;
    String operationName = url.getPath();
    if (operationName == null || operationName.length() == 0) {
        operationName = "/";
    }
    AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, remotePeer);
    span.setComponent(ComponentsDefine.FEIGN);
    Tags.HTTP.METHOD.set(span, request.method());
    Tags.URL.set(span, request.url());
    SpanLayer.asHttp(span);
    Field headersField = Request.class.getDeclaredField("headers");
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(headersField, headersField.getModifiers() & ~Modifier.FINAL);
    headersField.setAccessible(true);
    Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        List<String> contextCollection = new LinkedList<String>();
        contextCollection.add(next.getHeadValue());
        headers.put(next.getHeadKey(), contextCollection);
    }
    headers.putAll(request.headers());
    headersField.set(request, Collections.unmodifiableMap(headers));
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) Request(feign.Request) URL(java.net.URL) LinkedList(java.util.LinkedList) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan) LinkedHashMap(java.util.LinkedHashMap) Field(java.lang.reflect.Field) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) Collection(java.util.Collection)

Example 8 with Request

use of feign.Request in project spring-cloud-sleuth by spring-cloud.

the class TraceFeignAspect method executeTraceFeignClient.

Object executeTraceFeignClient(Object bean, ProceedingJoinPoint pjp) throws IOException {
    Object[] args = pjp.getArgs();
    Request request = (Request) args[0];
    Request.Options options = (Request.Options) args[1];
    return ((Client) bean).execute(request, options);
}
Also used : Request(feign.Request) Client(feign.Client)

Example 9 with Request

use of feign.Request in project feign by OpenFeign.

the class WhatShouldWeCacheBenchmarks method setup.

@Setup
public void setup() {
    feignContract = new Contract.Default();
    cachedContact = new Contract() {

        private final List<MethodMetadata> cached = new Default().parseAndValidateMetadata(FeignTestInterface.class);

        public List<MethodMetadata> parseAndValidateMetadata(Class<?> declaring) {
            return cached;
        }
    };
    fakeClient = new Client() {

        public Response execute(Request request, Request.Options options) throws IOException {
            Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
            return Response.builder().body((byte[]) null).status(200).headers(headers).reason("ok").request(request).build();
        }
    };
    cachedFakeFeign = Feign.builder().client(fakeClient).build();
    cachedFakeApi = cachedFakeFeign.newInstance(new HardCodedTarget<FeignTestInterface>(FeignTestInterface.class, "http://localhost"));
}
Also used : Request(feign.Request) HardCodedTarget(feign.Target.HardCodedTarget) IOException(java.io.IOException) Response(feign.Response) Collection(java.util.Collection) MethodMetadata(feign.MethodMetadata) List(java.util.List) Client(feign.Client) Contract(feign.Contract) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Setup(org.openjdk.jmh.annotations.Setup)

Example 10 with Request

use of feign.Request in project feign by OpenFeign.

the class Http2ClientAsyncTest method throwsFeignExceptionIncludingBody.

@Test
public void throwsFeignExceptionIncludingBody() throws Throwable {
    server.enqueue(new MockResponse().setBody("success!"));
    final TestInterfaceAsync api = newAsyncBuilder().decoder((response, type) -> {
        throw new IOException("timeout");
    }).target("http://localhost:" + server.getPort());
    final CompletableFuture<?> cf = api.body("Request body");
    server.takeRequest();
    try {
        unwrap(cf);
    } catch (final FeignException e) {
        Assertions.assertThat(e.getMessage()).isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
        Assertions.assertThat(e.contentUTF8()).isEqualTo("Request body");
        return;
    }
    fail();
}
Also used : ErrorDecoder(feign.codec.ErrorDecoder) Arrays(java.util.Arrays) Body(feign.Body) TypeToken(com.google.gson.reflect.TypeToken) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Decoder(feign.codec.Decoder) RequestInterceptor(feign.RequestInterceptor) FieldQueryMapEncoder(feign.querymap.FieldQueryMapEncoder) HttpMethod(feign.Request.HttpMethod) Gson(com.google.gson.Gson) AsyncFeign(feign.AsyncFeign) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) Assertions(org.assertj.core.api.Assertions) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) AsyncClient(feign.AsyncClient) FeignException(feign.FeignException) BeanQueryMapEncoder(feign.querymap.BeanQueryMapEncoder) Collection(java.util.Collection) RequestLine(feign.RequestLine) PropertyPojo(feign.PropertyPojo) Feign(feign.Feign) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) Encoder(feign.codec.Encoder) List(java.util.List) Type(java.lang.reflect.Type) MockWebServerAssertions.assertThat(feign.assertj.MockWebServerAssertions.assertThat) Http2Client(feign.http2client.Http2Client) ChildPojo(feign.ChildPojo) MockResponse(okhttp3.mockwebserver.MockResponse) MapEntry.entry(org.assertj.core.data.MapEntry.entry) RequestTemplate(feign.RequestTemplate) CoreMatchers.isA(org.hamcrest.CoreMatchers.isA) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Headers(feign.Headers) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseMappingDecoder(feign.Feign.ResponseMappingDecoder) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) StringDecoder(feign.codec.StringDecoder) Util(feign.Util) NoSuchElementException(java.util.NoSuchElementException) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) QueryMapEncoder(feign.QueryMapEncoder) ResponseMapper(feign.ResponseMapper) Response(feign.Response) Buffer(okio.Buffer) Target(feign.Target) HeaderMap(feign.HeaderMap) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) QueryMap(feign.QueryMap) HardCodedTarget(feign.Target.HardCodedTarget) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) Param(feign.Param) DecodeException(feign.codec.DecodeException) Request(feign.Request) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) EncodeException(feign.codec.EncodeException) MockResponse(okhttp3.mockwebserver.MockResponse) FeignException(feign.FeignException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Request (feign.Request)13 Test (org.junit.Test)9 Response (feign.Response)7 Client (feign.Client)6 RequestLine (feign.RequestLine)5 Collection (java.util.Collection)5 IOException (java.io.IOException)4 Charset (java.nio.charset.Charset)4 MockWebServer (okhttp3.mockwebserver.MockWebServer)4 Rule (org.junit.Rule)4 Feign (feign.Feign)3 FeignException (feign.FeignException)3 Param (feign.Param)3 QueryMap (feign.QueryMap)3 QueryMapEncoder (feign.QueryMapEncoder)3 RequestInterceptor (feign.RequestInterceptor)3 RequestTemplate (feign.RequestTemplate)3 ResponseMapper (feign.ResponseMapper)3 Decoder (feign.codec.Decoder)3 HashMap (java.util.HashMap)3