Skip to content

Commit

Permalink
feat(examples): all examples are now E2E regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Aug 14, 2017
1 parent 5d81925 commit 72b7e52
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 325 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,15 @@ be a valid JSON value: e.g. strings, numbers and objects.
Here is a complex example that shows how all 3 terms can be used together:

```go
jumper := Like(`"jumper"`)
shirt := Like(`"shirt"`)
tag := EachLike(fmt.Sprintf(`[%s, %s]`, jumper, shirt), 2)
size := Like(10)
colour := Term("red", "red|green|blue")

match := EachLike(
EachLike(
fmt.Sprintf(`{
"size": %s,
"size": 10,
"colour": %s,
"tag": %s
}`, size, colour, tag),
"tag": [["jumper", "shirt]]
}`, colour)
1),
1))
```
Expand Down
27 changes: 11 additions & 16 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,17 @@ cases.
Here is a complex example that shows how all 3 terms can be used together:
jumper := Like(`"jumper"`)
shirt := Like(`"shirt"`)
tag := EachLike(fmt.Sprintf(`[%s, %s]`, jumper, shirt), 2)
size := Like(10)
colour := Term("red", "red|green|blue")
match := EachLike(
EachLike(
fmt.Sprintf(
`{
"size": %s,
"colour": %s,
"tag": %s
}`, size, colour, tag),
1),
1))
colour := Term("red", "red|green|blue")
match := EachLike(
EachLike(
fmt.Sprintf(`{
"size": 10,
"colour": %s,
"tag": [["jumper", "shirt]]
}`, colour)
1),
1))
This example will result in a response body from the mock server that looks like:
Expand Down
2 changes: 1 addition & 1 deletion examples/consumer/goconsumer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *Client) login(username string, password string) (*User, error) {
"password": "%s"
}`, username, password)

res, err := http.Post(fmt.Sprintf("%s/users/login", c.Host), "application/json", bytes.NewReader([]byte(loginRequest)))
res, err := http.Post(fmt.Sprintf("%s/users/login", c.Host), "application/json; charset=utf-8", bytes.NewReader([]byte(loginRequest)))
if res.StatusCode != 200 || err != nil {
return nil, fmt.Errorf("login failed")
}
Expand Down
86 changes: 60 additions & 26 deletions examples/consumer/goconsumer/user_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,34 @@ import (
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"

"github.com/pact-foundation/pact-go/dsl"
"github.com/pact-foundation/pact-go/types"
)

// Common test data
var dir, _ = os.Getwd()
var pactDir = fmt.Sprintf("%s/../../pacts", dir)
var logDir = fmt.Sprintf("%s/log", dir)
var pact dsl.Pact
var loginRequest = ` { "username":"billy", "password": "issilly" }`
var form url.Values
var rr http.ResponseWriter
var req *http.Request

// var name = "Jean-Marie de La Beaujardière😀😍"
var name = "billy"
var like = dsl.Like
var eachLike = dsl.EachLike
var term = dsl.Term
var loginRequest = fmt.Sprintf(`{ "username":"%s", "password": "issilly" }`, name)

var commonHeaders = map[string]string{
"Content-Type": "application/json; charset=utf-8",
}

// Use this to control the setup and teardown of Pact
func TestMain(m *testing.M) {
// Setup Pact and related test stuff
Expand Down Expand Up @@ -82,7 +94,16 @@ func TestPactConsumerLoginHandler_UserExists(t *testing.T) {

return nil
}

body :=
like(fmt.Sprintf(
`{
"user": {
"name": "%s",
"type": %v
}
}`, name, term("admin", "admin|user|guest")))

// Pull from pact broker, used in e2e/integrated tests for pact-go release
// Setup interactions on the Mock Service. Note that you can have multiple
// interactions
pact.
Expand All @@ -96,16 +117,7 @@ func TestPactConsumerLoginHandler_UserExists(t *testing.T) {
}).
WillRespondWith(dsl.Response{
Status: 200,
Headers: map[string]string{
"Content-Type": "application/json; charset=utf-8",
},
Body: `
{
"user": {
"name": "billy"
}
}
`,
Body: body,
})

err := pact.Verify(testBillyExists)
Expand Down Expand Up @@ -133,15 +145,14 @@ func TestPactConsumerLoginHandler_UserDoesNotExist(t *testing.T) {
Given("User billy does not exist").
UponReceiving("A request to login with user 'billy'").
WithRequest(dsl.Request{
Method: "POST",
Path: "/users/login",
Body: loginRequest,
Method: "POST",
Path: "/users/login",
Body: loginRequest,
Headers: commonHeaders,
}).
WillRespondWith(dsl.Response{
Status: 404,
Headers: map[string]string{
"Content-Type": "application/json; charset=utf-8",
},
Status: 404,
Headers: commonHeaders,
})

err := pact.Verify(testBillyDoesNotExists)
Expand Down Expand Up @@ -169,19 +180,42 @@ func TestPactConsumerLoginHandler_UserUnauthorised(t *testing.T) {
Given("User billy is unauthorized").
UponReceiving("A request to login with user 'billy'").
WithRequest(dsl.Request{
Method: "POST",
Path: "/users/login",
Body: loginRequest,
Method: "POST",
Path: "/users/login",
Body: loginRequest,
Headers: commonHeaders,
}).
WillRespondWith(dsl.Response{
Status: 401,
Headers: map[string]string{
"Content-Type": "application/json; charset=utf-8",
},
Status: 401,
Headers: commonHeaders,
})

err := pact.Verify(testBillyUnauthorized)
if err != nil {
t.Fatalf("Error on Verify: %v", err)
}
}

func TestPactConsumerLoginHandler_Publish(t *testing.T) {
// Enable when running E2E/integration tests before a release
if os.Getenv("PACT_INTEGRATED_TESTS") != "" {
var brokerHost = os.Getenv("PACT_BROKER_HOST")

// Publish the Pacts...
p := dsl.Publisher{}
err := p.Publish(types.PublishRequest{
PactURLs: []string{filepath.ToSlash(fmt.Sprintf("%s/billy-bobby.json", pactDir))},
PactBroker: brokerHost,
ConsumerVersion: "1.0.0",
Tags: []string{"latest", "sit4"},
BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"),
BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"),
})

if err != nil {
t.Fatal("Error:", err)
}
} else {
t.Skip()
}
}
Loading

0 comments on commit 72b7e52

Please sign in to comment.