Skip to content

Commit

Permalink
refactor: split HTTPMockProvider into separate v2/3 versions
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Aug 15, 2020
1 parent 07c7fc9 commit b493361
Show file tree
Hide file tree
Showing 16 changed files with 431 additions and 134 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# TODO: bring this back out and remove files from history before merging into master
# *.so
# *.dylib
# Folders
_obj
_test
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pact: install docker

pactv3: clean
@echo "--- 🔨 Running Pact examples"
go test -v -tags=consumer -count=1 github.com/pact-foundation/pact-go/examples/v3/...
go test -v -tags=consumer -count=1 github.com/pact-foundation/pact-go/examples/v3/... -run TestConsumerV2

release:
echo "--- 🚀 Releasing it"
Expand Down Expand Up @@ -82,7 +82,9 @@ updatedeps:
go get -d -v -p 2 ./...

snyk-install:
ifeq (, $(shell which snyk))
npm i -g snyk
endif

snyk:
@if [ "$$TRAVIS_PULL_REQUEST" != "false" ]; then\
Expand Down
104 changes: 65 additions & 39 deletions examples/v3/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,28 @@ import (

type s = v3.String

// Example Pact: How to run me!
// 1. cd <pact-go>/examples
// 2. go test -v -run TestConsumer
func TestConsumer(t *testing.T) {
type User struct {
Name string `json:"name" pact:"example=billy"`
LastName string `json:"lastName" pact:"example=sampson"`
Date string `json:"datetime" pact:"example=20200101,regex=[0-9a-z-A-Z]+"`
}
func TestConsumerV2(t *testing.T) {
v3.SetLogLevel("TRACE")

// Create Pact connecting to local Daemon
mockProvider := &v3.HTTPMockProvider{
mockProvider, err := v3.NewHTTPMockProviderV2(v3.MockHTTPProviderConfigV2{
Consumer: "MyConsumer",
Provider: "MyProvider",
Host: "127.0.0.1",
Port: 8080,
SpecificationVersion: v3.V2,
TLS: true,
}

// Pass in test case
var test = func(config v3.MockServerConfig) error {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: config.TLSConfig,
},
}
req := &http.Request{
Method: "POST",
URL: &url.URL{
Host: fmt.Sprintf("%s:%d", config.Host, config.Port),
Scheme: "https",
Path: "/foobar",
},
Body: ioutil.NopCloser(strings.NewReader(`{"name":"billy"}`)),
Header: make(http.Header),
}
})

// NOTE: by default, request bodies are expected to be sent with a Content-Type
// of application/json. If you don't explicitly set the content-type, you
// will get a mismatch during Verification.
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer 1234")

_, err := client.Do(req)

return err
// TODO: probably better than deferring to the execute test phase, but not sure
if err != nil {
t.Fatal(err)
}

// Set up our expected interactions.
mockProvider.
AddInteraction().
Given("User foo exists").
// Given("User foo exists").
UponReceiving("A request to do a foo").
WithRequest(v3.Request{
Method: "POST",
Expand All @@ -96,3 +64,61 @@ func TestConsumer(t *testing.T) {
log.Fatalf("Error on Verify: %v", err)
}
}

func TestConsumerV3(t *testing.T) {
v3.SetLogLevel("TRACE")

// Create Pact connecting to local Daemon
mockProvider, err := v3.NewHTTPMockProviderV3(v3.MockHTTPProviderConfigV2{
Consumer: "MyConsumer",
Provider: "MyProvider",
Host: "127.0.0.1",
Port: 8080,
SpecificationVersion: v3.V2,
TLS: true,
})

if err != nil {
t.Fatal(err)
}

// Execute pact test
if err := mockProvider.ExecuteTest(test); err != nil {
log.Fatalf("Error on Verify: %v", err)
}
}

type User struct {
Name string `json:"name" pact:"example=billy"`
LastName string `json:"lastName" pact:"example=sampson"`
Date string `json:"datetime" pact:"example=20200101,regex=[0-9a-z-A-Z]+"`
}

// Pass in test case
var test = func(config v3.MockServerConfig) error {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: config.TLSConfig,
},
}
req := &http.Request{
Method: "POST",
URL: &url.URL{
Host: fmt.Sprintf("%s:%d", "localhost", config.Port),
Scheme: "https",
Path: "/foobar",
},
Body: ioutil.NopCloser(strings.NewReader(`{"name":"billy"}`)),
Header: make(http.Header),
}

// NOTE: by default, request bodies are expected to be sent with a Content-Type
// of application/json. If you don't explicitly set the content-type, you
// will get a mismatch during Verification.
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer 1234")

_, err := client.Do(req)

return err
}
63 changes: 63 additions & 0 deletions examples/v3/pacts/consumer-provider.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"consumer": {
"name": "consumer"
},
"interactions": [
{
"description": "A request to do a foo",
"request": {
"body": {
"name": "billy"
},
"headers": {
"Authorization": "Bearer 1234",
"Content-Type": "application/json"
},
"matchingRules": {
"$.body.name": {
"match": "type"
}
},
"method": "POST",
"path": "/foobar"
},
"response": {
"body": {
"dateTime": "2020-01-01",
"lastName": "LastName",
"name": "FirstName"
},
"headers": {
"Content-Type": "application/json"
},
"matchingRules": {
"$.body.dateTime": {
"match": "regex",
"regex": "[0-9\\-]+"
},
"$.body.lastName": {
"match": "type"
},
"$.body.name": {
"match": "type"
}
},
"status": 200
}
}
],
"metadata": {
"pactGo": {
"version": "v1.4.3"
},
"pactRust": {
"version": "0.6.3"
},
"pactSpecification": {
"version": "2.0.0"
}
},
"provider": {
"name": "provider"
}
}
Binary file added libs/libpact_mock_server_ffi.so
Binary file not shown.
Loading

0 comments on commit b493361

Please sign in to comment.