PatternHttpHeaderValueReplacingModifier implements HeaderModifier { @Override public HttpHeaders modify(HttpHeaders httpHeaders) { final HttpHeaders newHttpHeaders = new HttpHeaders(); for (String headerKey : httpHeaders.keySet()) { for (String headerValue : httpHeaders.get(headerKey)) { Matcher matcher = this.headerKeyPattern.matcher(headerValue); if (matcher.matches()) { matcher.reset(); StringBuilder builder = new StringBuilder(); int previous = 0; while (matcher.find()) { String prefix; if (matcher.groupCount() > 0) { prefix = headerValue.substring(previous, matcher.start(1)); previous = matcher.end(1); } else { prefix = headerValue.substring(previous, matcher.start()); previous = matcher.end(); } builder.append(prefix); builder.append(this.replacement); } if (previous < headerValue.length()) { builder.append(headerValue.substring(previous)); } newHttpHeaders.set(headerKey, builder.toString()); } else { newHttpHeaders.set(headerKey, headerValue); } } } return newHttpHeaders; } PatternHttpHeaderValueReplacingModifier(Pattern headerKeyPattern, String replacement); @Override HttpHeaders modify(HttpHeaders httpHeaders);  }
@Test public void shouldReplaceHeaderBasedOnPattern() throws Exception { final HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.set("Location", "http: final HttpHeaders modified = sut.modify(httpHeaders); assertThat(modified.get("Location"), hasItem("http: }
@Test public void shouldNotTouchNonMatchingHeaders() throws Exception { final HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); final HttpHeaders modified = sut.modify(httpHeaders); assertThat(modified.getContentType(), is(MediaType.APPLICATION_JSON)); }