Search in sources :

Example 1 with StubMapping

use of com.github.tomakehurst.wiremock.stubbing.StubMapping in project spring-cloud-netflix by spring-cloud.

the class WireMockRestAssuredRequestAdapter method filter.

@Override
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext context) {
    Map<String, Object> configuration = getConfiguration(requestSpec, context);
    configuration.put("contract.jsonPaths", this.jsonPaths.keySet());
    Response response = context.next(requestSpec, responseSpec);
    if (requestSpec.getBody() != null && !this.jsonPaths.isEmpty()) {
        String actual = new String((byte[]) requestSpec.getBody());
        for (JsonPath jsonPath : this.jsonPaths.values()) {
            new JsonPathValue(jsonPath, actual).assertHasValue(Object.class, "an object");
        }
    }
    if (this.builder != null) {
        this.builder.willReturn(getResponseDefinition(response));
        StubMapping stubMapping = this.builder.build();
        MatchResult match = stubMapping.getRequest().match(new WireMockRestAssuredRequestAdapter(requestSpec));
        assertThat(match.isExactMatch()).as("wiremock did not match request").isTrue();
        configuration.put("contract.stubMapping", stubMapping);
    }
    return response;
}
Also used : Response(io.restassured.response.Response) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping) JsonPath(com.jayway.jsonpath.JsonPath) MatchResult(com.github.tomakehurst.wiremock.matching.MatchResult)

Example 2 with StubMapping

use of com.github.tomakehurst.wiremock.stubbing.StubMapping in project irontest by zheng-wang.

the class IronTestUtils method substituteRequestBodyMainPatternValue.

public static void substituteRequestBodyMainPatternValue(List<HTTPStubMapping> httpStubMappings) {
    for (HTTPStubMapping httpStubMapping : httpStubMappings) {
        StubMapping spec = httpStubMapping.getSpec();
        List<ContentPattern<?>> requestBodyPatterns = spec.getRequest().getBodyPatterns();
        if (requestBodyPatterns != null) {
            for (int i = 0; i < requestBodyPatterns.size(); i++) {
                ContentPattern requestBodyPattern = requestBodyPatterns.get(i);
                if (requestBodyPattern instanceof EqualToXmlPattern) {
                    EqualToXmlPattern equalToXmlPattern = (EqualToXmlPattern) requestBodyPattern;
                    requestBodyPatterns.set(i, new EqualToXmlPattern(httpStubMapping.getRequestBodyMainPatternValue(), equalToXmlPattern.isEnablePlaceholders(), equalToXmlPattern.getPlaceholderOpeningDelimiterRegex(), equalToXmlPattern.getPlaceholderClosingDelimiterRegex()));
                    break;
                } else if (requestBodyPattern instanceof EqualToJsonPattern) {
                    EqualToJsonPattern equalToJsonPattern = (EqualToJsonPattern) requestBodyPattern;
                    requestBodyPatterns.set(i, new EqualToJsonPattern(httpStubMapping.getRequestBodyMainPatternValue(), equalToJsonPattern.isIgnoreArrayOrder(), equalToJsonPattern.isIgnoreExtraElements()));
                    break;
                }
            }
        }
    }
}
Also used : StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping)

Example 3 with StubMapping

use of com.github.tomakehurst.wiremock.stubbing.StubMapping in project irontest by zheng-wang.

the class HTTPStubsSetupTeststepRunner method run.

@Override
public BasicTeststepRun run() {
    WireMockServer wireMockServer = getTestcaseRunContext().getWireMockServer();
    // reset mock server
    wireMockServer.resetAll();
    // load stub mappings into mock server
    Map<Short, UUID> httpStubMappingInstanceIds = getTestcaseRunContext().getHttpStubMappingInstanceIds();
    HTTPStubsSetupTeststepProperties otherProperties = (HTTPStubsSetupTeststepProperties) getTeststep().getOtherProperties();
    wireMockServer.loadMappingsUsing(stubMappings -> {
        for (HTTPStubMapping stubMapping : otherProperties.getHttpStubMappings()) {
            StubMapping stubInstance = IronTestUtils.createStubInstance(stubMapping.getId(), stubMapping.getNumber(), stubMapping.getSpec());
            stubMappings.addMapping(stubInstance);
            httpStubMappingInstanceIds.put(stubMapping.getNumber(), stubInstance.getId());
        }
    });
    return new BasicTeststepRun();
}
Also used : HTTPStubMapping(io.irontest.models.HTTPStubMapping) HTTPStubsSetupTeststepProperties(io.irontest.models.teststep.HTTPStubsSetupTeststepProperties) HTTPStubMapping(io.irontest.models.HTTPStubMapping) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping) UUID(java.util.UUID) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer)

Example 4 with StubMapping

use of com.github.tomakehurst.wiremock.stubbing.StubMapping in project irontest by zheng-wang.

the class HTTPStubMappingMapper method map.

@Override
public HTTPStubMapping map(ResultSet rs, StatementContext ctx) throws SQLException {
    String specJSON = rs.getString("spec_json");
    StubMapping spec = StubMapping.buildFrom(specJSON);
    HTTPStubMapping httpStubMapping = new HTTPStubMapping(rs.getLong("id"), rs.getLong("testcase_id"), rs.getShort("number"), spec, rs.getString("request_body_main_pattern_value"), rs.getShort("expected_hit_count"));
    return httpStubMapping;
}
Also used : HTTPStubMapping(io.irontest.models.HTTPStubMapping) HTTPStubMapping(io.irontest.models.HTTPStubMapping) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping)

Example 5 with StubMapping

use of com.github.tomakehurst.wiremock.stubbing.StubMapping in project georocket by georocket.

the class RemoteElasticsearchClientTest method createIndex.

/**
 * Test if the client can create an index
 * @param context the test context
 */
@Test
public void createIndex(TestContext context) {
    StubMapping settings = stubFor(put(urlEqualTo("/" + INDEX)).withRequestBody(equalTo("")).willReturn(aResponse().withBody(ACKNOWLEDGED.encode()).withStatus(200)));
    Async async = context.async();
    client.createIndex().subscribe(ok -> {
        context.assertTrue(ok);
        verify(putRequestedFor(settings.getRequest().getUrlMatcher()));
        async.complete();
    }, context::fail);
}
Also used : Async(io.vertx.ext.unit.Async) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping) Test(org.junit.Test)

Aggregations

StubMapping (com.github.tomakehurst.wiremock.stubbing.StubMapping)12 Test (org.junit.Test)4 HTTPStubMapping (io.irontest.models.HTTPStubMapping)3 ArrayList (java.util.ArrayList)3 Async (io.vertx.ext.unit.Async)2 URL (java.net.URL)2 Calendar (java.util.Calendar)2 TimeZone (java.util.TimeZone)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 Configuration (org.apache.logging.log4j.core.config.Configuration)2 ConfigurationListener (org.apache.logging.log4j.core.config.ConfigurationListener)2 ConfigurationScheduler (org.apache.logging.log4j.core.config.ConfigurationScheduler)2 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)2 HttpWatcher (org.apache.logging.log4j.core.config.HttpWatcher)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)1 WireMock.matchingJsonPath (com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath)1 MatchResult (com.github.tomakehurst.wiremock.matching.MatchResult)1 ServeEvent (com.github.tomakehurst.wiremock.stubbing.ServeEvent)1