Skip to content

Commit

Permalink
wip: add tls support and further tidying up
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Aug 8, 2020
1 parent 46c639c commit 140497d
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 147 deletions.
50 changes: 30 additions & 20 deletions examples/v3/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"testing"

Expand All @@ -29,30 +31,38 @@ func TestConsumer(t *testing.T) {
mockProvider := &v3.MockProvider{
Consumer: "MyConsumer",
Provider: "MyProvider",
Host: "localhost",
Host: "127.0.0.1",
Port: 8080,
LogLevel: "TRACE",
SpecificationVersion: v3.V2,
TLS: true,
}

mockProvider.Setup()
defer mockProvider.Teardown()

// Pass in test case
var test = func(config v3.MockServerConfig) error {
u := fmt.Sprintf("http://localhost:%d/foobar", mockProvider.ServerPort)
req, err := http.NewRequest("GET", u, strings.NewReader(`{"name":"billy"}`))
client := &http.Client{
Transport: v3.GetTLSConfigForTLSMockServer(),
}
req := &http.Request{
Method: "POST",
URL: &url.URL{
Host: fmt.Sprintf("localhost:%d", mockProvider.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")

if err != nil {
return err
}
if _, err = http.DefaultClient.Do(req); err != nil {
return err
}
_, err := client.Do(req)

return err
}
Expand All @@ -61,9 +71,9 @@ func TestConsumer(t *testing.T) {
mockProvider.
AddInteraction().
Given("User foo exists").
UponReceiving("A request to get foo").
UponReceiving("A request to do a foo").
WithRequest(v3.Request{
Method: "GET",
Method: "POST",
Path: s("/foobar"),
Headers: v3.MapMatcher{"Content-Type": s("application/json"), "Authorization": s("Bearer 1234")},
Body: v3.MapMatcher{
Expand All @@ -73,16 +83,16 @@ func TestConsumer(t *testing.T) {
WillRespondWith(v3.Response{
Status: 200,
Headers: v3.MapMatcher{"Content-Type": s("application/json")},
Body: v3.Match(&User{}),
// Body: v3.MapMatcher{
// "dateTime": v3.Regex("2020-01-01", "[0-9\\-]+"),
// "name": s("FirstName"),
// "lastName": s("LastName"),
// },
// Body: v3.Match(&User{}),
Body: v3.MapMatcher{
"dateTime": v3.Regex("2020-01-01", "[0-9\\-]+"),
"name": s("FirstName"),
"lastName": s("LastName"),
},
})

// Verify
if err := mockProvider.Verify(test); err != nil {
// Execute pact test
if err := mockProvider.ExecuteTest(test); err != nil {
log.Fatalf("Error on Verify: %v", err)
}
}
64 changes: 64 additions & 0 deletions examples/v3/pacts/consumer-provider.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"consumer": {
"name": "consumer"
},
"interactions": [
{
"description": "A request to do a foo",
"providerState": "User foo exists",
"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 modified libs/libpact_mock_server_ffi.dylib
Binary file not shown.
2 changes: 1 addition & 1 deletion utils/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// GetFreePort Gets an available port by asking the kernal for a random port
// ready and available for use.
func GetFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
if err != nil {
return 0, err
}
Expand Down
Loading

0 comments on commit 140497d

Please sign in to comment.