Skip to content

Commit

Permalink
upgrade(broker): allow pb:pacts and pacts HAL links to be followed
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed May 20, 2018
1 parent 28cd1f8 commit 2e0f236
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
9 changes: 8 additions & 1 deletion dsl/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ type PactLink struct {

// HalLinks represents the _links key in a HAL document.
type HalLinks struct {
Pacts []PactLink `json:"pb:pacts"`
Pacts []PactLink `json:"pb:pacts"`
OldPacts []PactLink `json:"pacts"`
}

// HalDoc is a simple representation of the HAL response from a Pact Broker.
Expand Down Expand Up @@ -101,9 +102,15 @@ func findConsumers(provider string, request *types.VerifyRequest) error {
return err
}

for _, p := range doc.Links.OldPacts {
pactURLs[p.Title] = p.Href
}

for _, p := range doc.Links.Pacts {
pactURLs[p.Title] = p.Href
}

fmt.Println(pactURLs)
}

// Scrub out duplicate pacts across tags (e.g. 'latest' may equal 'prod' pact)
Expand Down
32 changes: 16 additions & 16 deletions dsl/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/pact-foundation/pact-go/utils"
)

func TestPact_findConsumersNoTags(t *testing.T) {
func TestBroker_findConsumersNoTags(t *testing.T) {
s := setupMockBroker(false)
defer s.Close()
request := types.VerifyRequest{
Expand All @@ -22,8 +22,8 @@ func TestPact_findConsumersNoTags(t *testing.T) {
t.Fatalf("Error: %s", err.Error())
}

if len(request.PactURLs) != 2 {
t.Fatalf("Expected 2 PactURLs but got: %d", len(request.PactURLs))
if len(request.PactURLs) != 4 {
t.Fatalf("Expected 4 PactURLs but got: %d", len(request.PactURLs))
}

pactURL := fmt.Sprintf("%s/pacts/provider/bobby/consumer/jessica/version/2.0.0", s.URL)
Expand All @@ -32,7 +32,7 @@ func TestPact_findConsumersNoTags(t *testing.T) {
}
}

func TestPact_findConsumersWithTags(t *testing.T) {
func TestBroker_findConsumersWithTags(t *testing.T) {
s := setupMockBroker(false)
defer s.Close()
request := types.VerifyRequest{
Expand All @@ -44,8 +44,8 @@ func TestPact_findConsumersWithTags(t *testing.T) {
t.Fatalf("Error: %s", err.Error())
}

if len(request.PactURLs) != 2 {
t.Fatalf("Expected 2 PactURLs but got: %d", len(request.PactURLs))
if len(request.PactURLs) != 4 {
t.Fatalf("Expected 4 PactURLs but got: %d", len(request.PactURLs))
}

pactURL := fmt.Sprintf("%s/pacts/provider/bobby/consumer/billy/version/1.0.1", s.URL)
Expand All @@ -54,7 +54,7 @@ func TestPact_findConsumersWithTags(t *testing.T) {
}
}

func TestPact_findConsumersBrokerDown(t *testing.T) {
func TestBroker_findConsumersBrokerDown(t *testing.T) {
port, _ := utils.GetFreePort()
request := types.VerifyRequest{
Tags: []string{"dev", "prod"},
Expand All @@ -67,7 +67,7 @@ func TestPact_findConsumersBrokerDown(t *testing.T) {
}
}

func TestPact_findConsumersInvalidResponse(t *testing.T) {
func TestBroker_findConsumersInvalidResponse(t *testing.T) {
s := setupMockBroker(false)
defer s.Close()
request := types.VerifyRequest{
Expand All @@ -81,7 +81,7 @@ func TestPact_findConsumersInvalidResponse(t *testing.T) {
}
}

func TestPact_findConsumersInvalidURL(t *testing.T) {
func TestBroker_findConsumersInvalidURL(t *testing.T) {
request := types.VerifyRequest{
BrokerURL: "%%%",
}
Expand All @@ -92,7 +92,7 @@ func TestPact_findConsumersInvalidURL(t *testing.T) {
}
}

func TestPact_findConsumersErrorResponse(t *testing.T) {
func TestBroker_findConsumersErrorResponse(t *testing.T) {
s := setupMockBroker(false)
defer s.Close()
request := types.VerifyRequest{
Expand All @@ -106,7 +106,7 @@ func TestPact_findConsumersErrorResponse(t *testing.T) {
}
}

func TestPact_findConsumersNoConsumers(t *testing.T) {
func TestBroker_findConsumersNoConsumers(t *testing.T) {
s := setupMockBroker(false)
defer s.Close()
request := types.VerifyRequest{
Expand All @@ -119,7 +119,7 @@ func TestPact_findConsumersNoConsumers(t *testing.T) {
}
}

func TestPact_findConsumersAuthenticated(t *testing.T) {
func TestBroker_findConsumersAuthenticated(t *testing.T) {
s := setupMockBroker(true)
defer s.Close()
request := types.VerifyRequest{
Expand All @@ -134,7 +134,7 @@ func TestPact_findConsumersAuthenticated(t *testing.T) {
}
}

func TestPact_findConsumersAuthenticatedFail(t *testing.T) {
func TestBroker_findConsumersAuthenticatedFail(t *testing.T) {
s := setupMockBroker(true)
defer s.Close()
request := types.VerifyRequest{
Expand Down Expand Up @@ -186,15 +186,15 @@ func setupMockBroker(auth bool) *httptest.Server {
// curl --user pactuser:pact -H "accept: application/hal+json" "http://pact.onegeek.com.au/pacts/provider/bobby/latest"
mux.HandleFunc("/pacts/provider/bobby/latest", authFunc(func(w http.ResponseWriter, req *http.Request) {
log.Println("[DEBUG] get pacts for provider 'bobby'")
fmt.Fprintf(w, `{"_links":{"self":{"href":"%s/pacts/provider/bobby/latest","title":"Latest pact versions for the provider bobby"},"provider":{"href":"%s/pacticipants/bobby","title":"bobby"},"pb:pacts":[{"href":"%s/pacts/provider/bobby/consumer/jessica/version/2.0.0","title":"Pact between jessica (v2.0.0) and bobby","name":"jessica"},{"href":"%s/pacts/provider/bobby/consumer/billy/version/1.0.0","title":"Pact between billy (v1.0.0) and bobby","name":"billy"}]}}`, server.URL, server.URL, server.URL, server.URL)
fmt.Fprintf(w, `{"_links":{"self":{"href":"%s/pacts/provider/bobby/latest","title":"Latest pact versions for the provider bobby"},"provider":{"href":"%s/pacticipants/bobby","title":"bobby"},"pb:pacts":[{"href":"%s/pacts/provider/bobby/consumer/jessica/version/2.0.0","title":"Pact between jessica (v2.0.0) and bobby","name":"jessica"},{"href":"%s/pacts/provider/bobby/consumer/billy/version/1.0.0","title":"Pact between billy (v1.0.0) and bobby","name":"billy"}],"pacts":[{"href":"%s/pacts/provider/bobby/consumer/jessica/version/2.0.0","title":"OLD Pact between jessica (v2.0.0) and bobby","name":"jessica"},{"href":"%s/pacts/provider/bobby/consumer/billy/version/1.0.0","title":"OLD Pact between billy (v1.0.0) and bobby","name":"billy"}]}}`, server.URL, server.URL, server.URL, server.URL, server.URL, server.URL)
w.Header().Add("Content-Type", "application/hal+json")
}))

// Find 'bobby' consumers for tag 'prod'
// curl --user pactuser:pact -H "accept: application/hal+json" "http://pact.onegeek.com.au/pacts/provider/bobby/latest/sit4"
mux.Handle("/pacts/provider/bobby/latest/prod", authFunc(func(w http.ResponseWriter, req *http.Request) {
log.Println("[DEBUG] get all pacts for provider 'bobby' where the tag 'prod' exists")
fmt.Fprintf(w, `{"_links":{"self":{"href":"%s/pacts/provider/bobby/latest/dev","title":"Latest pact versions for the provider bobby with tag 'dev'"},"provider":{"href":"%s/pacticipants/bobby","title":"bobby"},"pb:pacts":[{"href":"%s/pacts/provider/bobby/consumer/billy/version/1.0.0","title":"Pact between billy (v1.0.0) and bobby","name":"billy"}]}}`, server.URL, server.URL, server.URL)
fmt.Fprintf(w, `{"_links":{"self":{"href":"%s/pacts/provider/bobby/latest/dev","title":"Latest pact versions for the provider bobby with tag 'dev'"},"provider":{"href":"%s/pacticipants/bobby","title":"bobby"},"pb:pacts":[{"href":"%s/pacts/provider/bobby/consumer/billy/version/1.0.0","title":"Pact between billy (v1.0.0) and bobby","name":"billy"}],"pacts":[{"href":"%s/pacts/provider/bobby/consumer/billy/version/1.0.0","title":"OLD Pact between billy (v1.0.0) and bobby","name":"billy"}]}}`, server.URL, server.URL, server.URL, server.URL)
w.Header().Add("Content-Type", "application/hal+json")
}))

Expand All @@ -217,7 +217,7 @@ func setupMockBroker(auth bool) *httptest.Server {
// curl --user pactuser:pact -H "accept: application/hal+json" "http://pact.onegeek.com.au/pacts/provider/bobby/latest/sit4"
mux.Handle("/pacts/provider/bobby/latest/dev", authFunc(func(w http.ResponseWriter, req *http.Request) {
log.Println("[DEBUG] get all pacts for provider 'bobby' where the tag 'dev' exists")
fmt.Fprintf(w, `{"_links":{"self":{"href":"%s/pacts/provider/bobby/latest/dev","title":"Latest pact versions for the provider bobby with tag 'dev'"},"provider":{"href":"%s/pacticipants/bobby","title":"bobby"},"pb:pacts":[{"href":"%s/pacts/provider/bobby/consumer/billy/version/1.0.1","title":"Pact between billy (v1.0.1) and bobby","name":"billy"}]}}`, server.URL, server.URL, server.URL)
fmt.Fprintf(w, `{"_links":{"self":{"href":"%s/pacts/provider/bobby/latest/dev","title":"Latest pact versions for the provider bobby with tag 'dev'"},"provider":{"href":"%s/pacticipants/bobby","title":"bobby"},"pb:pacts":[{"href":"%s/pacts/provider/bobby/consumer/billy/version/1.0.1","title":"Pact between billy (v1.0.1) and bobby","name":"billy"}],"pacts":[{"href":"%s/pacts/provider/bobby/consumer/billy/version/1.0.1","title":"OLD Pact between billy (v1.0.1) and bobby","name":"billy"}]}}`, server.URL, server.URL, server.URL, server.URL)
w.Header().Add("Content-Type", "application/hal+json")
}))

Expand Down

0 comments on commit 2e0f236

Please sign in to comment.