-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathwiremock_test.go
96 lines (87 loc) · 2.24 KB
/
wiremock_test.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
package gosnowflake
import (
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"testing"
)
var wiremock *wiremockClient = newWiremock()
type wiremockClient struct {
protocol string
host string
port int
client http.Client
}
func newWiremock() *wiremockClient {
wmHost := os.Getenv("WIREMOCK_HOST")
if wmHost == "" {
wmHost = "127.0.0.1"
}
wmPortStr := os.Getenv("WIREMOCK_PORT")
if wmPortStr == "" {
wmPortStr = "14355"
}
wmPort, err := strconv.Atoi(wmPortStr)
if err != nil {
panic(fmt.Sprintf("WIREMOCK_PORT is not a number: %v", wmPortStr))
}
wmProtocol := os.Getenv("WIREMOCK_PROTOCOL")
if wmProtocol == "" {
wmProtocol = "http"
}
return &wiremockClient{
protocol: wmProtocol,
host: wmHost,
port: wmPort,
}
}
func (wm *wiremockClient) connectionConfig() *Config {
return &Config{
User: "testUser",
Host: wm.host,
Port: wm.port,
Account: "testAccount",
Protocol: "http",
}
}
type wiremockMapping struct {
filePath string
params map[string]string
}
func (wm *wiremockClient) registerMappings(t *testing.T, mappings ...wiremockMapping) {
for _, mapping := range wm.enrichWithTelemetry(mappings) {
f, err := os.Open("test_data/wiremock/mappings/" + mapping.filePath)
assertNilF(t, err)
defer f.Close()
mappingBodyBytes, err := io.ReadAll(f)
assertNilF(t, err)
mappingBody := string(mappingBodyBytes)
for key, val := range mapping.params {
mappingBody = strings.Replace(mappingBody, key, val, 1)
}
resp, err := wm.client.Post(fmt.Sprintf("%v/import", wm.mappingsURL()), "application/json", strings.NewReader(mappingBody))
assertNilF(t, err)
if resp.StatusCode != http.StatusOK {
respBody, err := io.ReadAll(resp.Body)
assertNilF(t, err)
t.Fatalf("cannot create mapping.\n%v", string(respBody))
}
}
t.Cleanup(func() {
req, err := http.NewRequest("DELETE", wm.mappingsURL(), nil)
assertNilE(t, err)
_, err = wm.client.Do(req)
assertNilE(t, err)
})
}
func (wm *wiremockClient) enrichWithTelemetry(mappings []wiremockMapping) []wiremockMapping {
return append(mappings, wiremockMapping{
filePath: "telemetry.json",
})
}
func (wm *wiremockClient) mappingsURL() string {
return fmt.Sprintf("%v://%v:%v/__admin/mappings", wm.protocol, wm.host, wm.port)
}