Skip to content

Commit

Permalink
chore: use a products example in the basic consumer test
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Jun 14, 2021
1 parent 1fd7edc commit ee27542
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions examples/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,74 +10,73 @@ import (
"github.com/stretchr/testify/assert"
)

func TestUserAPIClient(t *testing.T) {
func TestProductAPIClient(t *testing.T) {
// Specify the two applications in the integration we are testing
// NOTE: this can usually be extracted out of the individual test for re-use)
mockProvider, err := NewV2Pact(MockHTTPProviderConfig{
Consumer: "UserAPIConsumer",
Provider: "UserAPI",
Consumer: "ProductAPIConsumer",
Provider: "ProductAPI",
})
assert.NoError(t, err)

// Arrange: Setup our expected interactions
mockProvider.
AddInteraction().
Given("A user with ID 10 exists").
UponReceiving("A request for User 10").
WithRequest("GET", S("/user/10")).
Given("A product with ID 10 exists").
UponReceiving("A request for Product 10").
WithRequest("GET", S("/products/10")).
WillRespondWith(200).
WithBodyMatch(&User{})
WithBodyMatch(&Product{})

// Act: test our API client behaves correctly
err = mockProvider.ExecuteTest(func(config MockServerConfig) error {
// Initialise the API client and point it at the Pact mock server
client := newClient(config.Host, config.Port)

// Execute the API client
user, err := client.GetUser("10")
product, err := client.GetProduct("10")

// Assert: check the result
assert.NoError(t, err)
assert.Equal(t, 10, user.ID)
assert.Equal(t, 10, product.ID)

return err
})
assert.NoError(t, err)
}

// User domain model
type User struct {
ID int `json:"id" pact:"example=10"`
Name string `json:"name" pact:"example=Billy"`
LastName string `json:"lastName" pact:"example=Sampson"`
Date string `json:"datetime" pact:"example=2020-01-01'T'08:00:45,format=yyyy-MM-dd'T'HH:mm:ss,generator=datetime"`
// Product domain model
type Product struct {
ID int `json:"id" pact:"example=10"`
Name string `json:"name" pact:"example=Billy"`
Price string `json:"price" pact:"example=23.33"`
}

// User API Client to test
type userAPIClient struct {
// Product API Client to test
type productAPIClient struct {
port int
host string
}

func newClient(host string, port int) *userAPIClient {
return &userAPIClient{
func newClient(host string, port int) *productAPIClient {
return &productAPIClient{
host: host,
port: port,
}
}

func (u *userAPIClient) GetUser(id string) (*User, error) {
resp, err := http.Get(fmt.Sprintf("http://%s:%d:%s%s", u.host, u.port, "/user/", id))
func (u *productAPIClient) GetProduct(id string) (*Product, error) {
resp, err := http.Get(fmt.Sprintf("http://%s:%d:%s%s", u.host, u.port, "/products/", id))

if err != nil {
return nil, err
}

user := new(User)
err = json.NewDecoder(resp.Body).Decode(user)
product := new(Product)
err = json.NewDecoder(resp.Body).Decode(product)
if err != nil {
return nil, err
}

return user, err
return product, err
}

0 comments on commit ee27542

Please sign in to comment.