Skip to content

Commit

Permalink
[Knotx#41]KnotxServer response configuration - wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
dragarthPl committed Feb 19, 2020
1 parent 567d223 commit 4c370e1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ All notable changes to `knotx-server-http` will be documented in this file.

## Unreleased
List of changes that are finished but not yet released in any final version.
- [PR-46](https://github.com/Knotx/knotx-server-http/pull/46) - KnotxServer response configuration - wildcards [41](https://github.com/Knotx/knotx-server-http/issues/41)

## 2.1.0
- [PR-35](https://github.com/Knotx/knotx-server-http/pull/35) - Vert.x upgrade to 3.8.1. Cookie Handler was deprecated: cookies are enabled by default and `cookieHandler` handler is not required anymore.
Expand Down
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
implementation(platform("io.knotx:knotx-dependencies:${project.version}"))

api(project(":knotx-server-http-api"))
implementation("io.knotx:knotx-commons:${project.version}")
implementation(group = "io.vertx", name = "vertx-core")
implementation(group = "io.vertx", name = "vertx-service-proxy")
implementation(group = "io.vertx", name = "vertx-rx-java2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Optional;
import java.util.Set;

import io.knotx.commons.http.request.AllowedHeadersFilter;
import io.knotx.server.api.context.ClientResponse;
import io.knotx.server.api.context.RequestContext;
import io.netty.handler.codec.http.HttpResponseStatus;
Expand Down Expand Up @@ -81,7 +82,7 @@ private MultiMap getHeaders(Set<String> allowedResponseHeaders) {
clientResponse.getHeaders()
.names()
.stream()
.filter(header -> headerFilter(allowedResponseHeaders, header))
.filter(AllowedHeadersFilter.WildcardCaseInsensitive.create(allowedResponseHeaders))
.forEach(
name ->
clientResponse.getHeaders()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
class ServerResponseTest {

private static final Set<String> DEFAULT_ALLOWED_HEADERS = Collections.singleton("test");
private static final Set<String> WILDCARD_ALLOWED_HEADERS = Collections.singleton("*");
private static final Set<String> WILDCARDED_ALLOWED_HEADERS = Collections.singleton("te*");

@Mock
private HttpServerResponse httpResponse;
Expand Down Expand Up @@ -175,4 +177,48 @@ void checkHeadersFiltered() {
assertEquals(1, headersMultiMap.size());
assertEquals(headersMultiMap.get("test"), "testValue");
}

@Test
@DisplayName("Expect headers filtered by wildcarded allowed headers out context ok")
void checkWildcardedAllowedHeaders() {
//given
when(status.isFailed()).thenReturn(false);
when(httpResponse.headers()).thenReturn(headersMultiMap);
when(clientResponse.getStatusCode()).thenReturn(HttpResponseStatus.OK.code());
final MultiMap headers = MultiMap.caseInsensitiveMultiMap()
.add("test", "testValue")
.add("notAllowed", "value")
.add("shouldBeFilteredOut", "value");
when(clientResponse.getHeaders()).thenReturn(headers);

//when
new ServerResponse(requestContext).end(httpResponse, WILDCARDED_ALLOWED_HEADERS);

//then
assertEquals(1, headersMultiMap.size());
assertEquals(headersMultiMap.get("test"), "testValue");
}

@Test
@DisplayName("Expect headers filtered by wildcard sign out context ok")
void checkWildcardSignHeadersFiltered() {
//given
when(status.isFailed()).thenReturn(false);
when(httpResponse.headers()).thenReturn(headersMultiMap);
when(clientResponse.getStatusCode()).thenReturn(HttpResponseStatus.OK.code());
final MultiMap headers = MultiMap.caseInsensitiveMultiMap()
.add("test", "testValue")
.add("notAllowed", "value")
.add("shouldBeFilteredOut", "value");
when(clientResponse.getHeaders()).thenReturn(headers);

//when
new ServerResponse(requestContext).end(httpResponse, WILDCARD_ALLOWED_HEADERS);

//then
assertEquals(3, headersMultiMap.size());
assertEquals(headersMultiMap.get("test"), "testValue");
}


}

0 comments on commit 4c370e1

Please sign in to comment.