@@ -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.
185140func (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
0 commit comments