Handle any method as mock request method

This commit is contained in:
Piotr Fus 2023-05-28 18:46:16 +02:00
parent 8ba339b8a0
commit 374947847d
4 changed files with 29 additions and 2 deletions

View file

@ -166,7 +166,7 @@ Send POST request to localhost:<PORT>/serverControl
- response - groovy closure as string which must evaluate to string which will be response of mock when predicate is satisfied, optional, default { _ -> '' }
- soap - true or false, is request and response should be wrapped in soap Envelope and Body elements, default false
- statusCode - integer, status code of response when predicate is satisfied, default 200
- method - POST|PUT|DELETE|GET|TRACE|OPTION|HEAD, expected http method of request, default POST
- method - POST|PUT|DELETE|GET|TRACE|OPTION|HEAD|ANY_METHOD, expected http method of request, default `POST`, `ANY_METHOD` matches all HTTP methods
- responseHeaders - groovyClosure as string which must evaluate to Map which will be added to response headers, default { _ -> \[:] }
- schema - path to xsd schema file on mockserver classpath; default empty, so no vallidation of request is performed; if validation fails then response has got status 400 and response is raw message from validator
- imports - list of imports for closures (each import is separate tag); `alias` is the name of `fullClassName` available in closure; `fullClassName` must be available on classpath of mock server

View file

@ -11,6 +11,7 @@
<xs:enumeration value="HEAD"/>
<xs:enumeration value="OPTIONS"/>
<xs:enumeration value="PATCH"/>
<xs:enumeration value="ANY_METHOD" />
</xs:restriction>
</xs:simpleType>

View file

@ -1152,4 +1152,30 @@ class MockServerIntegrationTest extends Specification {
'test/other' | 'test/other'
'/test/other' | 'test/other'
}
def 'should match any method'() {
given:
String name = "testRest-${UUID.randomUUID().toString()}"
remoteMockServer.addMock(new AddMock(
name: name,
path: 'any-method',
port: 9999,
statusCode: 201,
soap: false,
method: Method.ANY_METHOD
))
when:
CloseableHttpResponse response = client.execute(req)
then:
response.statusLine.statusCode == 201
Util.consumeResponse(response)
cleanup:
remoteMockServer.removeMock(name)
where:
req << [
new HttpGet('http://localhost:9999/any-method'),
new HttpPost('http://localhost:9999/any-method'),
new HttpPatch('http://localhost:9999/any-method')
]
}
}

View file

@ -60,7 +60,7 @@ class Mock implements Comparable<Mock> {
boolean match(Method method, MockRequest request) {
boolean usesCondition = hasLimitedUses() ? usesLeft > 0 : true
return usesCondition && this.method == method && predicate(request)
return usesCondition && (this.method == method || this.method == Method.ANY_METHOD) && predicate(request)
}
MockResponse apply(MockRequest request) {