use of cn.taketoday.http.client.support.HttpRequestDecorator in project today-infrastructure by TAKETODAY.
the class InterceptingClientHttpRequestFactoryTests method changeURI.
@Test
public void changeURI() throws Exception {
final URI changedUri = new URI("https://example.com/2");
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
return execution.execute(new HttpRequestDecorator(request) {
@Override
public URI getURI() {
return changedUri;
}
}, body);
}
};
requestFactoryMock = new RequestFactoryMock() {
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
assertThat(uri).isEqualTo(changedUri);
return super.createRequest(uri, httpMethod);
}
};
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
ClientHttpRequest request = requestFactory.createRequest(new URI("https://example.com"), HttpMethod.GET);
request.execute();
}
use of cn.taketoday.http.client.support.HttpRequestDecorator in project today-infrastructure by TAKETODAY.
the class InterceptingClientHttpRequestFactoryTests method changeHeaders.
@Test
public void changeHeaders() throws Exception {
final String headerName = "Foo";
final String headerValue = "Bar";
final String otherValue = "Baz";
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpRequestDecorator wrapper = new HttpRequestDecorator(request);
wrapper.getHeaders().add(headerName, otherValue);
return execution.execute(wrapper, body);
}
};
requestMock = new RequestMock() {
@Override
public ClientHttpResponse execute() throws IOException {
List<String> headerValues = getHeaders().get(headerName);
assertThat(headerValues.size()).isEqualTo(2);
assertThat(headerValues.get(0)).isEqualTo(headerValue);
assertThat(headerValues.get(1)).isEqualTo(otherValue);
return super.execute();
}
};
requestMock.getHeaders().add(headerName, headerValue);
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
ClientHttpRequest request = requestFactory.createRequest(new URI("https://example.com"), HttpMethod.GET);
request.execute();
}
use of cn.taketoday.http.client.support.HttpRequestDecorator in project today-framework by TAKETODAY.
the class InterceptingClientHttpRequestFactoryTests method changeMethod.
@Test
public void changeMethod() throws Exception {
final HttpMethod changedMethod = HttpMethod.POST;
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
return execution.execute(new HttpRequestDecorator(request) {
@Override
public HttpMethod getMethod() {
return changedMethod;
}
}, body);
}
};
requestFactoryMock = new RequestFactoryMock() {
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
assertThat(httpMethod).isEqualTo(changedMethod);
return super.createRequest(uri, httpMethod);
}
};
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
ClientHttpRequest request = requestFactory.createRequest(new URI("https://example.com"), HttpMethod.GET);
request.execute();
}
use of cn.taketoday.http.client.support.HttpRequestDecorator in project today-framework by TAKETODAY.
the class InterceptingClientHttpRequestFactoryTests method changeHeaders.
@Test
public void changeHeaders() throws Exception {
final String headerName = "Foo";
final String headerValue = "Bar";
final String otherValue = "Baz";
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpRequestDecorator wrapper = new HttpRequestDecorator(request);
wrapper.getHeaders().add(headerName, otherValue);
return execution.execute(wrapper, body);
}
};
requestMock = new RequestMock() {
@Override
public ClientHttpResponse execute() throws IOException {
List<String> headerValues = getHeaders().get(headerName);
assertThat(headerValues.size()).isEqualTo(2);
assertThat(headerValues.get(0)).isEqualTo(headerValue);
assertThat(headerValues.get(1)).isEqualTo(otherValue);
return super.execute();
}
};
requestMock.getHeaders().add(headerName, headerValue);
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
ClientHttpRequest request = requestFactory.createRequest(new URI("https://example.com"), HttpMethod.GET);
request.execute();
}
use of cn.taketoday.http.client.support.HttpRequestDecorator in project today-framework by TAKETODAY.
the class InterceptingClientHttpRequestFactoryTests method changeURI.
@Test
public void changeURI() throws Exception {
final URI changedUri = new URI("https://example.com/2");
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
return execution.execute(new HttpRequestDecorator(request) {
@Override
public URI getURI() {
return changedUri;
}
}, body);
}
};
requestFactoryMock = new RequestFactoryMock() {
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
assertThat(uri).isEqualTo(changedUri);
return super.createRequest(uri, httpMethod);
}
};
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
ClientHttpRequest request = requestFactory.createRequest(new URI("https://example.com"), HttpMethod.GET);
request.execute();
}
Aggregations