W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
HTTP協(xié)議只需要在請求中指定方法和URL。合同的請求定義中必須包含相同的信息。
Groovy DSL。
org.springframework.cloud.contract.spec.Contract.make { request { // HTTP request method (GET/POST/PUT/DELETE). method 'GET' // Path component of request URL is specified as follows. urlPath('/users') } response { //... status 200 } }
YAML。
method: PUT url: /foo
可以指定一個絕對值而不是相對值url
,但是建議使用urlPath
,因為這樣做會使測試獨立于主機。
Groovy DSL。
org.springframework.cloud.contract.spec.Contract.make { request { method 'GET' // Specifying `url` and `urlPath` in one contract is illegal. url('http://localhost:8888/users') } response { //... status 200 } }
YAML。
request: method: PUT urlPath: /foo
request
可能包含查詢參數(shù)。
Groovy DSL。
org.springframework.cloud.contract.spec.Contract.make { request { //... method GET() urlPath('/users') { // Each parameter is specified in form // `'paramName' : paramValue` where parameter value // may be a simple literal or one of matcher functions, // all of which are used in this example. queryParameters { // If a simple literal is used as value // default matcher function is used (equalTo) parameter 'limit': 100 // `equalTo` function simply compares passed value // using identity operator (==). parameter 'filter': equalTo("email") // `containing` function matches strings // that contains passed substring. parameter 'gender': value(consumer(containing("[mf]")), producer('mf')) // `matching` function tests parameter // against passed regular expression. parameter 'offset': value(consumer(matching("[0-9]+")), producer(123)) // `notMatching` functions tests if parameter // does not match passed regular expression. parameter 'loginStartsWith': value(consumer(notMatching(".{0,2}")), producer(3)) } } //... } response { //... status 200 } }
YAML。
request: ... queryParameters: a: b b: c headers: foo: bar fooReq: baz cookies: foo: bar fooReq: baz body: foo: bar matchers: body: - path: $.foo type: by_regex value: bar headers: - key: foo regex: bar response: status: 200 fixedDelayMilliseconds: 1000 headers: foo2: bar foo3: foo33 fooRes: baz body: foo2: bar foo3: baz nullValue: null matchers: body: - path: $.foo2 type: by_regex value: bar - path: $.foo3 type: by_command value: executeMe($it) - path: $.nullValue type: by_null value: null headers: - key: foo2 regex: bar - key: foo3 command: andMeToo($it) cookies: - key: foo2 regex: bar - key: foo3 predefined:
request
可能包含其他請求標(biāo)頭,如以下示例所示:
Groovy DSL。
org.springframework.cloud.contract.spec.Contract.make { request { //... method GET() url "/foo" // Each header is added in form `'Header-Name' : 'Header-Value'`. // there are also some helper methods headers { header 'key': 'value' contentType(applicationJson()) } //... } response { //... status 200 } }
YAML。
request: ... headers: foo: bar fooReq: baz
request
可能包含其他請求cookie,如以下示例所示:
Groovy DSL。
org.springframework.cloud.contract.spec.Contract.make { request { //... method GET() url "/foo" // Each Cookies is added in form `'Cookie-Key' : 'Cookie-Value'`. // there are also some helper methods cookies { cookie 'key': 'value' cookie('another_key', 'another_value') } //... } response { //... status 200 } }
YAML。
request: ... cookies: foo: bar fooReq: baz
request
可能包含一個請求正文:
Groovy DSL。
org.springframework.cloud.contract.spec.Contract.make { request { //... method GET() url "/foo" // Currently only JSON format of request body is supported. // Format will be determined from a header or body's content. body '''{ "login" : "john", "name": "John The Contract" }''' } response { //... status 200 } }
YAML。
request: ... body: foo: bar
request
可能包含多部分元素。要包含多部分元素,請使用multipart
方法/部分,如以下示例所示
Groovy DSL。
YAML。
request: method: PUT url: /multipart headers: Content-Type: multipart/form-data;boundary=AaB03x multipart: params: # key (parameter name), value (parameter value) pair formParameter: '"formParameterValue"' someBooleanParameter: true named: - paramName: file fileName: filename.csv fileContent: file content matchers: multipart: params: - key: formParameter regex: ".+" - key: someBooleanParameter predefined: any_boolean named: - paramName: file fileName: predefined: non_empty fileContent: predefined: non_empty response: status: 200
在前面的示例中,我們以兩種方式之一定義參數(shù):
Groovy DSL
formParameter: $(consumer(…?), producer(…?))
)。named(…?)
方法。命名參數(shù)可以設(shè)置name
和content
。您可以通過帶有兩個參數(shù)的方法(例如,named("fileName", "fileContent")
)或通過映射表示法(例如,named(name: "fileName", content: "fileContent")
)來調(diào)用它。YAML
multipart.params
部分設(shè)置多部分參數(shù)multipart.named
部分設(shè)置命名參數(shù)(給定參數(shù)名稱的fileName
和fileContent
)。該部分包含paramName
(參數(shù)名稱),fileName
(文件名稱),fileContent
(文件內(nèi)容)字段可以通過matchers.multipart
部分設(shè)置動態(tài)位
params
部分,該部分可以接受regex
或predefined
正則表達(dá)式named
部分,其中首先通過paramName
定義參數(shù)名稱,然后可以通過regex
或{12 /傳遞fileName
或fileContent
的參數(shù)化} 正則表達(dá)式根據(jù)該合同,生成的測試如下:
// given: MockMvcRequestSpecification request = given() .header("Content-Type", "multipart/form-data;boundary=AaB03x") .param("formParameter", "\"formParameterValue\"") .param("someBooleanParameter", "true") .multiPart("file", "filename.csv", "file content".getBytes()); // when: ResponseOptions response = given().spec(request) .put("/multipart"); // then: assertThat(response.statusCode()).isEqualTo(200);
WireMock存根如下:
''' { "request" : { "url" : "/multipart", "method" : "PUT", "headers" : { "Content-Type" : { "matches" : "multipart/form-data;boundary=AaB03x.*" } }, "bodyPatterns" : [ { "matches" : ".*--(.*)\\r\\nContent-Disposition: form-data; name=\\"formParameter\\"\\r\\n(Content-Type: .*\\r\\n)?(Content-Transfer-Encoding: .*\\r\\n)?(Content-Length: \\\\d+\\r\\n)?\\r\\n\\".+\\"\\r\\n--\\\\1.*" }, { "matches" : ".*--(.*)\\r\\nContent-Disposition: form-data; name=\\"someBooleanParameter\\"\\r\\n(Content-Type: .*\\r\\n)?(Content-Transfer-Encoding: .*\\r\\n)?(Content-Length: \\\\d+\\r\\n)?\\r\\n(true|false)\\r\\n--\\\\1.*" }, { "matches" : ".*--(.*)\\r\\nContent-Disposition: form-data; name=\\"file\\"; filename=\\"[\\\\S\\\\s]+\\"\\r\\n(Content-Type: .*\\r\\n)?(Content-Transfer-Encoding: .*\\r\\n)?(Content-Length: \\\\d+\\r\\n)?\\r\\n[\\\\S\\\\s]+\\r\\n--\\\\1.*" } ] }, "response" : { "status" : 200, "transformers" : [ "response-template", "foo-transformer" ] } } '''
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: