Search in sources :

Example 1 with RequestPattern

use of com.github.tomakehurst.wiremock.matching.RequestPattern in project spring-cloud-contract by spring-cloud.

the class WireMockRequestStubStrategy method appendQueryParameters.

private void appendQueryParameters(RequestPatternBuilder requestPattern) {
    QueryParameters queryParameters = Optional.ofNullable(request).map(Request::getUrlPath).map(Url::getQueryParameters).orElseGet(() -> Optional.ofNullable(request).map(Request::getUrl).map(Url::getQueryParameters).orElse(null));
    Optional.ofNullable(queryParameters).map(QueryParameters::getParameters).ifPresent(parameters -> parameters.forEach(parameter -> requestPattern.withQueryParam(parameter.getName(), (StringValuePattern) convertToValuePattern(parameter.getClientValue()))));
}
Also used : Arrays(java.util.Arrays) JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) XmlToXPathsConverter.retrieveValue(org.springframework.cloud.contract.verifier.util.xml.XmlToXPathsConverter.retrieveValue) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) YamlContract(org.springframework.cloud.contract.verifier.converter.YamlContract) SingleContractMetadata(org.springframework.cloud.contract.verifier.file.SingleContractMetadata) MatchingType(org.springframework.cloud.contract.spec.internal.MatchingType) RequestPattern(com.github.tomakehurst.wiremock.matching.RequestPattern) FromFileProperty(org.springframework.cloud.contract.spec.internal.FromFileProperty) BodyMatcher(org.springframework.cloud.contract.spec.internal.BodyMatcher) RegexpBuilders.buildGStringRegexpForStubSide(org.springframework.cloud.contract.verifier.util.RegexpBuilders.buildGStringRegexpForStubSide) QueryParameters(org.springframework.cloud.contract.spec.internal.QueryParameters) YamlContractConverter(org.springframework.cloud.contract.verifier.converter.YamlContractConverter) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) BodyMatchers(org.springframework.cloud.contract.spec.internal.BodyMatchers) FORM(org.springframework.cloud.contract.verifier.util.ContentType.FORM) MapConverter(org.springframework.cloud.contract.verifier.util.MapConverter) DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) ContentUtils(org.springframework.cloud.contract.verifier.util.ContentUtils) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) RegexpBuilders.buildJSONRegexpMatch(org.springframework.cloud.contract.verifier.util.RegexpBuilders.buildJSONRegexpMatch) RequestPatternBuilder(com.github.tomakehurst.wiremock.matching.RequestPatternBuilder) GString(groovy.lang.GString) ClientDslProperty(org.springframework.cloud.contract.spec.internal.ClientDslProperty) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) List(java.util.List) StringValuePattern(com.github.tomakehurst.wiremock.matching.StringValuePattern) PathBodyMatcher(org.springframework.cloud.contract.spec.internal.PathBodyMatcher) OptionalProperty(org.springframework.cloud.contract.spec.internal.OptionalProperty) MatchingStrategy(org.springframework.cloud.contract.spec.internal.MatchingStrategy) CollectionUtils(org.springframework.util.CollectionUtils) Optional(java.util.Optional) ContentPattern(com.github.tomakehurst.wiremock.matching.ContentPattern) Pattern(java.util.regex.Pattern) EQUALITY(org.springframework.cloud.contract.spec.internal.MatchingType.EQUALITY) ContentUtils.getEqualsTypeFromContentType(org.springframework.cloud.contract.verifier.util.ContentUtils.getEqualsTypeFromContentType) Request(org.springframework.cloud.contract.spec.internal.Request) Function(java.util.function.Function) WireMock(com.github.tomakehurst.wiremock.client.WireMock) HashSet(java.util.HashSet) NamedProperty(org.springframework.cloud.contract.spec.internal.NamedProperty) UrlPattern(com.github.tomakehurst.wiremock.matching.UrlPattern) Body(org.springframework.cloud.contract.spec.internal.Body) Url(org.springframework.cloud.contract.spec.internal.Url) XmlToXPathsConverter(org.springframework.cloud.contract.verifier.util.xml.XmlToXPathsConverter) Parameters(com.github.tomakehurst.wiremock.extension.Parameters) Iterator(java.util.Iterator) JSON(org.springframework.cloud.contract.verifier.util.ContentType.JSON) MethodBufferingJsonVerifiable(org.springframework.cloud.contract.verifier.util.MethodBufferingJsonVerifiable) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) ContractVerifierMetadata(org.springframework.cloud.contract.verifier.dsl.ContractVerifierMetadata) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) RequestMethod(com.github.tomakehurst.wiremock.http.RequestMethod) RegexPatterns(org.springframework.cloud.contract.spec.internal.RegexPatterns) Collections(java.util.Collections) BINARY_EQUAL_TO(org.springframework.cloud.contract.spec.internal.MatchingStrategy.Type.BINARY_EQUAL_TO) StringUtils(org.springframework.util.StringUtils) Request(org.springframework.cloud.contract.spec.internal.Request) QueryParameters(org.springframework.cloud.contract.spec.internal.QueryParameters) Url(org.springframework.cloud.contract.spec.internal.Url)

