-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove headers that do not affect CORS decision-making from request a…
…dapter logging output (#9178) Signed-off-by: Tim Quinn <[email protected]>
- Loading branch information
Showing
6 changed files
with
185 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
microprofile/cors/src/test/java/io/helidon/microprofile/cors/HeadersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.microprofile.cors; | ||
|
||
import java.net.URI; | ||
import java.util.Map; | ||
|
||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.core.MultivaluedHashMap; | ||
import jakarta.ws.rs.core.UriInfo; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.allOf; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class HeadersTest { | ||
|
||
@Test | ||
void checkHeaders() { | ||
|
||
UriInfo uriInfo = mock(UriInfo.class); | ||
when(uriInfo.getRequestUri()).thenReturn(URI.create("http://myhost.com/testpath")); | ||
ContainerRequestContext context = mock(ContainerRequestContext.class); | ||
when(context.getHeaders()).thenReturn(new MultivaluedHashMap<>(Map.of("Origin", "http://myhost.com", | ||
"Host", "otherhost", | ||
"Authorization", "some-auth", | ||
"X-Custom", "myValue"))); | ||
when(context.getMethod()).thenReturn("POST"); | ||
when(context.getUriInfo()).thenReturn(uriInfo); | ||
|
||
CorsSupportMp.RequestAdapterMp requestAdapterMp = new CorsSupportMp.RequestAdapterMp(context); | ||
|
||
assertThat("Headers", | ||
requestAdapterMp.toString(), | ||
allOf( | ||
containsString("path=/testpath"), | ||
containsString("method=POST"), | ||
containsString("Origin=[http://myhost.com]"), | ||
containsString("Host=[otherhost]"), | ||
not(containsString("Authorization")), | ||
not(containsString("X-Custom")))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
webserver/cors/src/test/java/io/helidon/webserver/cors/HeadersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.webserver.cors; | ||
|
||
import io.helidon.http.HeaderNames; | ||
import io.helidon.http.HttpPrologue; | ||
import io.helidon.http.Method; | ||
import io.helidon.http.RoutedPath; | ||
import io.helidon.http.ServerRequestHeaders; | ||
import io.helidon.http.WritableHeaders; | ||
import io.helidon.webserver.http.ServerRequest; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.allOf; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class HeadersTest { | ||
|
||
@Test | ||
void checkHeaders() { | ||
RoutedPath path = Mockito.mock(RoutedPath.class); | ||
Mockito.when(path.path()) | ||
.thenReturn("/testpath"); | ||
|
||
ServerRequestHeaders serverRequestHeaders = ServerRequestHeaders.create( | ||
WritableHeaders.create() | ||
.add(HeaderNames.ORIGIN, "http://myhost.com") | ||
.add(HeaderNames.HOST, "otherhost") | ||
.add(HeaderNames.AUTHORIZATION, "some-auth") | ||
.add(HeaderNames.create("X-Custom"), "some-auth")); | ||
|
||
HttpPrologue httpPrologue = mock(HttpPrologue.class); | ||
when(httpPrologue.method()) | ||
.thenReturn(Method.POST); | ||
ServerRequest serverRequest = Mockito.mock(ServerRequest.class); | ||
|
||
Mockito.when(serverRequest.path()) | ||
.thenReturn(path); | ||
Mockito.when(serverRequest.prologue()) | ||
.thenReturn(httpPrologue); | ||
Mockito.when(serverRequest.headers()) | ||
.thenReturn(serverRequestHeaders); | ||
|
||
CorsServerRequestAdapter requestAdapterSe = new CorsServerRequestAdapter(serverRequest, null); | ||
assertThat("Headers", | ||
requestAdapterSe.toString(), | ||
allOf( | ||
containsString("path=/testpath"), | ||
containsString("method=POST"), | ||
containsString("Origin: http://myhost.com"), | ||
containsString("Host: otherhost"), | ||
not(containsString("Authorization")), | ||
not(containsString("X-Custom")))); | ||
} | ||
} |