-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[283] Add new RestClientBuilder method for adding headers.
Signed-off-by: Adam Anderson <[email protected]>
- Loading branch information
1 parent
78693d7
commit b16fe10
Showing
5 changed files
with
130 additions
and
0 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
99 changes: 99 additions & 0 deletions
99
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,99 @@ | ||
/* | ||
* 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 com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.containing; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; | ||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.fail; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.eclipse.microprofile.rest.client.tck.interfaces.SimpleGetApi; | ||
import org.eclipse.microprofile.rest.client.tck.providers.ReturnWithAllClientHeadersFilter; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.testng.annotations.BeforeTest; | ||
import org.testng.annotations.Test; | ||
|
||
import com.github.tomakehurst.wiremock.client.MappingBuilder; | ||
|
||
public class ClientBuilderHeaderTest extends WiremockArquillianTest { | ||
@Deployment | ||
public static Archive<?> createDeployment() { | ||
return ShrinkWrap.create(WebArchive.class, ClientBuilderHeaderTest.class.getSimpleName() + ".war") | ||
.addClasses( | ||
SimpleGetApi.class, | ||
ReturnWithAllClientHeadersFilter.class, | ||
WiremockArquillianTest.class); | ||
} | ||
|
||
private static void stub(String expectedHeaderName, String... expectedHeaderValue) { | ||
String expectedIncomingHeader = Arrays.stream(expectedHeaderValue) | ||
.collect(Collectors.joining(",")); | ||
String outputBody = expectedIncomingHeader.replace(',', '-'); | ||
MappingBuilder mappingBuilder = get(urlEqualTo("/")); | ||
|
||
// headers can be sent either in a single line with comma-separated values or in multiple lines | ||
// this should match both cases: | ||
Arrays.stream(expectedHeaderValue) | ||
.forEach(val -> mappingBuilder.withHeader(expectedHeaderName, containing(val))); | ||
stubFor( | ||
mappingBuilder | ||
.willReturn( | ||
aResponse().withStatus(200) | ||
.withBody(outputBody))); | ||
} | ||
@BeforeTest | ||
public void resetWiremock() { | ||
setupServer(); | ||
} | ||
|
||
@Test | ||
public void testHeaderBuilderMethod() { | ||
stub("BuilderHeader", "BuilderHeaderValue"); | ||
|
||
RestClientBuilder builder = RestClientBuilder.newBuilder().baseUri(getServerURI()); | ||
builder.header("BuilderHeader", "BuilderHeaderValue"); | ||
SimpleGetApi client = builder.build(SimpleGetApi.class); | ||
|
||
assertEquals(client.executeGet(), | ||
"BuilderHeaderValue"); | ||
} | ||
|
||
@Test | ||
public void testHeaderBuilderMethodNullValue() { | ||
stub("BuilderHeader", "BuilderHeaderValue"); | ||
|
||
RestClientBuilder builder = RestClientBuilder.newBuilder().baseUri(getServerURI()); | ||
try { | ||
builder.header("BuilderHeader", null); | ||
} catch (NullPointerException npe) { | ||
return; | ||
} | ||
fail("header(\"builderHeader\", null) should have thrown a NullPointerException"); | ||
} | ||
} |
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