use of org.apache.hc.core5.http.message.BasicHttpResponse in project httpcomponents-core by apache.
the class TestBasicMessages method testSetResponseStatus.
@Test
public void testSetResponseStatus() {
final HttpResponse response1 = new BasicHttpResponse(200, "OK");
Assertions.assertNotNull(response1.getCode());
Assertions.assertEquals(200, response1.getCode());
final HttpResponse response2 = new BasicHttpResponse(HttpStatus.SC_BAD_REQUEST, "Bad Request");
Assertions.assertEquals(HttpStatus.SC_BAD_REQUEST, response2.getCode());
final HttpResponse response3 = new BasicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "whatever");
Assertions.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response3.getCode());
Assertions.assertEquals("whatever", response3.getReasonPhrase());
final HttpResponse response4 = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
Assertions.assertThrows(IllegalArgumentException.class, () -> response4.setCode(-23));
}
use of org.apache.hc.core5.http.message.BasicHttpResponse in project httpcomponents-core by apache.
the class TestBasicMessageBuilders method testResponseBasics.
@Test
public void testResponseBasics() throws Exception {
final BasicResponseBuilder builder = BasicResponseBuilder.create(200);
Assertions.assertEquals(200, builder.getStatus());
Assertions.assertNull(builder.getHeaders());
Assertions.assertNull(builder.getVersion());
final BasicHttpResponse r1 = builder.build();
Assertions.assertNotNull(r1);
Assertions.assertEquals(200, r1.getCode());
Assertions.assertNull(r1.getVersion());
builder.setStatus(500);
builder.setVersion(HttpVersion.HTTP_1_0);
Assertions.assertEquals(500, builder.getStatus());
Assertions.assertEquals(HttpVersion.HTTP_1_0, builder.getVersion());
final BasicHttpResponse r2 = builder.build();
Assertions.assertEquals(500, r2.getCode());
Assertions.assertEquals(HttpVersion.HTTP_1_0, r2.getVersion());
builder.addHeader("h1", "v1");
builder.addHeader("h1", "v2");
builder.addHeader("h2", "v2");
assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2"), new BasicHeader("h2", "v2")));
assertThat(builder.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2")));
assertThat(builder.getFirstHeader("h1"), HeaderMatcher.same("h1", "v1"));
assertThat(builder.getLastHeader("h1"), HeaderMatcher.same("h1", "v2"));
final BasicHttpResponse r3 = builder.build();
assertThat(r3.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2"), new BasicHeader("h2", "v2")));
assertThat(r3.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2")));
assertThat(r3.getFirstHeader("h1"), HeaderMatcher.same("h1", "v1"));
assertThat(r3.getLastHeader("h1"), HeaderMatcher.same("h1", "v2"));
builder.removeHeader(new BasicHeader("h1", "v2"));
assertThat(builder.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1")));
assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h2", "v2")));
final BasicHttpResponse r4 = builder.build();
assertThat(r4.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1")));
assertThat(r4.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h2", "v2")));
builder.removeHeaders("h1");
assertThat(builder.getHeaders("h1"), HeadersMatcher.same());
assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h2", "v2")));
final BasicHttpResponse r5 = builder.build();
assertThat(r5.getHeaders("h1"), HeadersMatcher.same());
assertThat(r5.getHeaders(), HeadersMatcher.same(new BasicHeader("h2", "v2")));
}
use of org.apache.hc.core5.http.message.BasicHttpResponse in project httpcomponents-core by apache.
the class TestBasicMessageBuilders method testResponseCopy.
@Test
public void testResponseCopy() throws Exception {
final HttpResponse response = new BasicHttpResponse(400);
response.addHeader("h1", "v1");
response.addHeader("h1", "v2");
response.addHeader("h2", "v2");
response.setVersion(HttpVersion.HTTP_2);
final BasicResponseBuilder builder = BasicResponseBuilder.copy(response);
Assertions.assertEquals(400, builder.getStatus());
Assertions.assertEquals(HttpVersion.HTTP_2, builder.getVersion());
assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2"), new BasicHeader("h2", "v2")));
}
use of org.apache.hc.core5.http.message.BasicHttpResponse in project httpcomponents-core by apache.
the class AbstractAsyncServerAuthFilter method handle.
@Override
public final AsyncDataConsumer handle(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context, final AsyncFilterChain.ResponseTrigger responseTrigger, final AsyncFilterChain chain) throws HttpException, IOException {
final Header h = request.getFirstHeader(HttpHeaders.AUTHORIZATION);
final T challengeResponse = h != null ? parseChallengeResponse(h.getValue(), context) : null;
final URIAuthority authority = request.getAuthority();
final String requestUri = request.getRequestUri();
final boolean authenticated = authenticate(challengeResponse, authority, requestUri, context);
final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
final boolean expectContinue = expect != null && HeaderElements.CONTINUE.equalsIgnoreCase(expect.getValue());
if (authenticated) {
if (expectContinue) {
responseTrigger.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE));
}
return chain.proceed(request, entityDetails, context, responseTrigger);
}
final HttpResponse unauthorized = new BasicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, authority, requestUri, context));
final AsyncEntityProducer responseContentProducer = generateResponseContent(unauthorized);
if (respondImmediately || expectContinue || entityDetails == null) {
responseTrigger.submitResponse(unauthorized, responseContentProducer);
return null;
}
return new AsyncDataConsumer() {
@Override
public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
capacityChannel.update(Integer.MAX_VALUE);
}
@Override
public void consume(final ByteBuffer src) throws IOException {
}
@Override
public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
responseTrigger.submitResponse(unauthorized, responseContentProducer);
}
@Override
public void releaseResources() {
if (responseContentProducer != null) {
responseContentProducer.releaseResources();
}
}
};
}
use of org.apache.hc.core5.http.message.BasicHttpResponse in project httpcomponents-core by apache.
the class TerminalAsyncServerFilter method handle.
@Override
public AsyncDataConsumer handle(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context, final AsyncFilterChain.ResponseTrigger responseTrigger, final AsyncFilterChain chain) throws HttpException, IOException {
final AsyncServerExchangeHandler exchangeHandler = handlerFactory.create(request, context);
if (exchangeHandler != null) {
exchangeHandler.handleRequest(request, entityDetails, new ResponseChannel() {
@Override
public void sendInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
responseTrigger.sendInformation(response);
}
@Override
public void sendResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
responseTrigger.submitResponse(response, entityDetails != null ? new AsyncEntityProducer() {
@Override
public void failed(final Exception cause) {
exchangeHandler.failed(cause);
}
@Override
public boolean isRepeatable() {
return false;
}
@Override
public long getContentLength() {
return entityDetails.getContentLength();
}
@Override
public String getContentType() {
return entityDetails.getContentType();
}
@Override
public String getContentEncoding() {
return entityDetails.getContentEncoding();
}
@Override
public boolean isChunked() {
return entityDetails.isChunked();
}
@Override
public Set<String> getTrailerNames() {
return entityDetails.getTrailerNames();
}
@Override
public int available() {
return exchangeHandler.available();
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
exchangeHandler.produce(channel);
}
@Override
public void releaseResources() {
exchangeHandler.releaseResources();
}
} : null);
}
@Override
public void pushPromise(final HttpRequest promise, final AsyncPushProducer pushProducer, final HttpContext httpContext) throws HttpException, IOException {
responseTrigger.pushPromise(promise, pushProducer);
}
}, context);
return exchangeHandler;
}
responseTrigger.submitResponse(new BasicHttpResponse(HttpStatus.SC_NOT_FOUND), AsyncEntityProducers.create("Not found"));
return null;
}
Aggregations