@Test public void canModifyPreviouslyCreatedRequest() { LiveHttpRequest request = get("/foo") .header("remove", "remove") .build(); LiveHttpRequest newRequest = request.newBuilder() .uri("/home") .header("remove", "notanymore") .build(); assertThat(newRequest.url().path(), is("/home")); assertThat(newRequest.headers(), hasItem(header("remove", "notanymore"))); }
@Test public void transformsHeader() { LiveHttpRequest request = LiveHttpRequest.get("/").header("X-Styx-ID", "test").build() .newBuilder() .header("X-Styx-ID", "bar") .build(); assertEquals(request.header("X-Styx-ID"), Optional.of("bar")); }
private LiveHttpRequest enrich(LiveHttpRequest request, Context context) { LiveHttpRequest.Transformer builder = request.newBuilder(); xForwardedFor(request, context) .ifPresent(headerValue -> builder.header(X_FORWARDED_FOR, headerValue)); return builder .header(requestIdHeaderName, request.id()) .header(X_FORWARDED_PROTO, xForwardedProto(request, context.isSecure())) .build(); }
@Override public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) { String header = xHcomPluginsHeader(request); final String configPath = environment.pluginConfig(String.class); String pluginsList = environment.configuration().get("plugins.active").get(); LiveHttpRequest newRequest = request.newBuilder() .header(X_HCOM_PLUGINS_HEADER, header) .header(X_HCOM_PLUGINS_CONFIGURATION_PATH, configPath) .header(X_HCOM_PLUGINS_LIST, pluginsList) .header("X-Hcom-Styx-Started", styxStarted) .header("X-Hcom-Styx-Stopped", styxStopped) .build(); Function<ByteBuf, String> byteBufStringFunction = byteBuf -> byteBuf.toString(Charsets.UTF_8); return chain.proceed(newRequest) .flatMap(response -> response.aggregate(1 * 1024 * 1024)) .map(response -> response.newBuilder() .header(X_HCOM_PLUGINS_HEADER, header) .header(X_HCOM_PLUGINS_CONFIGURATION_PATH, configPath) .header(X_HCOM_PLUGINS_LIST, pluginsList) .header("X-Hcom-Styx-Started", styxStarted) .header("X-Hcom-Styx-Stopped", styxStopped) .build()) .map(HttpResponse::stream); }
@Override public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) { return request.aggregate(config.maxContentLength()) .flatMap(fullHttpRequest -> Eventual.from(asyncOperation(config.delayMillis()))) .map(outcome -> request.newBuilder().header("X-Outcome", outcome.result())) .flatMap(x -> chain.proceed(request)); } }
@Override public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) { LiveHttpRequest newRequest = request.newBuilder() .header(VIA, viaHeader(request)) .build(); return chain.proceed(newRequest) .map(response -> response.newBuilder() .header(VIA, viaHeader(response)) .build()); }
private static LiveHttpRequest addUserAgent(String userAgent, LiveHttpRequest request) { if (userAgent != null) { return request.newBuilder() .header(USER_AGENT, userAgent) .build(); } else { return request; } }