-
Notifications
You must be signed in to change notification settings - Fork 1
/
bookclient_test.go
113 lines (96 loc) · 2.4 KB
/
bookclient_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package book_test
import (
"errors"
"fmt"
"github.com/pact-foundation/pact-go/dsl"
"github.com/stretchr/testify/suite"
book "github.com/testingallthethings/038-pact-http-consumer-go"
"net/http"
"testing"
)
type BookClientSuite struct {
suite.Suite
}
var (
pact *dsl.Pact
)
func TestBookClientSuite(t *testing.T) {
suite.Run(t, new(BookClientSuite))
}
func (s *BookClientSuite) SetupSuite() {
pact = &dsl.Pact{
Consumer: "MarksBookClient",
Provider: "BookApi",
PactDir: "./pacts",
}
}
func (s *BookClientSuite) TearDownSuite() {
pact.Teardown()
}
func (s *BookClientSuite) TestGetBookThatDoesNotExist() {
pact.AddInteraction().
Given("There is not a book with ISBN 123456789").
UponReceiving("A GET request for book with ISBN 123456789").
WithRequest(
dsl.Request{
Method: http.MethodGet,
Path: dsl.String("/book/123456789"),
Headers: dsl.MapMatcher{
"Accept": dsl.String("application/json"),
},
},
).
WillRespondWith(
dsl.Response{
Status: http.StatusNotFound,
Headers: dsl.MapMatcher{
"Content-Type": dsl.String("application/json"),
},
Body: dsl.Match(book.JsonError{}),
},
)
test := func() error {
c := book.NewClient(fmt.Sprintf("http://localhost:%d", pact.Server.Port))
_, err := c.GetBook("123456789")
if err.Error() != "1234 - No book with ISBN 123456789" {
return errors.New("error returned not as expected")
}
return nil
}
s.NoError(pact.Verify(test))
}
func (s *BookClientSuite) TestGetBookThatDoesExist() {
pact.AddInteraction().
Given("Book with ISBN 987654321 exists").
UponReceiving("A GET request for book with ISBN 987654321").
WithRequest(
dsl.Request{
Method: http.MethodGet,
Path: dsl.String("/book/987654321"),
Headers: dsl.MapMatcher{
"Accept": dsl.String("application/json"),
},
},
).
WillRespondWith(
dsl.Response{
Status: http.StatusOK,
Headers: dsl.MapMatcher{
"Content-Type": dsl.String("application/json"),
},
Body: dsl.Match(book.Book{}),
},
)
test := func() error {
c := book.NewClient(fmt.Sprintf("http://localhost:%d", pact.Server.Port))
book, err := c.GetBook("987654321")
s.NoError(err)
s.Equal("987654321", book.ISBN)
s.Equal("Testing All The Things", book.Title)
s.Equal("testing.jpg", book.Image)
s.Equal("Computers", book.Genre)
s.Equal(2021, book.YearPublished)
return nil
}
s.NoError(pact.Verify(test))
}