@Test public void headerIsSetWhenMetadataPresent() { Map<String, String> metadata = new HashMap<>(); metadata.put("cfAppGuid", "123"); metadata.put("cfInstanceIndex", "4"); when(instance.getMetadata()).thenReturn(metadata); HttpRequest transformedRequest = transformer.transformRequest(request, instance); assertThat(transformedRequest.getHeaders().get("foo"), contains("bar", "baz")); assertEquals("123:4", transformedRequest.getHeaders().getFirst("X-CF-APP-INSTANCE")); }
@Override public HttpRequest transformRequest(HttpRequest request, ServiceInstance instance) { Map<String, String> metadata = instance.getMetadata(); if (metadata.containsKey(CF_APP_GUID) && metadata.containsKey(CF_INSTANCE_INDEX)) { final String headerValue = String.format("%s:%s", metadata.get(CF_APP_GUID), metadata.get(CF_INSTANCE_INDEX)); return new HttpRequestWrapper(request) { @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.putAll(super.getHeaders()); headers.add(SURGICAL_ROUTING_HEADER, headerValue); return headers; } }; } return request; }
SurgicalRoutingRequestTransformer implements LoadBalancerRequestTransformer { @Override public HttpRequest transformRequest(HttpRequest request, ServiceInstance instance) { Map<String, String> metadata = instance.getMetadata(); if (metadata.containsKey(CF_APP_GUID) && metadata.containsKey(CF_INSTANCE_INDEX)) { final String headerValue = String.format("%s:%s", metadata.get(CF_APP_GUID), metadata.get(CF_INSTANCE_INDEX)); return new HttpRequestWrapper(request) { @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.putAll(super.getHeaders()); headers.add(SURGICAL_ROUTING_HEADER, headerValue); return headers; } }; } return request; } }
SurgicalRoutingRequestTransformer implements LoadBalancerRequestTransformer { @Override public HttpRequest transformRequest(HttpRequest request, ServiceInstance instance) { Map<String, String> metadata = instance.getMetadata(); if (metadata.containsKey(CF_APP_GUID) && metadata.containsKey(CF_INSTANCE_INDEX)) { final String headerValue = String.format("%s:%s", metadata.get(CF_APP_GUID), metadata.get(CF_INSTANCE_INDEX)); return new HttpRequestWrapper(request) { @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.putAll(super.getHeaders()); headers.add(SURGICAL_ROUTING_HEADER, headerValue); return headers; } }; } return request; }  }
SurgicalRoutingRequestTransformer implements LoadBalancerRequestTransformer { @Override public HttpRequest transformRequest(HttpRequest request, ServiceInstance instance) { Map<String, String> metadata = instance.getMetadata(); if (metadata.containsKey(CF_APP_GUID) && metadata.containsKey(CF_INSTANCE_INDEX)) { final String headerValue = String.format("%s:%s", metadata.get(CF_APP_GUID), metadata.get(CF_INSTANCE_INDEX)); return new HttpRequestWrapper(request) { @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.putAll(super.getHeaders()); headers.add(SURGICAL_ROUTING_HEADER, headerValue); return headers; } }; } return request; }  @Override HttpRequest transformRequest(HttpRequest request, ServiceInstance instance); }
SurgicalRoutingRequestTransformer implements LoadBalancerRequestTransformer { @Override public HttpRequest transformRequest(HttpRequest request, ServiceInstance instance) { Map<String, String> metadata = instance.getMetadata(); if (metadata.containsKey(CF_APP_GUID) && metadata.containsKey(CF_INSTANCE_INDEX)) { final String headerValue = String.format("%s:%s", metadata.get(CF_APP_GUID), metadata.get(CF_INSTANCE_INDEX)); return new HttpRequestWrapper(request) { @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.putAll(super.getHeaders()); headers.add(SURGICAL_ROUTING_HEADER, headerValue); return headers; } }; } return request; }  @Override HttpRequest transformRequest(HttpRequest request, ServiceInstance instance); static final String CF_APP_GUID; static final String CF_INSTANCE_INDEX; static final String SURGICAL_ROUTING_HEADER; }