forked from pact-foundation/pact-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
71 lines (62 loc) · 1.49 KB
/
consumer.go
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
// Package main contains a runnable Consumer Pact test example.
package main
import (
"fmt"
"log"
"net/http"
"github.com/pact-foundation/pact-go/dsl"
)
// Example Pact: How to run me!
// 1. Start the daemon with `./pact-go daemon`
// 2. cd <pact-go>/examples
// 3. go run consumer.go
func main() {
// Create Pact connecting to local Daemon
pact := &dsl.Pact{
Port: 6666, // Ensure this port matches the daemon port!
Consumer: "My Consumer",
Provider: "My Provider",
}
defer pact.Teardown()
// Pass in test case
var test = func() error {
_, err := http.Get(fmt.Sprintf("http://localhost:%d/foobar", pact.Server.Port))
if err != nil {
log.Fatalf("Error sending request: %v", err)
}
_, err = http.Get(fmt.Sprintf("http://localhost:%d/bazbat", pact.Server.Port))
if err != nil {
log.Fatalf("Error sending request: %v", err)
}
return err
}
// Set up our interactions. Note we have multiple in this test case!
pact.
AddInteraction().
Given("Some state").
UponReceiving("Some name for the test").
WithRequest(dsl.Request{
Method: "GET",
Path: "/foobar",
}).
WillRespondWith(dsl.Response{
Status: 200,
})
pact.
AddInteraction().
Given("Some state2").
UponReceiving("Some name for the test").
WithRequest(dsl.Request{
Method: "GET",
Path: "/bazbat",
}).
WillRespondWith(dsl.Response{
Status: 200,
})
// Verify
err := pact.Verify(test)
if err != nil {
log.Fatalf("Error on Verify: %v", err)
}
fmt.Println("Test Passed!")
}