Skip to content

Commit

Permalink
feat(message): simplify message pact provider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Apr 4, 2018
1 parent 372633e commit 6b2e2c5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 18 deletions.
16 changes: 14 additions & 2 deletions dsl/pact.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ var checkCliCompatibility = func() {
// A Message Producer is analagous to Consumer in the HTTP Interaction model.
// It is the initiator of an interaction, and expects something on the other end
// of the interaction to respond - just in this case, not immediately.
func (p *Pact) VerifyMessageProvider(t *testing.T, request types.VerifyRequest, handlers map[string]func(...interface{}) (map[string]interface{}, error)) (types.ProviderVerifierResponse, error) {
func (p *Pact) VerifyMessageProvider(t *testing.T, request types.VerifyMessageRequest, handlers map[string]func(...interface{}) (map[string]interface{}, error)) (types.ProviderVerifierResponse, error) {
response := types.ProviderVerifierResponse{}

// Starts the message wrapper API with hooks back to the message handlers
Expand All @@ -336,6 +336,18 @@ func (p *Pact) VerifyMessageProvider(t *testing.T, request types.VerifyRequest,
return response, fmt.Errorf("unable to allocate a port for verification: %v", err)
}

// Construct verifier request
verificationRequest := types.VerifyRequest{
ProviderBaseURL: fmt.Sprintf("http://localhost:%d", port),
PactURLs: request.PactURLs,
BrokerURL: request.BrokerURL,
Tags: request.Tags,
BrokerUsername: request.BrokerUsername,
BrokerPassword: request.BrokerPassword,
PublishVerificationResults: request.PublishVerificationResults,
ProviderVersion: request.ProviderVersion,
}

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")

Expand Down Expand Up @@ -396,7 +408,7 @@ func (p *Pact) VerifyMessageProvider(t *testing.T, request types.VerifyRequest,
return response, portErr
}

res, err := p.VerifyProviderRaw(request)
res, err := p.VerifyProviderRaw(verificationRequest)

for _, example := range res.Examples {
t.Run(example.Description, func(st *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions examples/messages/consumer/message_pact_consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ var logDir = fmt.Sprintf("%s/log", dir)
func createPact() dsl.Pact {
// Create Pact connecting to local Daemon
return dsl.Pact{
Consumer: "billy",
Provider: "bobby",
Consumer: "PactGoMessageConsumer",
Provider: "PactGoMessageProvider",
LogDir: logDir,
PactDir: pactDir, // TODO: this seems to cause an issue "NoMethodError: undefined method `content' for #<Pact::Interaction:0x00007fc8f1a082e8>"
LogLevel: "DEBUG",
Expand Down
18 changes: 4 additions & 14 deletions examples/messages/provider/message_pact_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ func TestMessageProvider_Success(t *testing.T) {
pact := createPact()

// Map test descriptions to message producer (handlers)
// TODO: need to agree on the interface for invoking the function
// do we want to pass in args? ...interface{} is a bit of a catch-all
// TODO: convert these all to types to ease readability
functionMappings := map[string]func(...interface{}) (map[string]interface{}, error){
"a test message": func(...interface{}) (map[string]interface{}, error) {
Expand All @@ -31,20 +29,12 @@ func TestMessageProvider_Success(t *testing.T) {
}

// Verify the Provider with local Pact Files
// NOTE: these values don't matter right now,
// the verifier args are hard coded
// TODO: Add function mappings to the VerifyRequest type (or have separate one for producer)
// this can't happen until we remove the RPC shit, because functions can't be mapped
// over the wire
pact.VerifyMessageProvider(t, types.VerifyRequest{
ProviderBaseURL: fmt.Sprintf("http://localhost:%d", port),
PactURLs: []string{filepath.ToSlash(fmt.Sprintf("%s/message-pact.json", pactDir))},
pact.VerifyMessageProvider(t, types.VerifyMessageRequest{
PactURLs: []string{filepath.ToSlash(fmt.Sprintf("%s/message-pact.json", pactDir))},
}, functionMappings)
}

// Configuration / Test Data
// var port, _ = utils.GetFreePort()
var port = 9393
var dir, _ = os.Getwd()
var pactDir = fmt.Sprintf("%s/../pacts", dir)
var logDir = fmt.Sprintf("%s/log", dir)
Expand All @@ -53,8 +43,8 @@ var logDir = fmt.Sprintf("%s/log", dir)
func createPact() dsl.Pact {
// Create Pact connecting to local Daemon
return dsl.Pact{
Consumer: "messageconsumer",
Provider: "messageprovider",
Consumer: "PactGoMessageConsumer",
Provider: "PactGoMessageProvider",
LogDir: logDir,
LogLevel: "DEBUG",
PactFileWriteMode: "update",
Expand Down
68 changes: 68 additions & 0 deletions types/verify_mesage_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package types

import (
"fmt"
)

// VerifyMessageRequest contains the verification params.
// TODO: make this CLI "request" type an Interface (e.g. Validate())
// also make the core of it embeddable to be re-used
type VerifyMessageRequest struct {
// Local/HTTP paths to Pact files.
PactURLs []string

// Pact Broker URL for broker-based verification
BrokerURL string

// Tags to find in Broker for matrix-based testing
Tags []string

// Username when authenticating to a Pact Broker.
BrokerUsername string

// Password when authenticating to a Pact Broker.
BrokerPassword string

// PublishVerificationResults to the Pact Broker.
PublishVerificationResults bool

// ProviderVersion is the semantical version of the Provider API.
ProviderVersion string

// Arguments to the VerificationProvider
// Deprecated: This will be deleted after the native library replaces Ruby deps.
Args []string
}

// Validate checks that the minimum fields are provided.
// Deprecated: This map be deleted after the native library replaces Ruby deps,
// and should not be used outside of this library.
func (v *VerifyMessageRequest) Validate() error {
v.Args = []string{}

if len(v.PactURLs) != 0 {
v.Args = append(v.Args, v.PactURLs...)
} else {
return fmt.Errorf("Pact URLs is mandatory")
}

v.Args = append(v.Args, "--format", "json")

if v.BrokerUsername != "" {
v.Args = append(v.Args, "--broker-username", v.BrokerUsername)
}

if v.BrokerPassword != "" {
v.Args = append(v.Args, "--broker-password", v.BrokerPassword)
}

if v.ProviderVersion != "" {
v.Args = append(v.Args, "--provider_app_version", v.ProviderVersion)
}

if v.PublishVerificationResults {
v.Args = append(v.Args, "--publish_verification_results", "true")
}

return nil
}

0 comments on commit 6b2e2c5

Please sign in to comment.