Example 2 with RequestPattern

use of com.github.tomakehurst.wiremock.matching.RequestPattern in project furms by unity-idm.

the class UnityClientTest method shouldSend_DELETE_toUnityServer.

@Test
void shouldSend_DELETE_toUnityServer() {
    // given
    RequestPattern request = new RequestPatternBuilder(DELETE, new UrlPattern(new EqualToPattern("/path/to/unity/test?recursive=true"), false)).build();
    server.addStubMapping(new StubMapping(request, ResponseDefinitionBuilder.responseDefinition().withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE).withStatus(SC_OK).build()));
    // when
    unityClient.delete("/path/to/unity/test", Map.of("recursive", TRUE.toString()));
    // then
    VerificationResult verificationResult = server.countRequestsMatching(request);
    assertThat(verificationResult.getCount()).isEqualTo(1);
}
Also used : VerificationResult(com.github.tomakehurst.wiremock.verification.VerificationResult) EqualToPattern(com.github.tomakehurst.wiremock.matching.EqualToPattern) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping) RequestPattern(com.github.tomakehurst.wiremock.matching.RequestPattern) RequestPatternBuilder(com.github.tomakehurst.wiremock.matching.RequestPatternBuilder) UrlPattern(com.github.tomakehurst.wiremock.matching.UrlPattern) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with RequestPattern

use of com.github.tomakehurst.wiremock.matching.RequestPattern in project furms by unity-idm.

the class UnityClientTest method shouldSend_GET_toUnityServer.

@Test
void shouldSend_GET_toUnityServer() {
    // given
    RequestPattern request = new RequestPatternBuilder(GET, new UrlPattern(new EqualToPattern("/path/to/unity/test?param1=stringValue&param2=1"), false)).build();
    server.addStubMapping(new StubMapping(request, ResponseDefinitionBuilder.responseDefinition().withBody("{\"field\": \"string_value\"}").withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE).withStatus(SC_OK).build()));
    // when
    SampleDto response = unityClient.get("/path/to/unity/test", SampleDto.class, Map.of("param1", "stringValue", "param2", "1"));
    // then
    VerificationResult verificationResult = server.countRequestsMatching(request);
    assertThat(verificationResult.getCount()).isEqualTo(1);
    assertThat(response.getField()).isEqualTo("string_value");
}
Also used : VerificationResult(com.github.tomakehurst.wiremock.verification.VerificationResult) EqualToPattern(com.github.tomakehurst.wiremock.matching.EqualToPattern) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping) RequestPattern(com.github.tomakehurst.wiremock.matching.RequestPattern) RequestPatternBuilder(com.github.tomakehurst.wiremock.matching.RequestPatternBuilder) UrlPattern(com.github.tomakehurst.wiremock.matching.UrlPattern) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with RequestPattern

