Created
August 2, 2017 21:47
-
-
Save bcalmac/a85c1465c41cfbe4a2fa7de1a81c557e to your computer and use it in GitHub Desktop.
Simple WireMock transformer that converts the content to upper case
This file contains hidden or 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
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | |
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; | |
import static com.github.tomakehurst.wiremock.client.WireMock.get; | |
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | |
import static org.junit.Assert.assertEquals; | |
import wiremock.org.apache.http.client.utils.URIBuilder; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; | |
import com.github.tomakehurst.wiremock.common.FileSource; | |
import com.github.tomakehurst.wiremock.core.WireMockConfiguration; | |
import com.github.tomakehurst.wiremock.extension.Parameters; | |
import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformer; | |
import com.github.tomakehurst.wiremock.http.Request; | |
import com.github.tomakehurst.wiremock.http.ResponseDefinition; | |
import com.github.tomakehurst.wiremock.junit.WireMockRule; | |
import com.sun.jersey.api.client.Client; | |
@SuppressWarnings("WeakerAccess") | |
public class WireMockTransformerExample { | |
static int PORT = 8888; | |
static String UPPER = "upper"; | |
@Rule | |
public WireMockRule wireMockRule = new WireMockRule( | |
new WireMockConfiguration().port(PORT).extensions(new ReversingTransformer())); | |
Client client; | |
ResponseDefinitionTransformer reversingTransformer; | |
@Before | |
public void setUp() { | |
client = new Client(); | |
reversingTransformer = new ReversingTransformer(); | |
} | |
@After | |
public void tearDown() { | |
client.destroy(); | |
} | |
@Test | |
public void exampleTest() throws URISyntaxException { | |
String message = "Hello, world!"; | |
stubFor(get(anyUrl()).willReturn(aResponse().withBody(message).withTransformers(UPPER))); | |
assertEquals(message.toUpperCase(), client.resource(wiremockUri()).get(String.class)); | |
} | |
private URI wiremockUri() throws URISyntaxException { | |
return new URIBuilder().setScheme("http").setHost("localhost").setPort(PORT).build(); | |
} | |
private static class ReversingTransformer extends ResponseDefinitionTransformer { | |
@Override | |
public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource files, | |
Parameters parameters) { | |
return ResponseDefinitionBuilder.like(responseDefinition) | |
.withBody(responseDefinition.getBody().toUpperCase()) | |
.build(); | |
} | |
@Override | |
public String getName() { | |
return UPPER; | |
} | |
@Override | |
public boolean applyGlobally() { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment