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));
}
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));
}
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);
}
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"));
}
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();
}
Aggregations