-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPactSpec.groovy
93 lines (83 loc) · 2.83 KB
/
PactSpec.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
package com.example
import au.com.dius.pact.consumer.PactVerified$
import au.com.dius.pact.consumer.VerificationResult
import au.com.dius.pact.consumer.groovy.PactBuilder
import org.apache.http.client.fluent.Executor
import org.apache.http.client.fluent.Request
import org.apache.http.entity.ContentType
import org.junit.After
import org.junit.Test
class PactSpec {
@After
void teardown() {
Executor.closeIdleConnections()
}
@Test
void 'a request to create user'() {
PactBuilder legacyWebappService = new PactBuilder()
legacyWebappService {
serviceConsumer 'MyService'
hasPactWith 'LegacyService'
port 1234
given("Auth token valid")
uponReceiving('a request to create a user')
withAttributes(
method: 'post',
path: '/user',
headers: ['Accept': regexp(~/.*application\/json.*/, 'application/json'), 'AUTH-TOKEN': "MyKey"]
)
withBody {
firstName "First"
lastName "last"
}
willRespondWith(
status: 200,
headers: ['Content-Type': regexp(~/.*application\/json.*/, 'application/json')])
withBody {
status true
data {
someStuff "here"
}
}
}
VerificationResult result = legacyWebappService.run {
def response = Request.Post("http://localhost:1234/user")
.addHeader("Accept", "application/json")
.addHeader("AUTH-TOKEN", "MyKey")
.bodyString("{ \"firstName\": \"First\", \"lastName\": \"last\" }", ContentType.APPLICATION_JSON)
.execute().returnResponse()
assert response.getStatusLine().statusCode == 200
}
assert result == PactVerified$.MODULE$
}
@Test
void 'a request to create user with invalid auth token'() {
PactBuilder legacyWebappService = new PactBuilder()
legacyWebappService {
serviceConsumer 'MyService'
hasPactWith 'LegacyService'
port 1234
given("Aut htoken invalid")
uponReceiving('a request to create a user with an invalid auth token')
withAttributes(
method: 'post',
path: '/user',
headers: ['Accept': regexp(~/.*application\/json.*/, 'application/json'), 'AUTH-TOKEN': "MyKey"]
)
withBody {
firstName "First"
lastName "last"
}
willRespondWith(status: 401)
}
VerificationResult result = legacyWebappService.run {
def response = Request.Post("http://localhost:1234/user")
.addHeader("Accept", "application/json")
.addHeader("AUTH-TOKEN", "MyKey")
.bodyString("{ \"firstName\": \"First\", \"lastName\": \"last\" }", ContentType.APPLICATION_JSON)
.execute().returnResponse()
assert response.getStatusLine().statusCode == 401
}
assert result == PactVerified$.MODULE$
}
}