-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from YOU54F/step3
chore(deps): Step-3 - Pact V2 Update
- Loading branch information
Showing
113 changed files
with
1,069 additions
and
24,592 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Pact-Workshop(GoLang) | ||
|
||
on: | ||
push: | ||
branches: | ||
- step3 | ||
pull_request: | ||
branches: | ||
- step3 | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
shell: bash | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
- name: Set up Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.21 | ||
- name: install | ||
run: make install | ||
- name: install_pact_ffi_lib | ||
run: make install_pact_ffi_lib | ||
- name: consumer unit tests | ||
run: make unit | ||
- name: consumer pact tests | ||
run: make consumer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
TEST?=./... | ||
|
||
include ./make/config.mk | ||
|
||
install: | ||
@if [ ! -d pact/bin ]; then\ | ||
echo "--- 🛠 Installing Pact CLI dependencies";\ | ||
echo "--- Installing Pact CLI dependencies";\ | ||
curl -fsSL https://raw.githubusercontent.com/pact-foundation/pact-ruby-standalone/master/install.sh | bash;\ | ||
fi | ||
|
||
install_pact_ffi_lib: | ||
go install github.com/pact-foundation/pact-go/[email protected] | ||
sudo mkdir -p /usr/local/lib/ | ||
sudo $$HOME/go/bin/pact-go -l DEBUG install | ||
|
||
run-consumer: | ||
@go run consumer/client/cmd/main.go | ||
|
||
|
@@ -16,10 +19,11 @@ run-provider: | |
|
||
unit: | ||
@echo "--- 🔨Running Unit tests " | ||
go test -tags=unit -count=1 github.com/pact-foundation/pact-workshop-go/consumer/client -run 'TestClientUnit' | ||
go test -tags=unit -count=1 github.com/pact-foundation/pact-workshop-go/consumer/client -run 'TestClientUnit' -v | ||
|
||
consumer: export PACT_TEST := true | ||
consumer: install | ||
@echo "--- 🔨Running Consumer Pact tests " | ||
go test -tags=integration -count=1 github.com/pact-foundation/pact-workshop-go/consumer/client -run 'TestClientPact' | ||
go test -tags=integration -count=1 github.com/pact-foundation/pact-workshop-go/consumer/client -run 'TestClientPact' -v | ||
|
||
.PHONY: install unit consumer provider run-provider run-consumer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,90 @@ | ||
// +build integration | ||
//go:build integration | ||
|
||
package client | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"testing" | ||
|
||
"net/url" | ||
|
||
"github.com/pact-foundation/pact-go/dsl" | ||
"github.com/pact-foundation/pact-go/v2/consumer" | ||
"github.com/pact-foundation/pact-go/v2/log" | ||
"github.com/pact-foundation/pact-go/v2/matchers" | ||
"github.com/pact-foundation/pact-workshop-go/model" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var commonHeaders = dsl.MapMatcher{ | ||
"Content-Type": term("application/json; charset=utf-8", `application\/json`), | ||
"X-Api-Correlation-Id": dsl.Like("100"), | ||
} | ||
var Like = matchers.Like | ||
var EachLike = matchers.EachLike | ||
var Term = matchers.Term | ||
var Regex = matchers.Regex | ||
var HexValue = matchers.HexValue | ||
var Identifier = matchers.Identifier | ||
var IPAddress = matchers.IPAddress | ||
var IPv6Address = matchers.IPv6Address | ||
var Timestamp = matchers.Timestamp | ||
var Date = matchers.Date | ||
var Time = matchers.Time | ||
var UUID = matchers.UUID | ||
var ArrayMinLike = matchers.ArrayMinLike | ||
|
||
type S = matchers.S | ||
type Map = matchers.MapMatcher | ||
|
||
var u *url.URL | ||
var client *Client | ||
|
||
func TestMain(m *testing.M) { | ||
var exitCode int | ||
|
||
// Setup Pact and related test stuff | ||
setup() | ||
|
||
// Run all the tests | ||
exitCode = m.Run() | ||
|
||
// Shutdown the Mock Service and Write pact files to disk | ||
if err := pact.WritePact(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
func TestClientPact_GetUser(t *testing.T) { | ||
|
||
pact.Teardown() | ||
os.Exit(exitCode) | ||
} | ||
log.SetLogLevel("INFO") | ||
mockProvider, err := consumer.NewV2Pact(consumer.MockHTTPProviderConfig{ | ||
Consumer: os.Getenv("CONSUMER_NAME"), | ||
Provider: os.Getenv("PROVIDER_NAME"), | ||
LogDir: os.Getenv("LOG_DIR"), | ||
PactDir: os.Getenv("PACT_DIR"), | ||
}) | ||
assert.NoError(t, err) | ||
|
||
func TestClientPact_GetUser(t *testing.T) { | ||
t.Run("the user exists", func(t *testing.T) { | ||
id := 10 | ||
|
||
pact. | ||
err = mockProvider. | ||
AddInteraction(). | ||
Given("User sally exists"). | ||
UponReceiving("A request to login with user 'sally'"). | ||
WithRequest(request{ | ||
Method: "GET", | ||
Path: term("/users/10", "/users/[0-9]+"), | ||
WithRequestPathMatcher("GET", Regex("/users/"+strconv.Itoa(id), "/users/[0-9]+")). | ||
WillRespondWith(200, func(b *consumer.V2ResponseBuilder) { | ||
b.BodyMatch(model.User{}). | ||
Header("Content-Type", Term("application/json", `application\/json`)). | ||
Header("X-Api-Correlation-Id", Like("100")) | ||
}). | ||
WillRespondWith(dsl.Response{ | ||
Status: 200, | ||
Body: dsl.Match(model.User{}), | ||
Headers: commonHeaders, | ||
}) | ||
ExecuteTest(t, func(config consumer.MockServerConfig) error { | ||
// Act: test our API client behaves correctly | ||
|
||
err := pact.Verify(func() error { | ||
user, err := client.GetUser(id) | ||
// Get the Pact mock server URL | ||
u, _ = url.Parse("http://" + config.Host + ":" + strconv.Itoa(config.Port)) | ||
|
||
// Assert basic fact | ||
if user.ID != id { | ||
return fmt.Errorf("wanted user with ID %d but got %d", id, user.ID) | ||
} | ||
// Initialise the API client and point it at the Pact mock server | ||
client = &Client{ | ||
BaseURL: u, | ||
} | ||
|
||
return err | ||
}) | ||
// // Execute the API client | ||
user, err := client.GetUser(id) | ||
|
||
if err != nil { | ||
t.Fatalf("Error on Verify: %v", err) | ||
} | ||
}) | ||
} | ||
|
||
// Common test data | ||
var pact dsl.Pact | ||
|
||
// Aliases | ||
var term = dsl.Term | ||
|
||
type request = dsl.Request | ||
// // Assert basic fact | ||
if user.ID != id { | ||
return fmt.Errorf("wanted user with ID %d but got %d", id, user.ID) | ||
} | ||
|
||
func setup() { | ||
pact = createPact() | ||
|
||
// Proactively start service to get access to the port | ||
pact.Setup(true) | ||
|
||
u, _ = url.Parse(fmt.Sprintf("http://localhost:%d", pact.Server.Port)) | ||
return err | ||
}) | ||
|
||
client = &Client{ | ||
BaseURL: u, | ||
} | ||
assert.NoError(t, err) | ||
|
||
} | ||
}) | ||
|
||
func createPact() dsl.Pact { | ||
return dsl.Pact{ | ||
Consumer: os.Getenv("CONSUMER_NAME"), | ||
Provider: os.Getenv("PROVIDER_NAME"), | ||
LogDir: os.Getenv("LOG_DIR"), | ||
PactDir: os.Getenv("PACT_DIR"), | ||
LogLevel: "INFO", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.