Skip to content

Commit

Permalink
fix: verifier works multi-protocol
Browse files Browse the repository at this point in the history
- update tests to to use NewVerifier()
- refactor Verifier internals to start a dummy
  mock server for message pacts, if no HTTP server given
- Plugins, HTTP and Messages all supported from same verifier
  • Loading branch information
mefellows committed Nov 23, 2022
1 parent 9fce93a commit 3c4c56f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ env:
APP_REF: ${{ github.ref }}
LD_LIBRARY_PATH: /tmp
PACT_GO_LIB_DOWNLOAD_PATH: /tmp
LOG_LEVEL: trace
LOG_LEVEL: error
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
Expand Down
4 changes: 2 additions & 2 deletions examples/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestV3HTTPProvider(t *testing.T) {
// Start provider API in the background
go startServer()

verifier := provider.Verifier{}
verifier := provider.NewVerifier()

// Authorization middleware
// This is your chance to modify the request before it hits your provider
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestV3MessageProvider(t *testing.T) {
log.SetLogLevel("TRACE")
var user *User

verifier := provider.Verifier{}
verifier := provider.NewVerifier()

// Map test descriptions to message producer (handlers)
functionMappings := message.Handlers{
Expand Down
2 changes: 1 addition & 1 deletion installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ const (
var packages = map[string]packageInfo{
FFIPackage: {
libName: "libpact_ffi",
version: "0.3.13",
version: "0.3.14",
semverRange: ">= 0.3.0, < 1.0.0",
},
}
Expand Down
3 changes: 3 additions & 0 deletions message/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,15 @@ func CreateMessageHandler(messageHandlers Handlers) proxy.Middleware {
log.Printf("[TRACE] message verification handler received request: %+s, %s", body, r.URL.Path)

if err != nil {
log.Printf("[ERROR] unable to parse message verification request: %s", err)
w.WriteHeader(http.StatusBadRequest)
return
}

err = json.Unmarshal(body, &message)

if err != nil {
log.Printf("[ERROR] unable to parse message verification request: %s", err)
w.WriteHeader(http.StatusBadRequest)
return
}
Expand All @@ -238,6 +240,7 @@ func CreateMessageHandler(messageHandlers Handlers) proxy.Middleware {
res, metadata, handlerErr := f(message.States)

if handlerErr != nil {
log.Printf("[ERROR] error executive message handler %s", err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
Expand Down
42 changes: 13 additions & 29 deletions provider/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pact-foundation/pact-go/v2/message"
"github.com/pact-foundation/pact-go/v2/models"
"github.com/pact-foundation/pact-go/v2/proxy"
"github.com/pact-foundation/pact-go/v2/utils"
)

// Verifier is used to verify the provider side of an HTTP API contract
Expand Down Expand Up @@ -74,44 +75,27 @@ func (v *Verifier) verifyProviderRaw(request VerifyRequest, writer outputWriter)
return err
}

// Check if a provider has been given. If none, start a dummy service to attach the proxy to
if request.ProviderBaseURL == "" {
log.Println("[DEBUG] setting up a dummy server for verification, as none was provided")
port, err := utils.GetFreePort()
if err != nil {
log.Panic("unable to allocate a port for verification:", err)
}
go v.startDefaultHTTPServer(port)

request.ProviderBaseURL = fmt.Sprintf("http://localhost:%d", port)
}

err = request.validate(v.handle)
if err != nil {
return err
}

// Get provider address

// t, err := request.findTransportByScheme("http")
// log.Println("[DEBUG] findTransportByScheme", t)
// if err != nil {
// return err
// }

// if t != nil {
u, err = url.Parse(request.ProviderBaseURL)
if err != nil {
log.Panic("unable to parse the provider URL", err)
}
// } else {
// TODO: don't actually need to do this? I think we can just attach to the proxy itself
// port, err := utils.GetFreePort()
// log.Println("[DEBUG] free port", port)
// if err != nil {
// log.Panic("unable to allocate a port for verification:", err)
// }

// u = url.URL{
// Host: fmt.Sprintf("%s:%d", v.Hostname, port),
// Scheme: "http",
// }

// log.Println("[DEBUG] starting local http server on url", u)
// go v.startDefaultHTTPServer(port)
// }

if err != nil {
return err
}

m := []proxy.Middleware{}

Expand Down
30 changes: 6 additions & 24 deletions provider/verify_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ func (v *VerifyRequest) validate(handle *native.Verifier) error {

// TODO: review this
// We spin up our own transport for messages, but we need them defined for all others
// if len(v.Transports) == 0 && len(v.MessageHandlers) == 0 {
// return fmt.Errorf("one of 'Transports' or 'ProviderBaseURL' must be provided")
// }
if len(v.Transports) == 0 && len(v.MessageHandlers) == 0 && v.ProviderBaseURL == "" {
return fmt.Errorf("one of 'Transports' or 'ProviderBaseURL' must be provided")
}

filterDescription := valueOrFromEnvironment(v.FilterDescription, "PACT_DESCRIPTION")
filterState := valueOrFromEnvironment(v.FilterState, "PACT_PROVIDER_STATE")
Expand All @@ -179,11 +179,6 @@ func (v *VerifyRequest) validate(handle *native.Verifier) error {
handle.SetFilterInfo(filterDescription, filterState, filterNoState)
}

// TODO: this also needs to take the proxyp address
if v.ProviderStatesSetupURL != "" {
handle.SetProviderState(v.ProviderStatesSetupURL, true, true)
}

// TODO: support these
// SetVerificationOptions: 4,

Expand Down Expand Up @@ -285,31 +280,18 @@ func (v *VerifyRequest) Verify(handle *native.Verifier, writer outputWriter) err
for _, transport := range v.Transports {
log.Println("[DEBUG] adding transport to verification", transport)
handle.AddTransport(transport.Protocol, transport.Port, transport.Path, transport.Scheme)
// handle.AddTransport(transport.Protocol, transport.Port, "/", "http")
}

// address := getHost(v.ProviderBaseURL)
// port := getPort(v.ProviderBaseURL)

// WaitForPort(port, "tcp", address, 10*time.Second,
// fmt.Sprintf(`Timed out waiting for Provider API to start on port %d - are you sure it's running?`, port))
if v.ProviderStatesSetupURL != "" {
handle.SetProviderState(v.ProviderStatesSetupURL, true, true)
}

res := handle.Execute()
defer handle.Shutdown()

return res
}

func (v *VerifyRequest) findTransportByScheme(scheme string) (*Transport, error) {
for _, transport := range v.Transports {
if strings.Index(strings.ToLower(transport.Scheme), scheme) == 0 {
return &transport, nil
}
}

return nil, nil
}

// Get a port given a URL
func getPort(rawURL string) int {
parsedURL, err := url.Parse(rawURL)
Expand Down

0 comments on commit 3c4c56f

Please sign in to comment.