Skip to content

Commit bd98833

Browse files
committed
!fix(deps): pact-ffi-0.5.5 / removes some interfaces
CreateMockServer (for pact), use StartTransport preferred of Start Verifier.Verify (via args+), use VerifyProvider
1 parent f5d3b0c commit bd98833

4 files changed

Lines changed: 220 additions & 365 deletions

File tree

internal/native/mock_server.go

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"log"
1414
"os"
15+
"strconv"
1516
"strings"
1617
"unsafe"
1718
)
@@ -134,52 +135,6 @@ func (m *MockServer) WithSpecificationVersion(version specificationVersion) {
134135
C.pactffi_with_specification(m.pact.handle, C.int(version))
135136
}
136137

137-
// CreateMockServer creates a new Mock Server from a given Pact file.
138-
// Returns the port number it started on or an error if failed
139-
func (m *MockServer) CreateMockServer(pact string, address string, tls bool) (int, error) {
140-
log.Println("[DEBUG] mock server starting on address:", address)
141-
cPact := C.CString(pact)
142-
cAddress := C.CString(address)
143-
defer free(cPact)
144-
defer free(cAddress)
145-
tlsEnabled := false
146-
if tls {
147-
tlsEnabled = true
148-
}
149-
150-
p := C.pactffi_create_mock_server(cPact, cAddress, C.bool(tlsEnabled))
151-
152-
// | Error | Description |
153-
// |-------|-------------|
154-
// | -1 | A null pointer was received |
155-
// | -2 | The pact JSON could not be parsed |
156-
// | -3 | The mock server could not be started |
157-
// | -4 | The method panicked |
158-
// | -5 | The address is not valid |
159-
// | -6 | Could not create the TLS configuration with the self-signed certificate |
160-
port := int(p)
161-
switch port {
162-
case -1:
163-
return 0, ErrInvalidMockServerConfig
164-
case -2:
165-
return 0, ErrInvalidPact
166-
case -3:
167-
return 0, ErrMockServerUnableToStart
168-
case -4:
169-
return 0, ErrMockServerPanic
170-
case -5:
171-
return 0, ErrInvalidAddress
172-
case -6:
173-
return 0, ErrMockServerTLSConfiguration
174-
default:
175-
if port > 0 {
176-
log.Println("[DEBUG] mock server running on port:", port)
177-
return port, nil
178-
}
179-
return port, fmt.Errorf("an unknown error (code: %v) occurred when starting a mock server for the test", port)
180-
}
181-
}
182-
183138
// Verify verifies that all interactions were successful. If not, returns a slice
184139
// of Mismatch-es. Does not write the pact or cleanup server.
185140
func (m *MockServer) Verify(port int, dir string) (bool, []MismatchedRequest) {
@@ -288,14 +243,45 @@ func (m *MockServer) Start(address string, tls bool) (int, error) {
288243
}
289244

290245
log.Println("[DEBUG] mock server starting on address:", address)
291-
cAddress := C.CString(address)
292-
defer free(cAddress)
293-
tlsEnabled := false
294-
if tls {
295-
tlsEnabled = true
296-
}
297246

298-
p := C.pactffi_create_mock_server_for_pact(m.pact.handle, cAddress, C.bool(tlsEnabled))
247+
// Split address into host and port, handling URLs with schemes
248+
addr := address
249+
if idx := strings.Index(address, "://"); idx != -1 {
250+
addr = address[idx+3:]
251+
}
252+
if idx := strings.Index(addr, "/"); idx != -1 {
253+
addr = addr[:idx]
254+
}
255+
256+
host := addr
257+
port := 0
258+
if idx := strings.LastIndex(addr, ":"); idx != -1 {
259+
potentialPort := addr[idx+1:]
260+
if potentialPort != "" {
261+
if p, err := strconv.Atoi(potentialPort); err == nil {
262+
host = addr[:idx]
263+
port = p
264+
}
265+
}
266+
}
267+
log.Println("[DEBUG] parsed host:", host, "port:", port)
268+
cAddress := C.CString(host)
269+
defer free(cAddress)
270+
271+
cTransport := C.CString("http")
272+
defer free(cTransport)
273+
274+
// TODO: I dont think this is correct
275+
var configJSON string
276+
if tls {
277+
configJSON = `{"tls":true}`
278+
} else {
279+
configJSON = `{}`
280+
}
281+
cConfig := C.CString(configJSON)
282+
defer free(cConfig)
283+
284+
msPort := int(C.pactffi_create_mock_server_for_transport(m.pact.handle, cAddress, C.ushort(port), cTransport, cConfig))
299285

300286
// | Error | Description |
301287
// |-------|-------------|
@@ -305,8 +291,7 @@ func (m *MockServer) Start(address string, tls bool) (int, error) {
305291
// | -4 | The method panicked |
306292
// | -5 | The address is not valid |
307293
// | -6 | Could not create the TLS configuration with the self-signed certificate |
308-
port := int(p)
309-
switch port {
294+
switch msPort {
310295
case -1:
311296
return 0, ErrInvalidMockServerConfig
312297
case -2:
@@ -320,12 +305,12 @@ func (m *MockServer) Start(address string, tls bool) (int, error) {
320305
case -6:
321306
return 0, ErrMockServerTLSConfiguration
322307
default:
323-
if port > 0 {
324-
log.Println("[DEBUG] mock server running on port:", port)
325-
return port, nil
326-
}
327-
return port, fmt.Errorf("an unknown error (code: %v) occurred when starting a mock server for the test", port)
328-
}
308+
if msPort > 0 {
309+
log.Println("[DEBUG] mock server running on port:", msPort)
310+
return msPort, nil
311+
}
312+
return msPort, fmt.Errorf("an unknown error (code: %v) occurred when starting a mock server for the test", msPort)
313+
}
329314
}
330315

331316
// StartTransport starts up a mock server on the given address:port for the given transport

internal/native/mock_server_test.go

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -17,105 +17,6 @@ func init() {
1717
Init("")
1818
}
1919

20-
func TestMockServer_CreateAndCleanupMockServer(t *testing.T) {
21-
m := MockServer{}
22-
port, _ := m.CreateMockServer(pactComplex, "0.0.0.0:0", false)
23-
defer m.CleanupMockServer(port)
24-
25-
if port <= 0 {
26-
t.Fatal("want port > 0, got", port)
27-
}
28-
}
29-
30-
func TestMockServer_MismatchesSuccess(t *testing.T) {
31-
m := MockServer{}
32-
port, _ := m.CreateMockServer(pactSimple, "0.0.0.0:0", false)
33-
defer m.CleanupMockServer(port)
34-
35-
res, err := http.Get(fmt.Sprintf("http://localhost:%d/foobar", port))
36-
if err != nil {
37-
t.Fatalf("Error sending request: %v", err)
38-
}
39-
40-
if res.StatusCode != 200 {
41-
t.Fatalf("want '200', got '%d'", res.StatusCode)
42-
}
43-
44-
mismatches := m.MockServerMismatchedRequests(port)
45-
if len(mismatches) != 0 {
46-
t.Fatalf("want 0 mismatches, got '%d'", len(mismatches))
47-
}
48-
}
49-
50-
func TestMockServer_MismatchesFail(t *testing.T) {
51-
m := MockServer{}
52-
port, _ := m.CreateMockServer(pactSimple, "0.0.0.0:0", false)
53-
defer m.CleanupMockServer(port)
54-
55-
mismatches := m.MockServerMismatchedRequests(port)
56-
if len(mismatches) != 1 {
57-
t.Fatalf("want 1 mismatch, got '%d'", len(mismatches))
58-
}
59-
}
60-
61-
func TestMockServer_VerifySuccess(t *testing.T) {
62-
tmpPactFolder, err := os.MkdirTemp("", "pact-go")
63-
assert.NoError(t, err)
64-
65-
m := MockServer{}
66-
port, _ := m.CreateMockServer(pactSimple, "0.0.0.0:0", false)
67-
defer m.CleanupMockServer(port)
68-
69-
_, err = http.Get(fmt.Sprintf("http://localhost:%d/foobar", port))
70-
if err != nil {
71-
t.Fatalf("Error sending request: %v", err)
72-
}
73-
74-
success, mismatches := m.Verify(port, tmpPactFolder)
75-
if !success {
76-
t.Fatalf("want 'true' but got '%v'", success)
77-
}
78-
79-
if len(mismatches) != 0 {
80-
t.Fatalf("want 0 mismatches, got '%d'", len(mismatches))
81-
}
82-
}
83-
84-
func TestMockServer_VerifyFail(t *testing.T) {
85-
tmpPactFolder, err := os.MkdirTemp("", "pact-go")
86-
assert.NoError(t, err)
87-
m := MockServer{}
88-
port, _ := m.CreateMockServer(pactSimple, "0.0.0.0:0", false)
89-
90-
success, mismatches := m.Verify(port, tmpPactFolder)
91-
if success {
92-
t.Fatalf("want 'false' but got '%v'", success)
93-
}
94-
95-
if len(mismatches) != 1 {
96-
t.Fatalf("want 1 mismatch, got '%d'", len(mismatches))
97-
}
98-
}
99-
100-
func TestMockServer_WritePactfile(t *testing.T) {
101-
tmpPactFolder, err := os.MkdirTemp("", "pact-go")
102-
assert.NoError(t, err)
103-
104-
m := MockServer{}
105-
port, _ := m.CreateMockServer(pactSimple, "0.0.0.0:0", false)
106-
defer m.CleanupMockServer(port)
107-
108-
_, err = http.Get(fmt.Sprintf("http://localhost:%d/foobar", port))
109-
if err != nil {
110-
t.Fatalf("Error sending request: %v", err)
111-
}
112-
err = m.WritePactFile(port, tmpPactFolder)
113-
114-
if err != nil {
115-
t.Fatal("error: ", err)
116-
}
117-
}
118-
11920
func TestMockServer_GetTLSConfig(t *testing.T) {
12021
config := GetTLSConfig()
12122

@@ -155,7 +56,6 @@ func TestHandleBasedHTTPTests(t *testing.T) {
15556

15657
_, err = http.Get(fmt.Sprintf("http://0.0.0.0:%d/products", port))
15758
assert.NoError(t, err)
158-
15959
mismatches := m.MockServerMismatchedRequests(port)
16060
if len(mismatches) != 0 {
16161
t.Fatalf("want 0 mismatches, got '%d'", len(mismatches))

0 commit comments

Comments
 (0)