From ee2754211cfdafcb1e193118de7ec56b75afe03d Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Mon, 14 Jun 2021 23:38:46 +1000 Subject: [PATCH] chore: use a products example in the basic consumer test --- examples/basic_test.go | 47 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/examples/basic_test.go b/examples/basic_test.go index 7a66e6562..0d32b9b7c 100644 --- a/examples/basic_test.go +++ b/examples/basic_test.go @@ -10,23 +10,23 @@ 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 { @@ -34,50 +34,49 @@ func TestUserAPIClient(t *testing.T) { 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 }