/** * Sends an HTTP POST request with the specified content. */ default HttpResponse post(String path, String content, Charset charset) { return execute(HttpHeaders.of(HttpMethod.POST, path), content, charset); }
/** * Sends an HTTP PATCH request with the specified content. */ default HttpResponse patch(String path, byte[] content) { return execute(HttpHeaders.of(HttpMethod.PATCH, path), content); }
/** * Creates a new HTTP request with empty content and closes the stream. * * @param method the HTTP method of the request * @param path the path of the request */ static HttpRequest of(HttpMethod method, String path) { requireNonNull(method, "method"); requireNonNull(path, "path"); return of(HttpHeaders.of(method, path)); }
/** * Creates a new {@link HttpResult} with the specified content and the {@link HttpStatus#OK} status. * * @param content the content of the response */ static <T> HttpResult<T> of(T content) { return new DefaultHttpResult<>(HttpHeaders.of(HttpStatus.OK), requireNonNull(content, "content")); }
/** * Returns a copy of the specified {@link HttpHeaders}. */ static HttpHeaders copyOf(HttpHeaders headers) { return of().set(requireNonNull(headers, "headers")); }
/** * Sends an HTTP OPTIONS request. */ default HttpResponse options(String path) { return execute(HttpHeaders.of(HttpMethod.OPTIONS, path)); }
/** * Sends an HTTP HEAD request. */ default HttpResponse head(String path) { return execute(HttpHeaders.of(HttpMethod.HEAD, path)); }
/** * Sends an HTTP PUT request with the specified content. */ default HttpResponse put(String path, HttpData content) { return execute(HttpHeaders.of(HttpMethod.PUT, path), content); }
/** * Creates a new {@link HttpResult} with the specified {@link HttpStatus} and without content. * * @param status the HTTP status */ static <T> HttpResult<T> of(HttpStatus status) { return new DefaultHttpResult<>(HttpHeaders.of(status)); }
private HttpResponse newNotModified(HttpFileAttributes attrs, @Nullable String etag) { final HttpHeaders headers = HttpHeaders.of(HttpStatus.NOT_MODIFIED); return HttpResponse.of(addCommonHeaders(headers, attrs, etag)); } }
@Nullable private HttpHeaders readHeaders(@Nullable HttpFileAttributes attrs) throws IOException { if (attrs == null) { return null; } // TODO(trustin): Cache the headers (sans the 'date' header') if attrs did not change. final String etag = generateEntityTag(attrs); final HttpHeaders headers = HttpHeaders.of(HttpStatus.OK); headers.set(HttpHeaderNames.CONTENT_LENGTH, Long.toString(attrs.length())); return addCommonHeaders(headers, attrs, etag); }
/** * Returns an {@link HttpHeaders} with the specified {@code location}, the default {@code cache-control} * and the default {@code pragma} headers. */ static HttpHeaders headersWithLocation(String location) { return HttpHeaders.of(HttpStatus.FOUND) .add(HttpHeaderNames.LOCATION, location) .add(HttpHeaderNames.CACHE_CONTROL, DEFAULT_CACHE_CONTROL) .add(HttpHeaderNames.PRAGMA, DEFAULT_PRAGMA); }
private void handleOptions(ChannelHandlerContext ctx, DecodedHttpRequest req) { respond(ctx, req, AggregatedHttpMessage.of( HttpHeaders.of(HttpStatus.OK) .set(HttpHeaderNames.ALLOW, ALLOWED_METHODS_STRING)), newEarlyRespondingRequestContext(ctx, req, req.path(), null), null); }
protected HttpResponse echo(AggregatedHttpMessage aReq) { return HttpResponse.of( HttpHeaders.of(HttpStatus.OK), aReq.content()); } }
@Test public void contentTypeNotThrift() throws Exception { // Browser clients often send a non-thrift content type. final HttpHeaders headers = HttpHeaders.of(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=utf-8"); final HelloService.Iface client = Clients.newClient(server.uri(BINARY, "/hello"), HelloService.Iface.class, ClientOption.HTTP_HEADERS.newValue(headers)); assertThat(client.hello("Trustin")).isEqualTo("Hello, Trustin!"); }
@Test public void testAcceptHeaderWithCommaSeparatedMediaTypes() throws Exception { try (TTransport transport = newTransport("http", "/hello", HttpHeaders.of(HttpHeaderNames.ACCEPT, "text/plain, */*"))) { final HelloService.Client client = new HelloService.Client.Factory().getClient( ThriftProtocolFactories.BINARY.getProtocol(transport)); assertThat(client.hello("Trustin")).isEqualTo("Hello, Trustin!"); } }
private static AggregatedHttpMessage makeMetricsRequest() throws ExecutionException, InterruptedException { final HttpClient client = HttpClient.of("http://127.0.0.1:" + server.httpPort()); return client.execute(HttpHeaders.of(HttpMethod.GET, "/internal/prometheus/metrics") .setObject(HttpHeaderNames.ACCEPT, MediaType.PLAIN_TEXT_UTF_8)) .aggregate().get(); }
@Test public void enableTracking() throws Exception { try { RequestContextAssembly.enable(); final HttpClient client = HttpClient.of(rule.uri("/")); assertThat(client.execute(HttpHeaders.of(HttpMethod.GET, "/foo")).aggregate().get().status()) .isEqualTo(HttpStatus.OK); } finally { RequestContextAssembly.disable(); } }
@Bean public ThriftServiceRegistrationBean helloThriftService() { return new ThriftServiceRegistrationBean() .setServiceName("helloService") .setService(THttpService.of((HelloService.Iface) name -> "hello " + name)) .setPath("/thrift") .setDecorators(ImmutableList.of(LoggingService.newDecorator())) .setExampleRequests(Collections.singleton(new hello_args("nameVal"))) .setExampleHeaders(Collections.singleton(HttpHeaders.of( HttpHeaderNames.of("x-additional-header"), "headerVal"))); } }
@Test public void unframed_grpcEncoding() throws Exception { final HttpClient client = HttpClient.of(server.httpUri("/")); final AggregatedHttpMessage response = client.execute( HttpHeaders.of(HttpMethod.POST, UnitTestServiceGrpc.getStaticUnaryCallMethod().getFullMethodName()) .set(HttpHeaderNames.CONTENT_TYPE, "application/protobuf") .set(GrpcHeaderNames.GRPC_ENCODING, "gzip"), REQUEST_MESSAGE.toByteArray()).aggregate().get(); assertThat(response.status()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE); assertNoRpcContent(); }