use of com.github.tomakehurst.wiremock.matching.RequestPattern in project atlasdb by palantir.

the class AtlasDbHttpClientsTest method ifOneServerResponds503WithNoRetryHeaderTheRequestIsRerouted.

@Test
public void ifOneServerResponds503WithNoRetryHeaderTheRequestIsRerouted() {
    RETRY_OTHER_FIRST_RESPONSE_TRANSFORMER.registerUrl(getUriForPort(availablePort1));
    RETRY_OTHER_FIRST_RESPONSE_TRANSFORMER.registerUrl(getUriForPort(availablePort2));
    TestResource client = AtlasDbHttpClients.createProxyWithFailover(MetricsManagers.createForTests(), ImmutableServerListConfig.builder().addServers(getUriForPort(availablePort1)).addServers(getUriForPort(availablePort2)).sslConfiguration(SSL_CONFIG).build(), TestResource.class, AUXILIARY_REMOTING_PARAMETERS);
    int response = client.getTestNumber();
    RequestPattern request = getRequestedFor(urlMatching(GET_ENDPOINT)).build();
    assertThat(availableServer1.findRequestsMatching(request).getRequests()).hasSizeGreaterThanOrEqualTo(1);
    assertThat(availableServer2.findRequestsMatching(request).getRequests()).hasSizeGreaterThanOrEqualTo(1);
    assertThat(response).isIn(TEST_NUMBER_1, TEST_NUMBER_2);
    RETRY_OTHER_FIRST_RESPONSE_TRANSFORMER.reset();
}
Also used : RequestPattern(com.github.tomakehurst.wiremock.matching.RequestPattern) Test(org.junit.Test)

Example 5 with RequestPattern

use of com.github.tomakehurst.wiremock.matching.RequestPattern in project wiremock by wiremock.

the class FindRequestsTask method execute.

@Override
public ResponseDefinition execute(Admin admin, Request request, PathParams pathParams) {
    RequestPattern requestPattern = Json.read(request.getBodyAsString(), RequestPattern.class);
    FindRequestsResult result = admin.findRequestsMatching(requestPattern);
    return responseDefinition().withStatus(HTTP_OK).withBody(Json.write(result)).withHeader("Content-Type", "application/json").build();
}
Also used : FindRequestsResult(com.github.tomakehurst.wiremock.verification.FindRequestsResult) RequestPattern(com.github.tomakehurst.wiremock.matching.RequestPattern)

Aggregations

RequestPattern (com.github.tomakehurst.wiremock.matching.RequestPattern)20 StubMapping (com.github.tomakehurst.wiremock.stubbing.StubMapping)8 RequestPatternBuilder (com.github.tomakehurst.wiremock.matching.RequestPatternBuilder)6 UrlPattern (com.github.tomakehurst.wiremock.matching.UrlPattern)6 VerificationResult (com.github.tomakehurst.wiremock.verification.VerificationResult)6 ResponseDefinition (com.github.tomakehurst.wiremock.http.ResponseDefinition)5 Test (org.junit.jupiter.api.Test)5 EqualToPattern (com.github.tomakehurst.wiremock.matching.EqualToPattern)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 RequestPatternBuilder.newRequestPattern (com.github.tomakehurst.wiremock.matching.RequestPatternBuilder.newRequestPattern)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 WireMock (com.github.tomakehurst.wiremock.client.WireMock)2 Parameters (com.github.tomakehurst.wiremock.extension.Parameters)2 RequestMethod (com.github.tomakehurst.wiremock.http.RequestMethod)2 ContentPattern (com.github.tomakehurst.wiremock.matching.ContentPattern)2 StringValuePattern (com.github.tomakehurst.wiremock.matching.StringValuePattern)2 GString (groovy.lang.GString)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2