-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmocks.go
137 lines (109 loc) · 4.14 KB
/
mocks.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
/*
Copyright 2019 The Jetstack cert-manager contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"net/http"
"strings"
"testing"
"github.com/jarcoal/httpmock"
"github.com/softlayer/softlayer-go/datatypes"
"github.com/stretchr/testify/assert"
)
var (
domains map[int]datatypes.Dns_Domain
)
func init() {
domains = map[int]datatypes.Dns_Domain{}
domainID := 123123
domainName := "example.com"
domains[domainID] = datatypes.Dns_Domain{Id: &domainID, Name: &domainName}
domainIDNew := 321321
domainNameNew := "example.net"
domains[domainIDNew] = datatypes.Dns_Domain{Id: &domainIDNew, Name: &domainNameNew}
}
func parseFilter(filterString string) (map[string]map[string]map[string]string, error) {
var filter map[string]map[string]map[string]string
if filterString == "" {
return filter, nil
}
err := json.Unmarshal([]byte(filterString), &filter)
if err != nil {
return nil, err
}
return filter, nil
}
func registerMocks(t *testing.T) {
httpmock.RegisterResponder("GET", "https://api.softlayer.com/rest/v3/SoftLayer_Account/getDomains.json",
func(req *http.Request) (*http.Response, error) {
filter, err := parseFilter(req.URL.Query().Get("objectFilter"))
if err != nil {
return nil, err
}
query := filter["domains"]["name"]["operation"]
for _, domain := range domains {
if strings.TrimRight(query, ".") == *domain.Name {
return httpmock.NewJsonResponse(200, []datatypes.Dns_Domain{domain})
}
}
return httpmock.NewJsonResponse(404, nil)
},
)
httpmock.RegisterResponder("GET", `=~^https://api\.softlayer\.com/rest/v3/SoftLayer_Dns_Domain/(\d+)/getResourceRecords.json`,
func(req *http.Request) (*http.Response, error) {
filter, err := parseFilter(req.URL.Query().Get("objectFilter"))
if err != nil {
return nil, err
}
queryHost := filter["resourceRecords"]["host"]["operation"]
queryType := filter["resourceRecords"]["type"]["operation"]
id := httpmock.MustGetSubmatchAsUint(req, 1)
recordHost := "test"
recordData := "123"
recordType := "a"
aRecord := datatypes.Dns_Domain_ResourceRecord{Type: &recordType, Host: &recordHost, Data: &recordData}
if queryHost == "" && queryType == "" {
return httpmock.NewJsonResponse(200, []datatypes.Dns_Domain_ResourceRecord{aRecord})
}
if id == 123123 {
return httpmock.NewJsonResponse(200, []datatypes.Dns_Domain_ResourceRecord{})
}
if id == 321321 {
recordHost := "_acme-challenge"
recordData := "123"
recordType := "txt"
aRecord := datatypes.Dns_Domain_ResourceRecord{Type: &recordType, Host: &recordHost, Data: &recordData}
return httpmock.NewJsonResponse(200, []datatypes.Dns_Domain_ResourceRecord{aRecord})
}
return httpmock.NewJsonResponse(404, nil)
},
)
httpmock.RegisterResponder("POST", `=~^https://api\.softlayer\.com/rest/v3/SoftLayer_Dns_Domain/(\d+)/createTxtRecord\.json`,
func(req *http.Request) (*http.Response, error) {
assert.Equal(t, []string{"Basic dW5pdHRlc3Q6dW5pdHRlc3QtdG9rZW4="}, req.Header["Authorization"])
id := httpmock.MustGetSubmatchAsUint(req, 1)
recordData := "123d=="
recordID := int(id)
if val, ok := domains[recordID]; ok {
record := datatypes.Dns_Domain_ResourceRecord_TxtType{Dns_Domain_ResourceRecord: datatypes.Dns_Domain_ResourceRecord{Id: &recordID, Data: &recordData, Host: val.Name}}
return httpmock.NewJsonResponse(200, record)
}
return httpmock.NewJsonResponse(404, nil)
},
)
httpmock.RegisterResponder("POST", "https://api.softlayer.com/rest/v3/SoftLayer_Dns_Domain_ResourceRecord/deleteObjects.json",
func(req *http.Request) (*http.Response, error) {
return httpmock.NewJsonResponse(200, true)
},
)
}