-
Notifications
You must be signed in to change notification settings - Fork 5
/
BodyContentMatcherSpec.groovy
97 lines (81 loc) 路 3.29 KB
/
BodyContentMatcherSpec.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright (C) 2018 Christopher J. Stehno
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.stehno.ersatz.issues
import com.stehno.ersatz.DecodingContext
import com.stehno.ersatz.ErsatzServer
import com.stehno.ersatz.util.HttpClient
import okhttp3.MediaType
import okhttp3.Response
import spock.lang.AutoCleanup
import spock.lang.Specification
import javax.xml.parsers.DocumentBuilderFactory
import static com.stehno.ersatz.ContentType.TEXT_XML
import static com.stehno.ersatz.Decoders.utf8String
import static com.stehno.ersatz.Encoders.text
import static okhttp3.RequestBody.create
import static org.hamcrest.CoreMatchers.equalTo
import static org.hamcrest.xml.HasXPath.hasXPath
class BodyContentMatcherSpec extends Specification {
@AutoCleanup private final ErsatzServer server = new ErsatzServer()
private final HttpClient http = new HttpClient()
void 'matching all of body content'() {
setup:
String requestXml = '<request><node foo="bar"/></request>'
String responseXml = '<response>OK</response>'
server.expectations {
post('/posting') {
decoder 'text/xml; charset=utf-8', utf8String
body requestXml, 'text/xml; charset=utf-8'
responder {
body responseXml, TEXT_XML
encoder TEXT_XML, String, text
}
}
}
when:
Response response = http.post(server.httpUrl('/posting'), create(MediaType.get('text/xml; charset=utf-8'), requestXml))
then:
response.body().string() == responseXml
}
void 'matching part of body content'() {
setup:
String requestXml = '<request><node foo="bar"/></request>'
String responseXml = '<response>OK</response>'
server.expectations {
post('/posting') {
decoder('text/xml; charset=utf-8') { byte[] bytes, DecodingContext ctx ->
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(bytes))
}
body hasXPath('string(//request/node/@foo)', equalTo('bar')), 'text/xml; charset=utf-8'
called 1
responder {
body responseXml, TEXT_XML
encoder TEXT_XML, String, text
}
}
}
when:
Response response = http.post(server.httpUrl('/posting'), create(MediaType.get('text/xml; charset=utf-8'), requestXml))
then:
response.body().string() == responseXml
when:
response = http.post(server.httpUrl('/posting'), create(MediaType.get('text/xml; charset=utf-8'), '<request><node foo="blah"/></request>'))
then:
response.code() == 404
and:
server.verify()
}
}