Skip to content

Commit

Permalink
feat: case-insensitive headerfilterinterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
koin612 committed Aug 11, 2023
1 parent 5f7c443 commit 0f06c44
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/com/predic8/membrane/core/http/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions core/src/test/java/com/predic8/membrane/core/http/HeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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"));
}
}

0 comments on commit 0f06c44

Please sign in to comment.