forked from ymruan/pact-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder_test.go
75 lines (64 loc) · 2.06 KB
/
builder_test.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
72
73
74
75
package pact
import (
"net/http"
"testing"
"github.com/SEEK-Jobs/pact-go/provider"
)
func Test_Builder_CanBuild(t *testing.T) {
builder := NewConsumerPactBuilder(&BuilderConfig{PactPath: "./pact_examples"}).
ServiceConsumer("chrome browser").
HasPactWith("go api")
ps, _ := builder.GetMockProviderService()
request := provider.NewJSONRequest("GET", "/user", "id=23", nil)
header := make(http.Header)
header.Add("content-type", "application/json")
response := provider.NewJSONResponse(200, header)
response.SetBody(`{ "id": 23, "firstName": "John", "lastName": "Doe" }`)
if err := ps.Given("there is a user with id {23}").
UponReceiving("get request for user with id {23}").
With(*request).
WillRespondWith(*response); err != nil {
t.Error(err)
t.FailNow()
}
request.Query = "id=200"
response.Status = 404
response.Headers = nil
response.ResetContent()
ps.Given("there is no user with id {200}").
UponReceiving("get request for user with id {200}").
With(*request).
WillRespondWith(*response)
if err := builder.Build(); err != nil {
t.Error(err)
}
}
func Test_Builder_CannotBuild_WhenThereIsNoConsumer(t *testing.T) {
builder := NewConsumerPactBuilder(&BuilderConfig{PactPath: "./"}).
HasPactWith("serviceprovider")
if err := builder.Build(); err != nil {
if err != errInvalidConsumer {
t.Error("expected invalid consumer error")
}
} else {
t.Error("should not build without consumer")
}
}
func Test_Builder_CannotBuild_WhenThereIsNoProvider(t *testing.T) {
builder := NewConsumerPactBuilder(&BuilderConfig{PactPath: "./"}).
ServiceConsumer("consumer")
if err := builder.Build(); err != nil {
if err != errInvalidProvider {
t.Error("expected invalid provider error")
}
} else {
t.Error("should not build without consumer")
}
}
func Test_Builder_CannotBuild_WhenPactCannotBePersisted(t *testing.T) {
builder := NewConsumerPactBuilder(&BuilderConfig{PactPath: "//3434"}).
ServiceConsumer("consumer").HasPactWith("serviceProvider")
if err := builder.Build(); err == nil {
t.Error("should not build without consumer")
}
}