Skip to content

Commit

Permalink
feat: add parameters to write pact file and execute integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PingWriter committed Nov 28, 2024
1 parent a1e63fc commit a6fe5bf
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions consumer/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ type MockHTTPProviderConfig struct {
// TLS enables a mock service behind a self-signed certificate
// TODO: document and test this
TLS bool

// NotWritePactFile doesn't write the pact file at the end of the provider test.
NotWritePactFile bool

// NotExecuteIntegrationTest doesn't execute integration test in the provider test.
NotExecuteIntegrationTest bool
}

// httpMockProvider is the entrypoint for http consumer tests
Expand Down Expand Up @@ -129,7 +135,9 @@ func (p *httpMockProvider) configure() error {
// Will cleanup interactions between tests within a suite
// and write the pact file if successful
func (p *httpMockProvider) ExecuteTest(t *testing.T, integrationTest func(MockServerConfig) error) error {
log.Println("[DEBUG] pact verify")
if !p.config.NotExecuteIntegrationTest {
log.Println("[DEBUG] pact verify")
}

var err error
if p.config.AllowedMockServerPorts != "" && p.config.Port <= 0 {
Expand All @@ -148,30 +156,36 @@ func (p *httpMockProvider) ExecuteTest(t *testing.T, integrationTest func(MockSe
return err
}

// Run the integration test
err = integrationTest(MockServerConfig{
Port: p.config.Port,
Host: p.config.Host,
TLSConfig: GetTLSConfigForTLSMockServer(),
})
if !p.config.NotExecuteIntegrationTest {
// Run the integration test
err = integrationTest(MockServerConfig{
Port: p.config.Port,
Host: p.config.Host,
TLSConfig: GetTLSConfigForTLSMockServer(),
})

res, mismatches := p.mockserver.Verify(p.config.Port, p.config.PactDir)
p.displayMismatches(t, mismatches)
res, mismatches := p.mockserver.Verify(p.config.Port, p.config.PactDir)
p.displayMismatches(t, mismatches)

if err != nil {
return err
}
if err != nil {
return err
}

if !res {
return fmt.Errorf("pact validation failed: %+v %+v", res, mismatches)
}
if !res {
return fmt.Errorf("pact validation failed: %+v %+v", res, mismatches)
}

if len(mismatches) > 0 {
return fmt.Errorf("pact validation failed: %+v", mismatches)
if len(mismatches) > 0 {
return fmt.Errorf("pact validation failed: %+v", mismatches)
}
}

p.mockserver.CleanupPlugins()

if p.config.NotWritePactFile {
return nil
}

return p.writePact()
}

Expand Down

0 comments on commit a6fe5bf

Please sign in to comment.