From 0f06c44d303ef38b86fd270909b8d05523f72941 Mon Sep 17 00:00:00 2001 From: koin612 Date: Fri, 11 Aug 2023 13:47:26 +0200 Subject: [PATCH] feat: case-insensitive headerfilterinterceptor --- .../predic8/membrane/core/http/Header.java | 5 ++++ .../interceptor/HeaderFilterInterceptor.java | 2 +- .../membrane/core/http/HeaderTest.java | 10 ++++++++ .../HeaderFilterInterceptorTest.java | 23 +++++++++++++++---- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/predic8/membrane/core/http/Header.java b/core/src/main/java/com/predic8/membrane/core/http/Header.java index 3582414bc..cb8a432f3 100644 --- a/core/src/main/java/com/predic8/membrane/core/http/Header.java +++ b/core/src/main/java/com/predic8/membrane/core/http/Header.java @@ -191,6 +191,11 @@ public HeaderField[] getAllHeaderFields() { return fields.toArray(new HeaderField[0]); } + public boolean contains(String header) { + return fields.stream() + .anyMatch(headerField -> headerField.getHeaderName().equals(header)); + } + /** * Since {@link HttpUtil#readLine(InputStream)} assembles the String byte-by-byte * converting it to char-by-char, we use ISO-8859-1 for output here. diff --git a/core/src/main/java/com/predic8/membrane/core/interceptor/HeaderFilterInterceptor.java b/core/src/main/java/com/predic8/membrane/core/interceptor/HeaderFilterInterceptor.java index e9e3a6062..51d2808e6 100644 --- a/core/src/main/java/com/predic8/membrane/core/interceptor/HeaderFilterInterceptor.java +++ b/core/src/main/java/com/predic8/membrane/core/interceptor/HeaderFilterInterceptor.java @@ -69,7 +69,7 @@ public String getPattern() { @MCTextContent public void setPattern(String pattern) { this.pattern = pattern; - p = Pattern.compile(pattern); + p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE); } public boolean matches(String header) { diff --git a/core/src/test/java/com/predic8/membrane/core/http/HeaderTest.java b/core/src/test/java/com/predic8/membrane/core/http/HeaderTest.java index e27905a40..06948a77c 100644 --- a/core/src/test/java/com/predic8/membrane/core/http/HeaderTest.java +++ b/core/src/test/java/com/predic8/membrane/core/http/HeaderTest.java @@ -65,6 +65,16 @@ public void testGetMimeType() throws Exception { assertTrue(new MimeType(header.getContentType()).match(TEXT_XML)); } + @Test + @DisplayName("Find containing headers") + void headerContains() { + var header = new Header(); + header.setValue("X-Foo", "123"); + + assertTrue(header.contains("x-foo")); + assertFalse(header.contains("x-bar")); + } + @Test public void testGetCharsetNull() { Header header = new Header(); diff --git a/core/src/test/java/com/predic8/membrane/core/interceptor/HeaderFilterInterceptorTest.java b/core/src/test/java/com/predic8/membrane/core/interceptor/HeaderFilterInterceptorTest.java index 3a252f19b..ea9c9158f 100644 --- a/core/src/test/java/com/predic8/membrane/core/interceptor/HeaderFilterInterceptorTest.java +++ b/core/src/test/java/com/predic8/membrane/core/interceptor/HeaderFilterInterceptorTest.java @@ -15,6 +15,7 @@ import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import com.google.common.collect.Lists; @@ -24,6 +25,8 @@ import com.predic8.membrane.core.interceptor.HeaderFilterInterceptor.Action; import com.predic8.membrane.core.interceptor.HeaderFilterInterceptor.Rule; +import java.util.List; + public class HeaderFilterInterceptorTest { @Test @@ -38,10 +41,22 @@ public void doit() throws Exception { fhi.handleResponse(exc); HeaderField[] h = exc.getResponse().getHeader().getAllHeaderFields(); - assertEquals(3, h.length); - assertEquals("Content-Length", h[0].getHeaderName().toString()); - assertEquals("a", h[1].getHeaderName().toString()); - assertEquals("e", h[2].getHeaderName().toString()); + assertEquals(2, h.length); + assertEquals("a", h[0].getHeaderName().toString()); + assertEquals("e", h[1].getHeaderName().toString()); } + @Test + @DisplayName("Remove header from Response") + void remove() throws Exception { + var exc = new Exchange(null); + exc.setResponse(Response.ok().header("Strict-Transport-Security", "foo").build()); + + var filter = new HeaderFilterInterceptor(); + filter.setRules(List.of(new Rule("strict-transport-security", Action.REMOVE))); + + filter.handleResponse(exc); + + assertFalse(exc.getResponse().getHeader().contains("STRICT-TRANSPORT-SECURITY")); + } }