@Test public void shouldBuildMappingWithResponseBody() { StubMapping mapping = new MappingBuilder(POST, new UrlMatchingStrategy()) .willReturn(new ResponseDefinitionBuilder().withBody("Some content")) .build(); assertThat(mapping.getResponse().getBody(), is("Some content")); }
@Test public void shouldBuildMappingWithMatchedUrlNoHeaders() { UrlMatchingStrategy urlStrategy = new UrlMatchingStrategy(); urlStrategy.setUrlPattern("/match/[A-Z]{5}"); StubMapping mapping = new MappingBuilder(POST, urlStrategy).willReturn(new ResponseDefinitionBuilder()).build(); assertThat(mapping.getRequest().getUrlPattern(), is("/match/[A-Z]{5}")); }
@Test public void shouldBuildMappingWithExactUrlNoHeaders() { UrlMatchingStrategy urlStrategy = new UrlMatchingStrategy(); urlStrategy.setUrl("/match/this"); StubMapping mapping = new MappingBuilder(POST, urlStrategy) .willReturn(new ResponseDefinitionBuilder().withStatus(201)) .build(); assertThat(mapping.getRequest().getUrl(), is("/match/this")); assertThat(mapping.getRequest().getMethod(), is(POST)); assertThat(mapping.getResponse().getStatus(), is(201)); }
public StubMapping build() { if (scenarioName == null && (requiredScenarioState != null || newScenarioState != null)) { throw new IllegalStateException( "Scenario name must be specified to require or set a new scenario state"); } RequestPattern requestPattern = requestPatternBuilder.build(); ResponseDefinition response = responseDefBuilder.build(); StubMapping mapping = new StubMapping(requestPattern, response); mapping.setPriority(priority); mapping.setScenarioName(scenarioName); mapping.setRequiredScenarioState(requiredScenarioState); mapping.setNewScenarioState(newScenarioState); return mapping; }
@SuppressWarnings("unchecked") @Test public void shouldBuildMappingWithResponseHeaders() { StubMapping mapping = new MappingBuilder(POST, new UrlMatchingStrategy()) .willReturn( new ResponseDefinitionBuilder() .withHeader("Content-Type", "text/xml") .withHeader("Encoding", "UTF-8")) .build(); assertThat( mapping.getResponse().getHeaders().all(), hasItems(header("Content-Type", "text/xml"), header("Encoding", "UTF-8"))); }
@Test public void shouldBuildMappingWithExactUrlAndRequestParameters() { UrlMatchingStrategy urlStrategy = new UrlMatchingStrategy(); urlStrategy.setUrl("/match/this"); StubMapping mapping = new MappingBuilder(POST, urlStrategy) .withParameter("p1", parameterStrategyEqualTo("val1")) .withParameter("p2", parameterStrategyMatches("val\\d")) .withParameter("p3", parameterStrategyDoesNotMatch("[A-Z]+")) .willReturn(new ResponseDefinitionBuilder()) .build(); assertThat(mapping.getRequest().getParameters(), hasEntry("p1", parameterEqualTo("val1"))); assertThat(mapping.getRequest().getParameters(), hasEntry("p2", parameterMatches("val\\d"))); assertThat( mapping.getRequest().getParameters(), hasEntry("p3", parameterDoesNotMatch("[A-Z]+"))); }
@Test public void shouldBuildMappingWithExactUrlAndRequestHeaders() { UrlMatchingStrategy urlStrategy = new UrlMatchingStrategy(); urlStrategy.setUrl("/match/this"); StubMapping mapping = new MappingBuilder(POST, urlStrategy) .withHeader("Content-Type", headerStrategyEqualTo("text/plain")) .withHeader("Encoding", headerStrategyMatches("UTF-\\d")) .withHeader("X-My-Thing", headerStrategyDoesNotMatch("[A-Z]+")) .willReturn(new ResponseDefinitionBuilder()) .build(); assertThat( mapping.getRequest().getHeaders(), hasEntry("Content-Type", headerEqualTo("text/plain"))); assertThat(mapping.getRequest().getHeaders(), hasEntry("Encoding", headerMatches("UTF-\\d"))); assertThat( mapping.getRequest().getHeaders(), hasEntry("X-My-Thing", headerDoesNotMatch("[A-Z]+"))); }
@Override public void addStubMapping(StubMapping stubMapping) { if (stubMapping.getRequest().hasCustomMatcher()) { throw new AdminException( "Custom matchers can't be used when administering a remote WireMock server. " + "Use WireMockRule.stubFor() or WireMockServer.stubFor() to administer the local instance."); } executeRequest( adminRoutes.requestSpecForTask(CreateStubMappingTask.class), PathParams.empty(), stubMapping, Void.class, 201); }