-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[283] Add new RestClientBuilder method for adding headers. #378
Merged
Emily-Jiang
merged 3 commits into
microprofile:main
from
WhiteCat22:header_builder_method
Jun 17, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
110 changes: 110 additions & 0 deletions
110
tck/src/main/java/org/eclipse/microprofile/rest/client/tck/ClientBuilderHeaderTest.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,110 @@ | ||
/* | ||
* Copyright 2024 Contributors to the Eclipse Foundation | ||
* | ||
* 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 org.eclipse.microprofile.rest.client.tck; | ||
|
||
import static org.testng.Assert.assertTrue; | ||
import static org.testng.Assert.fail; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.eclipse.microprofile.rest.client.tck.interfaces.ClientBuilderHeaderClient; | ||
import org.eclipse.microprofile.rest.client.tck.interfaces.ClientBuilderHeaderMethodClient; | ||
import org.eclipse.microprofile.rest.client.tck.providers.ReturnWithAllDuplicateClientHeadersFilter; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.testng.Arquillian; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import jakarta.json.JsonArray; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonString; | ||
import jakarta.json.JsonValue; | ||
|
||
public class ClientBuilderHeaderTest extends Arquillian { | ||
@Deployment | ||
public static Archive<?> createDeployment() { | ||
return ShrinkWrap.create(WebArchive.class, ClientBuilderHeaderTest.class.getSimpleName() + ".war") | ||
.addClasses( | ||
ClientBuilderHeaderMethodClient.class, | ||
ReturnWithAllDuplicateClientHeadersFilter.class, | ||
WiremockArquillianTest.class); | ||
} | ||
|
||
@Test | ||
public void testHeaderBuilderMethod() { | ||
|
||
RestClientBuilder builder = RestClientBuilder.newBuilder().baseUri("http://localhost:8080/"); | ||
builder.register(ReturnWithAllDuplicateClientHeadersFilter.class); | ||
builder.header("InterfaceAndBuilderHeader", "builder"); | ||
ClientBuilderHeaderMethodClient client = builder.build(ClientBuilderHeaderMethodClient.class); | ||
|
||
checkHeaders(client.getAllHeaders("headerparam"), "method"); | ||
} | ||
|
||
@Test | ||
public void testHeaderBuilderInterface() { | ||
|
||
RestClientBuilder builder = RestClientBuilder.newBuilder().baseUri("http://localhost:8080/"); | ||
builder.register(ReturnWithAllDuplicateClientHeadersFilter.class); | ||
builder.header("InterfaceAndBuilderHeader", "builder"); | ||
ClientBuilderHeaderClient client = builder.build(ClientBuilderHeaderClient.class); | ||
|
||
checkHeaders(client.getAllHeaders("headerparam"), "interface"); | ||
} | ||
|
||
@Test | ||
public void testHeaderBuilderMethodNullValue() { | ||
|
||
RestClientBuilder builder = RestClientBuilder.newBuilder().baseUri("http://localhost:8080/"); | ||
try { | ||
builder.header("BuilderHeader", null); | ||
} catch (NullPointerException npe) { | ||
return; | ||
} | ||
fail("header(\"builderHeader\", null) should have thrown a NullPointerException"); | ||
} | ||
|
||
private static void checkHeaders(final JsonObject headers, final String clientHeaderParamName) { | ||
final List<String> clientRequestHeaders = headerValues(headers, "InterfaceAndBuilderHeader"); | ||
|
||
assertTrue(clientRequestHeaders.contains("builder"), | ||
"Header InterfaceAndBuilderHeader did not container \"builder\": " + clientRequestHeaders); | ||
assertTrue(clientRequestHeaders.contains(clientHeaderParamName), | ||
"Header InterfaceAndBuilderHeader did not container \"" + clientHeaderParamName + "\": " | ||
+ clientRequestHeaders); | ||
|
||
final List<String> headerParamHeaders = headerValues(headers, "HeaderParam"); | ||
assertTrue(headerParamHeaders.contains("headerparam"), | ||
"Header HeaderParam did not container \"headerparam\": " + headerParamHeaders); | ||
} | ||
|
||
private static List<String> headerValues(final JsonObject headers, final String headerName) { | ||
final JsonArray headerValues = headers.getJsonArray(headerName); | ||
Assert.assertNotNull(headerValues, | ||
String.format("Expected header '%s' to be present in %s", headerName, headers)); | ||
return headerValues.stream().map( | ||
v -> (v.getValueType() == JsonValue.ValueType.STRING ? ((JsonString) v).getString() : v.toString())) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...n/java/org/eclipse/microprofile/rest/client/tck/interfaces/ClientBuilderHeaderClient.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,34 @@ | ||
/* | ||
* Copyright 2024 Contributors to the Eclipse Foundation | ||
* | ||
* 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 org.eclipse.microprofile.rest.client.tck.interfaces; | ||
|
||
import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam; | ||
|
||
import jakarta.json.JsonObject; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.HeaderParam; | ||
import jakarta.ws.rs.Path; | ||
|
||
@ClientHeaderParam(name = "InterfaceAndBuilderHeader", value = "interface") | ||
@Path("/") | ||
public interface ClientBuilderHeaderClient { | ||
|
||
@GET | ||
JsonObject getAllHeaders(@HeaderParam("HeaderParam") String param); | ||
} |
34 changes: 34 additions & 0 deletions
34
.../org/eclipse/microprofile/rest/client/tck/interfaces/ClientBuilderHeaderMethodClient.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,34 @@ | ||
/* | ||
* Copyright 2024 Contributors to the Eclipse Foundation | ||
* | ||
* 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 org.eclipse.microprofile.rest.client.tck.interfaces; | ||
|
||
import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam; | ||
|
||
import jakarta.json.JsonObject; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.HeaderParam; | ||
import jakarta.ws.rs.Path; | ||
|
||
@Path("/") | ||
public interface ClientBuilderHeaderMethodClient { | ||
|
||
@GET | ||
@ClientHeaderParam(name = "InterfaceAndBuilderHeader", value = "method") | ||
JsonObject getAllHeaders(@HeaderParam("HeaderParam") String param); | ||
} |
47 changes: 47 additions & 0 deletions
47
...pse/microprofile/rest/client/tck/providers/ReturnWithAllDuplicateClientHeadersFilter.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,47 @@ | ||
/* | ||
* Copyright 2018, 2021 Contributors to the Eclipse Foundation | ||
* | ||
* 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 org.eclipse.microprofile.rest.client.tck.providers; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import jakarta.json.Json; | ||
import jakarta.json.JsonArrayBuilder; | ||
import jakarta.json.JsonObjectBuilder; | ||
import jakarta.ws.rs.client.ClientRequestContext; | ||
import jakarta.ws.rs.client.ClientRequestFilter; | ||
import jakarta.ws.rs.core.MultivaluedMap; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
public class ReturnWithAllDuplicateClientHeadersFilter implements ClientRequestFilter { | ||
|
||
@Override | ||
public void filter(ClientRequestContext clientRequestContext) throws IOException { | ||
JsonObjectBuilder allClientHeaders = Json.createObjectBuilder(); | ||
MultivaluedMap<String, Object> clientHeaders = clientRequestContext.getHeaders(); | ||
for (String headerName : clientHeaders.keySet()) { | ||
List<Object> header = clientHeaders.get(headerName); | ||
final JsonArrayBuilder headerValues = Json.createArrayBuilder(); | ||
header.forEach(h -> headerValues.add(h.toString())); | ||
allClientHeaders.add(headerName, headerValues); | ||
} | ||
clientRequestContext.abortWith(Response.ok(allClientHeaders.build()).build()); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should block the PR based on this, but we don't actually need the
WiremockArquillianTest.class
here.