Skip to content

Commit

Permalink
refactor(review): improve API and reduce noise
Browse files Browse the repository at this point in the history
Reduce need for pointers, remove leaky fs interactions types and
update tests.

Rinor albeit breaking changes to dsl package
  • Loading branch information
mefellows committed Jul 3, 2016
1 parent 8b917b1 commit 9c1f206
Show file tree
Hide file tree
Showing 20 changed files with 393 additions and 381 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestLogin(t *testing.T) {

// Create Pact, connecting to local Daemon
// Ensure the port matches the daemon port!
pact := &dsl.Pact{
pact := dsl.Pact{
Port: 6666,
Consumer: "My Consumer",
Provider: "My Provider",
Expand All @@ -101,11 +101,11 @@ func TestLogin(t *testing.T) {
AddInteraction().
Given("User Matt exists"). // Provider State
UponReceiving("A request to login"). // Test Case Name
WithRequest(&dsl.Request{
WithRequest(dsl.Request{
Method: "GET",
Path: "/login",
}).
WillRespondWith(&dsl.Response{
WillRespondWith(dsl.Response{
Status: 200,
})

Expand Down Expand Up @@ -241,15 +241,15 @@ Read more about [flexible matching](https://github.com/realestate-com-au/pact/wi
satisfy the requirements of each of your known consumers:

```go
response := pact.VerifyProvider(&types.VerifyRequest{
response := pact.VerifyProvider(types.VerifyRequest{
ProviderBaseURL: "http://localhost:8000",
PactURLs: []string{"./pacts/my_consumer-my_provider.json"},
ProviderStatesURL: "http://localhost:8000/states",
ProviderStatesSetupURL: "http://localhost:8000/setup",
})

if response.ExitCode != 0 {
t.Fatalf("Got non-zero exit code '%d', expected 0", response.ExitCode)
if err != nil {
t.Fatal("Error:", err)
}
```

Expand All @@ -268,7 +268,7 @@ When validating a Provider, you have 3 options to provide the Pact files:
1. Use `PactURLs` to specify the exact set of pacts to be replayed:

```go
response = pact.VerifyProvider(&types.VerifyRequest{
response = pact.VerifyProvider(types.VerifyRequest{
ProviderBaseURL: "http://myproviderhost",
PactURLs: []string{"http://broker/pacts/provider/them/consumer/me/latest/dev"},
ProviderStatesURL: "http://myproviderhost/states",
Expand All @@ -280,7 +280,7 @@ When validating a Provider, you have 3 options to provide the Pact files:
1. Use `PactBroker` to automatically find all of the latest consumers:

```go
response = pact.VerifyProvider(&types.VerifyRequest{
response = pact.VerifyProvider(types.VerifyRequest{
ProviderBaseURL: "http://myproviderhost",
BrokerURL: "http://brokerHost",
ProviderStatesURL: "http://myproviderhost/states",
Expand All @@ -292,7 +292,7 @@ When validating a Provider, you have 3 options to provide the Pact files:
1. Use `PactBroker` and `Tags` to automatically find all of the latest consumers:

```go
response = pact.VerifyProvider(&types.VerifyRequest{
response = pact.VerifyProvider(types.VerifyRequest{
ProviderBaseURL: "http://myproviderhost",
BrokerURL: "http://brokerHost",
Tags: []string{"latest", "sit4"},
Expand Down Expand Up @@ -387,7 +387,7 @@ on how to make it work for you.
#### Publishing from Go code

```go
pact.PublishPacts(&types.PublishRequest{
pact.PublishPacts(types.PublishRequest{
PactBroker: "http://pactbroker:8000",
PactURLs: []string{"./pacts/my_consumer-my_provider.json"},
ConsumerVersion: "1.0.0",
Expand Down Expand Up @@ -421,7 +421,7 @@ to filter log messages. The CLI already contains flags to manage this,
should you want to control log level in your tests, you can set it like so:

```go
pact := &Pact{
pact := Pact{
...
LogLevel: "DEBUG", // One of DEBUG, INFO, ERROR, NONE
}
Expand Down
22 changes: 11 additions & 11 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ A typical consumer-side test would look something like this:
// Create Pact, connecting to local Daemon
// Ensure the port matches the daemon port!
pact := &Pact{
pact := Pact{
Port: 6666,
Consumer: "My Consumer",
Provider: "My Provider",
Expand All @@ -39,11 +39,11 @@ A typical consumer-side test would look something like this:
AddInteraction().
Given("User Matt exists"). // Provider State
UponReceiving("A request to login"). // Test Case Name
WithRequest(&Request{
WithRequest(Request{
Method: "GET",
Path: "/login",
}).
WillRespondWith(&Response{
WillRespondWith(Response{
Status: 200,
})
Expand Down Expand Up @@ -131,15 +131,15 @@ A typical Provider side test would like something like:
func TestProvider_PactContract(t *testing.T) {
go startMyAPI("http://localhost:8000")
response := pact.VerifyProvider(&types.VerifyRequest{
response := pact.VerifyProvider(types.VerifyRequest{
ProviderBaseURL: "http://localhost:8000",
PactURLs: []string{"./pacts/my_consumer-my_provider.json"},
ProviderStatesURL: "http://localhost:8000/states",
ProviderStatesSetupURL: "http://localhost:8000/setup",
})
if response.ExitCode != 0 {
t.Fatalf("Got non-zero exit code '%d', expected 0", response.ExitCode)
if err != nil {
t.Fatal("Error:", err)
}
}
Expand All @@ -157,7 +157,7 @@ When validating a Provider, you have 3 options to provide the Pact files:
1. Use "PactURLs" to specify the exact set of pacts to be replayed:
response = pact.VerifyProvider(&types.VerifyRequest{
response = pact.VerifyProvider(types.VerifyRequest{
ProviderBaseURL: "http://myproviderhost",
PactURLs: []string{"http://broker/pacts/provider/them/consumer/me/latest/dev"},
ProviderStatesURL: "http://myproviderhost/states",
Expand All @@ -168,7 +168,7 @@ When validating a Provider, you have 3 options to provide the Pact files:
2. Use "PactBroker" to automatically find all of the latest consumers:
response = pact.VerifyProvider(&types.VerifyRequest{
response = pact.VerifyProvider(types.VerifyRequest{
ProviderBaseURL: "http://myproviderhost",
BrokerURL: brokerHost,
ProviderStatesURL: "http://myproviderhost/states",
Expand All @@ -179,7 +179,7 @@ When validating a Provider, you have 3 options to provide the Pact files:
3. Use "PactBroker" and "Tags" to automatically find all of the latest consumers:
response = pact.VerifyProvider(&types.VerifyRequest{
response = pact.VerifyProvider(types.VerifyRequest{
ProviderBaseURL: "http://myproviderhost",
BrokerURL: brokerHost,
Tags: []string{"latest", "sit4"},
Expand Down Expand Up @@ -270,7 +270,7 @@ on how to make it work for you.
Publishing using Go code:
pact.PublishPacts(&types.PublishRequest{
pact.PublishPacts(types.PublishRequest{
PactBroker: "http://pactbroker:8000",
PactURLs: []string{"./pacts/my_consumer-my_provider.json"},
ConsumerVersion: "1.0.0",
Expand Down Expand Up @@ -300,7 +300,7 @@ Pact Go uses a simple log utility (logutils - https://github.com/hashicorp/logut
to filter log messages. The CLI already contains flags to manage this,
should you want to control log level in your tests, you can set it like so:
pact := &Pact{
pact := Pact{
...
LogLevel: "DEBUG", // One of DEBUG, INFO, ERROR, NONE
}
Expand Down
2 changes: 1 addition & 1 deletion dsl/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func findConsumers(provider string, request *types.VerifyRequest) error {

req.Header.Set("Accept", "application/hal+json")

if request != nil && request.BrokerUsername != "" && request.BrokerPassword != "" {
if request.BrokerUsername != "" && request.BrokerPassword != "" {
req.SetBasicAuth(request.BrokerUsername, request.BrokerPassword)
}

Expand Down
36 changes: 18 additions & 18 deletions dsl/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (

func TestPact_findConsumersNoTags(t *testing.T) {
port := setupMockBroker(false)
request := &types.VerifyRequest{
request := types.VerifyRequest{
BrokerURL: fmt.Sprintf("http://localhost:%d", port),
}
err := findConsumers("bobby", request)
err := findConsumers("bobby", &request)
if err != nil {
t.Fatalf("Error: %s", err.Error())
}
Expand All @@ -32,11 +32,11 @@ func TestPact_findConsumersNoTags(t *testing.T) {

func TestPact_findConsumersWithTags(t *testing.T) {
port := setupMockBroker(false)
request := &types.VerifyRequest{
request := types.VerifyRequest{
Tags: []string{"dev", "prod"},
BrokerURL: fmt.Sprintf("http://localhost:%d", port),
}
err := findConsumers("bobby", request)
err := findConsumers("bobby", &request)
if err != nil {
t.Fatalf("Error: %s", err.Error())
}
Expand All @@ -53,11 +53,11 @@ func TestPact_findConsumersWithTags(t *testing.T) {

func TestPact_findConsumersBrokerDown(t *testing.T) {
port, _ := utils.GetFreePort()
request := &types.VerifyRequest{
request := types.VerifyRequest{
Tags: []string{"dev", "prod"},
BrokerURL: fmt.Sprintf("http://localhost:%d", port),
}
err := findConsumers("idontexist", request)
err := findConsumers("idontexist", &request)

if err == nil {
t.Fatalf("Expected error but got none")
Expand All @@ -66,22 +66,22 @@ func TestPact_findConsumersBrokerDown(t *testing.T) {

func TestPact_findConsumersInvalidResponse(t *testing.T) {
port := setupMockBroker(false)
request := &types.VerifyRequest{
request := types.VerifyRequest{
Tags: []string{"broken"},
BrokerURL: fmt.Sprintf("http://localhost:%d", port),
}
err := findConsumers("bobby", request)
err := findConsumers("bobby", &request)

if err == nil {
t.Fatalf("Expected error but got none")
}
}

func TestPact_findConsumersInvalidURL(t *testing.T) {
request := &types.VerifyRequest{
request := types.VerifyRequest{
BrokerURL: "%%%",
}
err := findConsumers("broken", request)
err := findConsumers("broken", &request)

if err == nil {
t.Fatalf("Expected error but got none")
Expand All @@ -90,11 +90,11 @@ func TestPact_findConsumersInvalidURL(t *testing.T) {

func TestPact_findConsumersErrorResponse(t *testing.T) {
port := setupMockBroker(false)
request := &types.VerifyRequest{
request := types.VerifyRequest{
Tags: []string{"dev"},
BrokerURL: fmt.Sprintf("http://localhost:%d", port),
}
err := findConsumers("broken", request)
err := findConsumers("broken", &request)

if err == nil {
t.Fatalf("Expected error but got none")
Expand All @@ -103,37 +103,37 @@ func TestPact_findConsumersErrorResponse(t *testing.T) {

func TestPact_findConsumersNoConsumers(t *testing.T) {
port := setupMockBroker(false)
request := &types.VerifyRequest{
request := types.VerifyRequest{
Tags: []string{"dev", "prod"},
BrokerURL: fmt.Sprintf("http://localhost:%d", port),
}
err := findConsumers("idontexist", request)
err := findConsumers("idontexist", &request)
if err == nil {
t.Fatalf("Expected error but got none")
}
}

func TestPact_findConsumersAuthenticated(t *testing.T) {
port := setupMockBroker(true)
request := &types.VerifyRequest{
request := types.VerifyRequest{
Tags: []string{"dev", "prod"},
BrokerURL: fmt.Sprintf("http://localhost:%d", port),
BrokerUsername: "foo",
BrokerPassword: "bar",
}
err := findConsumers("bobby", request)
err := findConsumers("bobby", &request)
if err != nil {
t.Fatalf("Error: %s", err)
}
}

func TestPact_findConsumersAuthenticatedFail(t *testing.T) {
port := setupMockBroker(true)
request := &types.VerifyRequest{
request := types.VerifyRequest{
Tags: []string{"dev", "prod"},
BrokerURL: fmt.Sprintf("http://localhost:%d", port),
}
err := findConsumers("bobby", request)
err := findConsumers("bobby", &request)

switch err {
case ErrUnauthorized:
Expand Down
12 changes: 8 additions & 4 deletions dsl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (p *PactClient) StartServer(args []string) *types.MockServer {
}

// VerifyProvider runs the verification process against a running Provider.
func (p *PactClient) VerifyProvider(request *types.VerifyRequest) *types.CommandResponse {
func (p *PactClient) VerifyProvider(request types.VerifyRequest) (string, error) {
log.Println("[DEBUG] client: verifying a provider")

port := getPort(request.ProviderBaseURL)
Expand All @@ -120,11 +120,15 @@ func (p *PactClient) VerifyProvider(request *types.VerifyRequest) *types.Command
}
}

return &res
if res.ExitCode > 0 {
return "", fmt.Errorf("provider verification failed: %s", res.Message)
}

return res.Message, err
}

// ListServers lists all running Pact Mock Servers.
func (p *PactClient) ListServers() *types.PactListResponse {
func (p *PactClient) ListServers() types.PactListResponse {
log.Println("[DEBUG] client: listing servers")
var res types.PactListResponse
client, err := getHTTPClient(p.Port)
Expand All @@ -134,7 +138,7 @@ func (p *PactClient) ListServers() *types.PactListResponse {
log.Println("[ERROR] rpc:", err.Error())
}
}
return &res
return res
}

// StopServer stops a remote Pact Mock Server.
Expand Down
Loading

0 comments on commit 9c1f206

Please sign in to comment.