-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
mock_server.go
803 lines (653 loc) · 22.9 KB
/
mock_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
package native
/*
#include "pact.h"
*/
import "C"
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"unsafe"
)
type interactionPart int
const (
INTERACTION_PART_REQUEST interactionPart = iota
INTERACTION_PART_RESPONSE
)
const (
RESULT_OK interactionPart = iota
RESULT_FAILED
)
type specificationVersion int
const (
SPECIFICATION_VERSION_UNKNOWN specificationVersion = iota
SPECIFICATION_VERSION_V1
SPECIFICATION_VERSION_V1_1
SPECIFICATION_VERSION_V2
SPECIFICATION_VERSION_V3
SPECIFICATION_VERSION_V4
)
type logLevel int
const (
LOG_LEVEL_OFF logLevel = iota
LOG_LEVEL_ERROR
LOG_LEVEL_WARN
LOG_LEVEL_INFO
LOG_LEVEL_DEBUG
LOG_LEVEL_TRACE
)
var logLevelStringToInt = map[string]logLevel{
"OFF": LOG_LEVEL_OFF,
"ERROR": LOG_LEVEL_ERROR,
"WARN": LOG_LEVEL_WARN,
"INFO": LOG_LEVEL_INFO,
"DEBUG": LOG_LEVEL_DEBUG,
"TRACE": LOG_LEVEL_TRACE,
}
// Pact is a Go representation of the PactHandle struct
type Pact struct {
handle C.PactHandle
}
// Interaction is a Go representation of the InteractionHandle struct
type Interaction struct {
handle C.InteractionHandle
}
// Version returns the current semver FFI interface version
func Version() string {
v := C.pactffi_version()
return C.GoString(v)
}
var loggingInitialised string
// Init initialises the library
func Init(logLevel string) {
log.Println("[DEBUG] initialising native interface")
logLevel = strings.ToUpper(logLevel)
if loggingInitialised != "" {
log.Printf("log level ('%s') cannot be set to '%s' after initialisation\n", loggingInitialised, logLevel)
} else {
l, ok := logLevelStringToInt[logLevel]
if !ok {
l = LOG_LEVEL_INFO
}
log.Printf("[DEBUG] initialised native log level to %s (%d)", logLevel, l)
if os.Getenv("PACT_LOG_PATH") != "" {
log.Println("[DEBUG] initialised native log to log to file:", os.Getenv("PACT_LOG_PATH"))
err := logToFile(os.Getenv("PACT_LOG_PATH"), l)
if err != nil {
log.Println("[ERROR] failed to log to file:", err)
}
} else {
log.Println("[DEBUG] initialised native log to log to stdout")
err := logToStdout(l)
if err != nil {
log.Println("[ERROR] failed to log to stdout:", err)
}
}
}
}
// MockServer is the public interface for managing the HTTP mock server
type MockServer struct {
pact *Pact
messagePact *MessagePact
interactions []*Interaction
}
// NewHTTPPact creates a new HTTP mock server for a given consumer/provider
func NewHTTPPact(consumer string, provider string) *MockServer {
cConsumer := C.CString(consumer)
cProvider := C.CString(provider)
defer free(cConsumer)
defer free(cProvider)
return &MockServer{pact: &Pact{handle: C.pactffi_new_pact(cConsumer, cProvider)}}
}
// Version returns the current semver FFI interface version
func (m *MockServer) Version() string {
return Version()
}
func (m *MockServer) WithSpecificationVersion(version specificationVersion) {
C.pactffi_with_specification(m.pact.handle, C.int(version))
}
// CreateMockServer creates a new Mock Server from a given Pact file.
// Returns the port number it started on or an error if failed
func (m *MockServer) CreateMockServer(pact string, address string, tls bool) (int, error) {
log.Println("[DEBUG] mock server starting on address:", address)
cPact := C.CString(pact)
cAddress := C.CString(address)
defer free(cPact)
defer free(cAddress)
tlsEnabled := false
if tls {
tlsEnabled = true
}
p := C.pactffi_create_mock_server(cPact, cAddress, C.bool(tlsEnabled))
// | Error | Description |
// |-------|-------------|
// | -1 | A null pointer was received |
// | -2 | The pact JSON could not be parsed |
// | -3 | The mock server could not be started |
// | -4 | The method panicked |
// | -5 | The address is not valid |
// | -6 | Could not create the TLS configuration with the self-signed certificate |
port := int(p)
switch port {
case -1:
return 0, ErrInvalidMockServerConfig
case -2:
return 0, ErrInvalidPact
case -3:
return 0, ErrMockServerUnableToStart
case -4:
return 0, ErrMockServerPanic
case -5:
return 0, ErrInvalidAddress
case -6:
return 0, ErrMockServerTLSConfiguration
default:
if port > 0 {
log.Println("[DEBUG] mock server running on port:", port)
return port, nil
}
return port, fmt.Errorf("an unknown error (code: %v) occurred when starting a mock server for the test", port)
}
}
// Verify verifies that all interactions were successful. If not, returns a slice
// of Mismatch-es. Does not write the pact or cleanup server.
func (m *MockServer) Verify(port int, dir string) (bool, []MismatchedRequest) {
mismatches := m.MockServerMismatchedRequests(port)
log.Println("[DEBUG] mock server mismatches:", len(mismatches))
return len(mismatches) == 0, mismatches
}
// MockServerMismatchedRequests returns a JSON object containing any mismatches from
// the last set of interactions.
func (m *MockServer) MockServerMismatchedRequests(port int) []MismatchedRequest {
log.Println("[DEBUG] mock server determining mismatches:", port)
var res []MismatchedRequest
mismatches := C.pactffi_mock_server_mismatches(C.int(port))
// This method can return a nil pointer, in which case, it
// should be considered a failure (or at least, an issue)
// converting it to a string might also do nasty things here!
if mismatches == nil {
log.Println("[WARN] received a null pointer from the native interface, returning empty list of mismatches")
return []MismatchedRequest{}
}
err := json.Unmarshal([]byte(C.GoString(mismatches)), &res)
if err != nil {
log.Println("[ERROR] failed to unmarshal mismatches response, returning empty list of mismatches")
return []MismatchedRequest{}
}
return res
}
// CleanupMockServer frees the memory from the previous mock server.
func (m *MockServer) CleanupMockServer(port int) bool {
if len(m.interactions) == 0 {
return true
}
log.Println("[DEBUG] mock server cleaning up port:", port)
res := C.pactffi_cleanup_mock_server(C.int(port))
return bool(res)
}
// WritePactFile writes the Pact to file.
// TODO: expose overwrite
func (m *MockServer) WritePactFile(port int, dir string) error {
log.Println("[DEBUG] writing pact file for mock server on port:", port, ", dir:", dir)
cDir := C.CString(dir)
defer free(cDir)
// overwritePact := 0
// if overwrite {
// overwritePact = 1
// }
// res := int(C.pactffi_write_pact_file(C.int(port), cDir, C.int(overwritePact)))
res := int(C.pactffi_write_pact_file(C.int(port), cDir, C.bool(false)))
// | Error | Description |
// |-------|-------------|
// | 1 | A general panic was caught |
// | 2 | The pact file was not able to be written |
// | 3 | A mock server with the provided port was not found |
switch res {
case 0:
return nil
case 1:
return ErrMockServerPanic
case 2:
return ErrUnableToWritePactFile
case 3:
return ErrMockServerNotfound
default:
return fmt.Errorf("an unknown error ocurred when writing to pact file")
}
}
// GetTLSConfig returns a tls.Config compatible with the TLS
// mock server
func GetTLSConfig() *tls.Config {
cert := C.pactffi_get_tls_ca_certificate()
defer libRustFree(cert)
goCert := C.GoString(cert)
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM([]byte(goCert))
return &tls.Config{
RootCAs: certPool,
}
}
func free(str *C.char) {
C.free(unsafe.Pointer(str))
}
func libRustFree(str *C.char) {
C.pactffi_free_string(str)
}
// Start starts up the mock HTTP server on the given address:port and TLS config
// https://docs.rs/pact_mock_server_ffi/0.0.7/pact_mock_server_ffi/fn.create_mock_server_for_pact.html
func (m *MockServer) Start(address string, tls bool) (int, error) {
if len(m.interactions) == 0 {
return 0, ErrNoInteractions
}
log.Println("[DEBUG] mock server starting on address:", address)
cAddress := C.CString(address)
defer free(cAddress)
tlsEnabled := false
if tls {
tlsEnabled = true
}
p := C.pactffi_create_mock_server_for_pact(m.pact.handle, cAddress, C.bool(tlsEnabled))
// | Error | Description |
// |-------|-------------|
// | -1 | A null pointer was received |
// | -2 | The pact JSON could not be parsed |
// | -3 | The mock server could not be started |
// | -4 | The method panicked |
// | -5 | The address is not valid |
// | -6 | Could not create the TLS configuration with the self-signed certificate |
port := int(p)
switch port {
case -1:
return 0, ErrInvalidMockServerConfig
case -2:
return 0, ErrInvalidPact
case -3:
return 0, ErrMockServerUnableToStart
case -4:
return 0, ErrMockServerPanic
case -5:
return 0, ErrInvalidAddress
case -6:
return 0, ErrMockServerTLSConfiguration
default:
if port > 0 {
log.Println("[DEBUG] mock server running on port:", port)
return port, nil
}
return port, fmt.Errorf("an unknown error (code: %v) occurred when starting a mock server for the test", port)
}
}
// StartTransport starts up a mock server on the given address:port for the given transport
// https://docs.rs/pact_ffi/latest/pact_ffi/mock_server/fn.pactffi_create_mock_server_for_transport.html
func (m *MockServer) StartTransport(transport string, address string, port int, config map[string][]interface{}) (int, error) {
if len(m.interactions) == 0 {
return 0, ErrNoInteractions
}
log.Println("[DEBUG] mock server starting on address:", address, port)
cAddress := C.CString(address)
defer free(cAddress)
cTransport := C.CString(transport)
defer free(cTransport)
configJson := stringFromInterface(config)
cConfig := C.CString(configJson)
defer free(cConfig)
p := C.pactffi_create_mock_server_for_transport(m.pact.handle, cAddress, C.ushort(port), cTransport, cConfig)
// | Error | Description
// |-------|-------------
// | -1 | An invalid handle was received. Handles should be created with pactffi_new_pact
// | -2 | transport_config is not valid JSON
// | -3 | The mock server could not be started
// | -4 | The method panicked
// | -5 | The address is not valid
msPort := int(p)
switch msPort {
case -1:
return 0, ErrInvalidMockServerConfig
case -2:
return 0, ErrInvalidMockServerConfig
case -3:
return 0, ErrMockServerUnableToStart
case -4:
return 0, ErrMockServerPanic
case -5:
return 0, ErrInvalidAddress
default:
if msPort > 0 {
log.Println("[DEBUG] mock server running on port:", msPort)
return msPort, nil
}
return msPort, fmt.Errorf("an unknown error (code: %v) occurred when starting a mock server for the test", msPort)
}
}
// Sets the additional metadata on the Pact file. Common uses are to add the client library details such as the name and version
func (m *MockServer) WithMetadata(namespace, k, v string) *MockServer {
cNamespace := C.CString(namespace)
defer free(cNamespace)
cName := C.CString(k)
defer free(cName)
cValue := C.CString(v)
defer free(cValue)
C.pactffi_with_pact_metadata(m.pact.handle, cNamespace, cName, cValue)
return m
}
// NewInteraction initialises a new interaction for the current contract
func (m *MockServer) UsingPlugin(pluginName string, pluginVersion string) error {
cPluginName := C.CString(pluginName)
defer free(cPluginName)
cPluginVersion := C.CString(pluginVersion)
defer free(cPluginVersion)
r := C.pactffi_using_plugin(m.pact.handle, cPluginName, cPluginVersion)
InstallSignalHandlers()
// 1 - A general panic was caught.
// 2 - Failed to load the plugin.
// 3 - Pact Handle is not valid.
res := int(r)
switch res {
case 1:
return ErrPluginGenericPanic
case 2:
return ErrPluginFailed
case 3:
return ErrHandleNotFound
default:
if res != 0 {
return fmt.Errorf("an unknown error (code: %v) occurred when adding a plugin for the test. Received error code:", res)
}
}
return nil
}
// NewInteraction initialises a new interaction for the current contract
func (m *MockServer) CleanupPlugins() {
C.pactffi_cleanup_plugins(m.pact.handle)
}
// NewInteraction initialises a new interaction for the current contract
func (m *MockServer) NewInteraction(description string) *Interaction {
cDescription := C.CString(description)
defer free(cDescription)
i := &Interaction{
handle: C.pactffi_new_interaction(m.pact.handle, cDescription),
}
m.interactions = append(m.interactions, i)
return i
}
// NewInteraction initialises a new interaction for the current contract
func (i *Interaction) WithPluginInteractionContents(part interactionPart, contentType string, contents string) error {
cContentType := C.CString(contentType)
defer free(cContentType)
cContents := C.CString(contents)
defer free(cContents)
r := C.pactffi_interaction_contents(i.handle, C.int(part), cContentType, cContents)
// 1 - A general panic was caught.
// 2 - The mock server has already been started.
// 3 - The interaction handle is invalid.
// 4 - The content type is not valid.
// 5 - The contents JSON is not valid JSON.
// 6 - The plugin returned an error.
res := int(r)
switch res {
case 1:
return ErrPluginGenericPanic
case 2:
return ErrPluginMockServerStarted
case 3:
return ErrPluginInteractionHandleInvalid
case 4:
return ErrPluginInvalidContentType
case 5:
return ErrPluginInvalidJson
case 6:
return ErrPluginSpecificError
default:
if res != 0 {
return fmt.Errorf("an unknown error (code: %v) occurred when adding a plugin for the test. Received error code:", res)
}
}
return nil
}
func (i *Interaction) UponReceiving(description string) *Interaction {
cDescription := C.CString(description)
defer free(cDescription)
C.pactffi_upon_receiving(i.handle, cDescription)
return i
}
func (i *Interaction) Given(state string) *Interaction {
cState := C.CString(state)
defer free(cState)
C.pactffi_given(i.handle, cState)
return i
}
func (i *Interaction) GivenWithParameter(state string, params map[string]interface{}) *Interaction {
cState := C.CString(state)
defer free(cState)
for k, v := range params {
cKey := C.CString(k)
defer free(cKey)
param := stringFromInterface(v)
cValue := C.CString(param)
defer free(cValue)
C.pactffi_given_with_param(i.handle, cState, cKey, cValue)
}
return i
}
func (i *Interaction) WithRequest(method string, pathOrMatcher interface{}) *Interaction {
cMethod := C.CString(method)
defer free(cMethod)
path := stringFromInterface(pathOrMatcher)
cPath := C.CString(path)
defer free(cPath)
C.pactffi_with_request(i.handle, cMethod, cPath)
return i
}
func (i *Interaction) WithRequestHeaders(valueOrMatcher map[string][]interface{}) *Interaction {
return i.withHeaders(INTERACTION_PART_REQUEST, valueOrMatcher)
}
func (i *Interaction) WithResponseHeaders(valueOrMatcher map[string][]interface{}) *Interaction {
return i.withHeaders(INTERACTION_PART_RESPONSE, valueOrMatcher)
}
func (i *Interaction) withHeaders(part interactionPart, valueOrMatcher map[string][]interface{}) *Interaction {
for k, v := range valueOrMatcher {
cName := C.CString(k)
defer free(cName)
for _, header := range v {
value := stringFromInterface(header)
cValue := C.CString(value)
defer free(cValue)
C.pactffi_with_header_v2(i.handle, C.int(part), cName, C.ulong(0), cValue)
}
}
return i
}
func (i *Interaction) WithQuery(valueOrMatcher map[string][]interface{}) *Interaction {
for k, values := range valueOrMatcher {
cName := C.CString(k)
defer free(cName)
for idx, v := range values {
value := stringFromInterface(v)
cValue := C.CString(value)
defer free(cValue)
C.pactffi_with_query_parameter_v2(i.handle, cName, C.ulong(idx), cValue)
}
}
return i
}
func (i *Interaction) WithJSONRequestBody(body interface{}) *Interaction {
return i.withJSONBody(body, INTERACTION_PART_REQUEST)
}
func (i *Interaction) WithJSONResponseBody(body interface{}) *Interaction {
return i.withJSONBody(body, INTERACTION_PART_RESPONSE)
}
func (i *Interaction) withJSONBody(body interface{}, part interactionPart) *Interaction {
cHeader := C.CString("application/json")
defer free(cHeader)
jsonBody := stringFromInterface(body)
cBody := C.CString(jsonBody)
defer free(cBody)
C.pactffi_with_body(i.handle, C.int(part), cHeader, cBody)
return i
}
func (i *Interaction) WithRequestBody(contentType string, body []byte) *Interaction {
return i.withBody(contentType, body, 0)
}
func (i *Interaction) WithResponseBody(contentType string, body []byte) *Interaction {
return i.withBody(contentType, body, 1)
}
func (i *Interaction) withBody(contentType string, body []byte, part interactionPart) *Interaction {
cHeader := C.CString(contentType)
defer free(cHeader)
cBody := C.CString(string(body))
defer free(cBody)
C.pactffi_with_body(i.handle, C.int(part), cHeader, cBody)
return i
}
func (i *Interaction) withBinaryBody(contentType string, body []byte, part interactionPart) *Interaction {
cHeader := C.CString(contentType)
defer free(cHeader)
C.pactffi_with_binary_file(i.handle, C.int(part), cHeader, (*C.uchar)(unsafe.Pointer(&body[0])), C.ulong(len(body)))
return i
}
func (i *Interaction) WithBinaryRequestBody(body []byte) *Interaction {
return i.withBinaryBody("application/octet-stream", body, INTERACTION_PART_REQUEST)
}
func (i *Interaction) WithBinaryResponseBody(body []byte) *Interaction {
return i.withBinaryBody("application/octet-stream", body, INTERACTION_PART_RESPONSE)
}
func (i *Interaction) WithRequestMultipartFile(contentType string, filename string, mimePartName string) *Interaction {
return i.withMultipartFile(contentType, filename, mimePartName, INTERACTION_PART_REQUEST)
}
func (i *Interaction) WithResponseMultipartFile(contentType string, filename string, mimePartName string) *Interaction {
return i.withMultipartFile(contentType, filename, mimePartName, INTERACTION_PART_RESPONSE)
}
func (i *Interaction) withMultipartFile(contentType string, filename string, mimePartName string, part interactionPart) *Interaction {
cHeader := C.CString(contentType)
defer free(cHeader)
cPartName := C.CString(mimePartName)
defer free(cPartName)
cFilename := C.CString(filename)
defer free(cFilename)
C.pactffi_with_multipart_file(i.handle, C.int(part), cHeader, cFilename, cPartName)
return i
}
// Set the expected HTTTP response status
func (i *Interaction) WithStatus(status int) *Interaction {
C.pactffi_response_status(i.handle, C.ushort(status))
return i
}
type stringLike interface {
String() string
}
func stringFromInterface(obj interface{}) string {
switch t := obj.(type) {
case string:
return t
default:
bytes, err := json.Marshal(obj)
if err != nil {
panic(fmt.Sprintln("unable to marshal body to JSON:", err))
}
return quotedString(string(bytes))
}
}
// This fixes a quirk where certain "matchers" (e.g. matchers.S/String) are
// really just strings. However, whene we JSON encode them they get wrapped in quotes
// and the rust core sees them as plain strings, requiring then the quotes to be matched
func quotedString(s string) string {
if s[0] == '"' && s[len(s)-1] == '"' {
return s[1 : len(s)-1]
}
return s
}
// Experimental logging options
// func LogMessage(pkg, level, message string) {
// cPkg := C.CString(pkg)
// defer free(cPkg)
// cLevel := C.CString(level)
// defer free(cLevel)
// cMessage := C.CString(message)
// defer free(cMessage)
// res := C.pactffi_log_message(cPkg, cLevel, cMessage)
// log.Println("[DEBUG] log_to_buffer res", res)
// }
// func logToBuffer(level logLevel) error {
// res := C.pactffi_log_to_buffer(C.int(level))
// log.Println("[DEBUG] log_to_buffer res", res)
// return logResultToError(int(res))
// }
func logToStdout(level logLevel) error {
res := C.pactffi_log_to_stdout(C.int(level))
log.Println("[DEBUG] log_to_stdout res", res)
return logResultToError(int(res))
}
func logToFile(file string, level logLevel) error {
cFile := C.CString(file)
defer free(cFile)
res := C.pactffi_log_to_file(cFile, C.int(level))
log.Println("[DEBUG] log_to_file res", res)
return logResultToError(int(res))
}
// func getLogBuffer() string {
// buf := C.pactffi_fetch_log_buffer()
// defer free(buf)
// return C.GoString(buf)
// }
func logResultToError(res int) error {
switch res {
case 0:
return nil
case -1:
return ErrCantSetLogger
case -2:
return ErrNoLogger
case -3:
return ErrSpecifierNotUtf8
case -4:
return ErrUnknownSinkType
case -5:
return ErrMissingFilePath
case -6:
return ErrCantOpenSinkToFile
case -7:
return ErrCantConstructSink
default:
return fmt.Errorf("an unknown error ocurred when writing to pact file")
}
}
// Errors
var (
// ErrHandleNotFound indicates the underlying handle was not found, and a logic error in the framework
ErrHandleNotFound = fmt.Errorf("unable to find the native interface handle (this indicates a defect in the framework)")
// ErrMockServerPanic indicates a panic ocurred when invoking the remote Mock Server.
ErrMockServerPanic = fmt.Errorf("a general panic occured when starting/invoking mock service (this indicates a defect in the framework)")
// ErrUnableToWritePactFile indicates an error when writing the pact file to disk.
ErrUnableToWritePactFile = fmt.Errorf("unable to write to file")
// ErrMockServerNotfound indicates the Mock Server could not be found.
ErrMockServerNotfound = fmt.Errorf("unable to find mock server with the given port")
// ErrInvalidMockServerConfig indicates an issue configuring the mock server
ErrInvalidMockServerConfig = fmt.Errorf("configuration for the mock server was invalid and an unknown error occurred (this is most likely a defect in the framework)")
// ErrInvalidPact indicates the pact file provided to the mock server was not a valid pact file
ErrInvalidPact = fmt.Errorf("pact given to mock server is invalid")
// ErrMockServerUnableToStart means the mock server could not be started in the rust library
ErrMockServerUnableToStart = fmt.Errorf("unable to start the mock server")
// ErrInvalidAddress means the address provided to the mock server was invalid and could not be understood
ErrInvalidAddress = fmt.Errorf("invalid address provided to the mock server")
// ErrMockServerTLSConfiguration indicates a TLS mock server could not be started
// and is likely a framework level problem
ErrMockServerTLSConfiguration = fmt.Errorf("a tls mock server could not be started (this is likely a defect in the framework)")
// ErrNoInteractions indicates no Interactions have been registered to a mock server, and cannot be started/stopped until at least one is added
ErrNoInteractions = fmt.Errorf("no interactions have been registered for the mock server")
// ErrPluginFailed indicates the plugin could not be started
ErrPluginFailed = fmt.Errorf("the plugin could not be started")
)
// Log Errors
var (
ErrCantSetLogger = fmt.Errorf("can't set logger (applying the logger failed, perhaps because one is applied already).")
ErrNoLogger = fmt.Errorf("no logger has been initialized (call `logger_init` before any other log function).")
ErrSpecifierNotUtf8 = fmt.Errorf("The sink specifier was not UTF-8 encoded.")
ErrUnknownSinkType = fmt.Errorf(`the sink type specified is not a known type (known types: "buffer", "stdout", "stderr", or "file /some/path").`)
ErrMissingFilePath = fmt.Errorf("no file path was specified in a file-type sink specification.")
ErrCantOpenSinkToFile = fmt.Errorf("opening a sink to the specified file path failed (check permissions).")
ErrCantConstructSink = fmt.Errorf("can't construct the log sink")
)