Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions consumer/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ type httpMockProvider struct {
specificationVersion models.SpecificationVersion
config MockHTTPProviderConfig
mockserver *native.MockServer

// running is true while the mock server for ExecuteTest is up.
running bool
// logs holds the mock server logs read so far for the most recent
// ExecuteTest.
logs strings.Builder
}

// MockServerConfig stores the address configuration details of the server for the current executing test
Expand Down Expand Up @@ -113,7 +119,9 @@ func (p *httpMockProvider) ExecuteTest(t *testing.T, integrationTest func(MockSe
return errors.New("error: unable to find free port, mock server will fail to start")
}

p.logs.Reset()
p.config.Port, err = p.mockserver.Start(fmt.Sprintf("%s:%d", p.config.Host, p.config.Port), p.config.TLS)
p.running = err == nil
defer p.reset()
if err != nil {
//nolint:wrapcheck // MockServer.Start returns the internal/native sentinels
Expand Down Expand Up @@ -149,6 +157,17 @@ func (p *httpMockProvider) ExecuteTest(t *testing.T, integrationTest func(MockSe
return p.writePact()
}

// MockServerLogs returns the logs pact_ffi wrote for the mock server of the
// most recent ExecuteTest, at the configured log level. It can be called from
// within the integration test, or once ExecuteTest has returned.
func (p *httpMockProvider) MockServerLogs() string {
if p.running {
p.logs.WriteString(p.mockserver.MockServerLogs(p.config.Port))
}

return p.logs.String()
}

// configure applies the defaults for any unset consumer test configuration.
func (p *httpMockProvider) configure() {
log.Println("[DEBUG] pact setup")
Expand Down Expand Up @@ -185,6 +204,10 @@ func (p *httpMockProvider) configure() {

// Clear state between tests.
func (p *httpMockProvider) reset() {
if p.running {
p.logs.WriteString(p.mockserver.MockServerLogs(p.config.Port))
p.running = false
}
p.mockserver.CleanupMockServer(p.config.Port)
p.mockserver.CleanupPlugins()
p.config.Port = 0
Expand Down
232 changes: 232 additions & 0 deletions consumer/http_headers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
package consumer

import (
"bufio"
"context"
"fmt"
"net"
"net/http"
"strconv"
"strings"
"testing"

"github.com/pact-foundation/pact-go/v2/matchers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// repeatedHeaderCase is a request header expected with one value per index,
// and the field lines the client sends for it.
type repeatedHeaderCase struct {
name string
header string
expected []matchers.Matcher
lines []string
match bool
// rfc9110Warning, when set, is whether the mock server should log the
// pact-reference#584 deprecation warning for the request.
rfc9110Warning *bool
}

// rfc9110WarningText is part of the deprecation warning pact_ffi logs when
// repeated header values only match when their field lines are compared one
// at a time (pact-reference#584).
const rfc9110WarningText = "RFC 9110 §5.3"

// repeatedHeaderCases covers how the mock server matches repeated request
// headers. RFC 9110 §5.3 treats repeated field lines as one line with the
// values joined by commas, so separate lines and a single comma-joined line
// should match the same expectation.
func repeatedHeaderCases() []repeatedHeaderCase {
linkA := `<https://a.example/x,y>; rel="a"`
linkB := `<https://b.example/>; rel="b"`
noWarning := false

return []repeatedHeaderCase{
{
name: "H1 separate lines",
header: "X-Request",
expected: []matchers.Matcher{S("a"), S("b")},
lines: []string{"a", "b"},
match: true,
},
{
name: "H2 one comma-joined line",
header: "X-Request",
expected: []matchers.Matcher{S("a"), S("b")},
lines: []string{"a, b"},
match: true,
},
{
name: "H3 values containing commas on separate lines",
header: "X-Sunset",
expected: []matchers.Matcher{S("Sat, 01 Jan 2028 00:00:00 GMT"), S("Sun, 02 Jan 2028 00:00:00 GMT")},
lines: []string{"Sat, 01 Jan 2028 00:00:00 GMT", "Sun, 02 Jan 2028 00:00:00 GMT"},
match: true,
},
{
name: "H4 singleton Content-Type sent as one comma-joined line",
header: "Content-Type",
expected: []matchers.Matcher{S("text/plain"), S("text/html")},
lines: []string{"text/plain, text/html"},
match: false,
},
{
name: "H5 Cache-Control sent as one comma-joined line",
header: "Cache-Control",
expected: []matchers.Matcher{S("no-cache"), S("no-store")},
lines: []string{"no-cache, no-store"},
match: true,
},
{
name: "H6a regex matcher per index",
header: "X-Request",
expected: []matchers.Matcher{Regex("first-1", `^first-\d+$`), Regex("first-2", `^first-\d+$`)},
lines: []string{"first-42", "first-99"},
match: true,
rfc9110Warning: &noWarning,
},
{
// Whitespace inside a quoted-string is literal (RFC 9110 §5.6.4), so
// "x, y" never equals "x , y", line by line or joined, and no
// pact-reference#584 deprecation warning is logged.
name: "H6b quoted value whitespace is literal",
header: "X-Request",
expected: []matchers.Matcher{S(`"x , y"`), S("z")},
lines: []string{`"x, y"`, "z"},
match: false,
rfc9110Warning: &noWarning,
},
{
name: "H8a Link values with a comma in the URI on separate lines",
header: "Link",
expected: []matchers.Matcher{S(linkA), S(linkB)},
lines: []string{linkA, linkB},
match: true,
},
{
name: "H8b Link values with a comma in the URI on one comma-joined line",
header: "Link",
expected: []matchers.Matcher{S(linkA), S(linkB)},
lines: []string{linkA + ", " + linkB},
match: true,
},
}
}

// assertRFC9110Warning checks whether logs hold the pact-reference#584
// deprecation warning, when tt expects one way or the other.
func assertRFC9110Warning(t *testing.T, tt repeatedHeaderCase, logs string) {
t.Helper()
if tt.rfc9110Warning == nil {
return
}
if *tt.rfc9110Warning {
assert.Contains(t, logs, rfc9110WarningText)
} else {
assert.NotContains(t, logs, rfc9110WarningText)
}
}

// sendRawHeaderLines sends GET /headers to the mock server, writing each of
// lines as its own name: value field line. The request is written by hand
// because net/http merges repeated header lines into one before sending.
func sendRawHeaderLines(msc MockServerConfig, name string, lines []string) (int, error) {
address := net.JoinHostPort(msc.Host, strconv.Itoa(msc.Port))
var dialer net.Dialer
conn, err := dialer.DialContext(context.Background(), "tcp", address)
if err != nil {
return 0, fmt.Errorf("connecting to the mock server: %w", err)
}
defer func() { _ = conn.Close() }()

var req strings.Builder
fmt.Fprintf(&req, "GET /headers HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n", address)
for _, line := range lines {
fmt.Fprintf(&req, "%s: %s\r\n", name, line)
}
req.WriteString("\r\n")

_, err = conn.Write([]byte(req.String()))
if err != nil {
return 0, fmt.Errorf("writing the request: %w", err)
}

res, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
return 0, fmt.Errorf("reading the response: %w", err)
}
defer func() { _ = res.Body.Close() }()

return res.StatusCode, nil
}

func TestV3HTTPRepeatedRequestHeaders(t *testing.T) {
for _, tt := range repeatedHeaderCases() {
t.Run(tt.name, func(t *testing.T) {
p, err := NewV3Pact(MockHTTPProviderConfig{
Consumer: "consumer",
Provider: "provider",
PactDir: t.TempDir(),
})
require.NoError(t, err)

var status int
err = p.AddInteraction().
UponReceiving(tt.name).
WithRequest("GET", "/headers", func(b *V3RequestBuilder) {
b.Header(tt.header, tt.expected...)
}).
WillRespondWith(200).
ExecuteTest(t, func(msc MockServerConfig) error {
var sendErr error
status, sendErr = sendRawHeaderLines(msc, tt.header, tt.lines)
return sendErr
})

if tt.match {
require.NoError(t, err)
assert.Equal(t, http.StatusOK, status)
} else {
require.Error(t, err)
assert.NotEqual(t, http.StatusOK, status)
}
assertRFC9110Warning(t, tt, p.MockServerLogs())
})
}
}

func TestV4HTTPRepeatedRequestHeaders(t *testing.T) {
for _, tt := range repeatedHeaderCases() {
t.Run(tt.name, func(t *testing.T) {
p, err := NewV4Pact(MockHTTPProviderConfig{
Consumer: "consumer",
Provider: "provider",
PactDir: t.TempDir(),
})
require.NoError(t, err)

var status int
err = p.AddInteraction().
UponReceiving(tt.name).
WithRequest("GET", "/headers", func(b *V4RequestBuilder) {
b.Header(tt.header, tt.expected...)
}).
WillRespondWith(200).
ExecuteTest(t, func(msc MockServerConfig) error {
var sendErr error
status, sendErr = sendRawHeaderLines(msc, tt.header, tt.lines)
return sendErr
})

if tt.match {
require.NoError(t, err)
assert.Equal(t, http.StatusOK, status)
} else {
require.Error(t, err)
assert.NotEqual(t, http.StatusOK, status)
}
assertRFC9110Warning(t, tt, p.MockServerLogs())
})
}
}
8 changes: 4 additions & 4 deletions installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ func checkMusl() bool {
return false
}

// download template structure: "https://github.com/pact-foundation/pact-reference/releases/download/RELEASE-vVERSION/RELEASE-TARGET.ARCHIVE_EXTENSION"
var downloadTemplate = "https://github.com/pact-foundation/pact-reference/releases/download/%s-v%s/%s-%s.%s"
// download template structure: "https://github.com/YOU54F/pact-reference/releases/download/RELEASE-vVERSION/RELEASE-TARGET.ARCHIVE_EXTENSION"
var downloadTemplate = "https://github.com/YOU54F/pact-reference/releases/download/%s-v%s/%s-%s.%s"

var supportedOSes = map[string]string{
"darwin": macos,
Expand Down Expand Up @@ -435,14 +435,14 @@ var packages = map[string]packageInfo{
FFIPackage: {
libName: ffiLibBaseName,
releaseName: "pact_ffi",
version: "0.5.12",
version: "0.5.13-rc.1",
// Pin to the shipped FFI minor. The bindings in this release reference
// symbols specific to this libpact_ffi minor, and past minors have both
// added and removed symbols, so a wider range lets a mismatched-but-in-range
// library already on disk pass CheckPackageInstall and skip reinstalling,
// which then fails at link/load time with undefined FFI symbols. Bump this
// in lockstep with version above.
semverRange: ">= 0.5.12, < 0.6.0",
semverRange: ">= 0.5.13-rc.1", // hashicorp go-version rejects a prerelease against "< 0.6.0"
},
}

Expand Down
16 changes: 8 additions & 8 deletions installer/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func TestInstallerDownloader(t *testing.T) {
pkg: FFIPackage,
want: func() string {
if checkMusl() {
return fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-x86_64-unknown-linux-musl.tar.xz", packages[FFIPackage].version)
return fmt.Sprintf("https://github.com/YOU54F/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-x86_64-unknown-linux-musl.tar.xz", packages[FFIPackage].version)
}
return fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-x86_64-unknown-linux-gnu.tar.xz", packages[FFIPackage].version)
return fmt.Sprintf("https://github.com/YOU54F/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-x86_64-unknown-linux-gnu.tar.xz", packages[FFIPackage].version)
}(),
test: Installer{
os: linux,
Expand All @@ -63,9 +63,9 @@ func TestInstallerDownloader(t *testing.T) {
pkg: FFIPackage,
want: func() string {
if checkMusl() {
return fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-aarch64-unknown-linux-musl.tar.xz", packages[FFIPackage].version)
return fmt.Sprintf("https://github.com/YOU54F/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-aarch64-unknown-linux-musl.tar.xz", packages[FFIPackage].version)
}
return fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-aarch64-unknown-linux-gnu.tar.xz", packages[FFIPackage].version)
return fmt.Sprintf("https://github.com/YOU54F/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-aarch64-unknown-linux-gnu.tar.xz", packages[FFIPackage].version)
}(),
test: Installer{
os: linux,
Expand All @@ -75,7 +75,7 @@ func TestInstallerDownloader(t *testing.T) {
{
name: "ffi lib - macos x86_64",
pkg: FFIPackage,
want: fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-x86_64-apple-darwin.tar.xz", packages[FFIPackage].version),
want: fmt.Sprintf("https://github.com/YOU54F/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-x86_64-apple-darwin.tar.xz", packages[FFIPackage].version),
test: Installer{
os: macos,
arch: x86_64,
Expand All @@ -84,7 +84,7 @@ func TestInstallerDownloader(t *testing.T) {
{
name: "ffi lib - macos arm64",
pkg: FFIPackage,
want: fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-aarch64-apple-darwin.tar.xz", packages[FFIPackage].version),
want: fmt.Sprintf("https://github.com/YOU54F/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-aarch64-apple-darwin.tar.xz", packages[FFIPackage].version),
test: Installer{
os: macos,
arch: aarch64,
Expand All @@ -93,7 +93,7 @@ func TestInstallerDownloader(t *testing.T) {
{
name: "ffi lib - windows aarch64",
pkg: FFIPackage,
want: fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-aarch64-pc-windows-msvc.zip", packages[FFIPackage].version),
want: fmt.Sprintf("https://github.com/YOU54F/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-aarch64-pc-windows-msvc.zip", packages[FFIPackage].version),
test: Installer{
os: windows,
arch: aarch64,
Expand All @@ -102,7 +102,7 @@ func TestInstallerDownloader(t *testing.T) {
{
name: "ffi lib - windows x86_64",
pkg: FFIPackage,
want: fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-x86_64-pc-windows-msvc.zip", packages[FFIPackage].version),
want: fmt.Sprintf("https://github.com/YOU54F/pact-reference/releases/download/pact_ffi-v%s/pact_ffi-x86_64-pc-windows-msvc.zip", packages[FFIPackage].version),
test: Installer{
os: windows,
arch: x86_64,
Expand Down
Loading
Loading