Debian Codegraph @

fastly/fastly_test.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "github.com/dnaeon/go-vcr/recorder"
6 "sync"
7 "testing"
8 )
9
10 // testClient is the test client.
11 var testClient = DefaultClient()
12
13 // testStatsClient is the test client for realtime stats.
14 var testStatsClient = NewRealtimeStatsClient()
15
16 // testServiceID is the ID of the testing service.
17 var testServiceID = "7i6HN3TK9wS159v2gPAZ8A"
18
19 // testVersionLock is a lock around version creation because the Fastly API
20 // kinda dies on concurrent requests to create a version.
21 var testVersionLock sync.Mutex
22
23 func record(t *testing.T, fixture string, f func(*Client)) {
24 r, err := recorder.New("fixtures/" + fixture)
25 if err != nil {
26 t.Fatal(err)
27 }
28 defer func() {
29 if err := r.Stop(); err != nil {
30 t.Fatal(err)
31 }
32 }()
33
34 client := DefaultClient()
35 client.HTTPClient.Transport = r
36
37 f(client)
38 }
39
40 func recordRealtimeStats(t *testing.T, fixture string, f func(*RTSClient)) {
41 r, err := recorder.New("fixtures/" + fixture)
42 if err != nil {
43 t.Fatal(err)
44 }
45 defer func() {
46 if err := r.Stop(); err != nil {
47 t.Fatal(err)
48 }
49 }()
50
51 client := NewRealtimeStatsClient()
52 client.client.HTTPClient.Transport = r
53
54 f(client)
55 }
56
57 func createTestService(t *testing.T, serviceFixture string, serviceNameSuffix string) *Service {
58
59 var err error
60 var service *Service
61
62 record(t, serviceFixture, func(client *Client) {
63 service, err = client.CreateService(&CreateServiceInput{
64 Name: fmt.Sprintf("test_service_%s", serviceNameSuffix),
65 Comment: "go-fastly client test",
66 })
67 })
68 if err != nil {
69 t.Fatal(err)
70 }
71
72 return service
73 }
74
75 // testVersion is a new, blank version suitable for testing.
76 func testVersion(t *testing.T, c *Client) *Version {
77 testVersionLock.Lock()
78 defer testVersionLock.Unlock()
79
80 v, err := c.CreateVersion(&CreateVersionInput{
81 Service: testServiceID,
82 })
83 if err != nil {
84 t.Fatal(err)
85 }
86 return v
87 }
88
89 func createTestVersion(t *testing.T, versionFixture string, serviceId string) *Version {
90
91 var err error
92 var version *Version
93
94 record(t, versionFixture, func(client *Client) {
95 testVersionLock.Lock()
96 defer testVersionLock.Unlock()
97
98 version, err = client.CreateVersion(&CreateVersionInput{
99 Service: serviceId,
100 })
101 if err != nil {
102 t.Fatal(err)
103 }
104 })
105
106 return version
107 }
108
109 func createTestDictionary(t *testing.T, dictionaryFixture string, serviceId string, version int, dictionaryNameSuffix string) *Dictionary {
110
111 var err error
112 var dictionary *Dictionary
113
114 record(t, dictionaryFixture, func(client *Client) {
115 dictionary, err = client.CreateDictionary(&CreateDictionaryInput{
116 Service: serviceId,
117 Version: version,
118 Name: fmt.Sprintf("test_dictionary_%s", dictionaryNameSuffix),
119 })
120 })
121 if err != nil {
122 t.Fatal(err)
123 }
124 return dictionary
125 }
126
127 func deleteTestDictionary(t *testing.T, dictionary *Dictionary, deleteFixture string) {
128
129 var err error
130
131 record(t, deleteFixture, func(client *Client) {
132 err = client.DeleteDictionary(&DeleteDictionaryInput{
133 Service: dictionary.ServiceID,
134 Version: dictionary.Version,
135 Name: dictionary.Name,
136 })
137 })
138 if err != nil {
139 t.Fatal(err)
140 }
141 }
142
143 func createTestACL(t *testing.T, createFixture string, serviceId string, version int, aclNameSuffix string) *ACL {
144
145 var err error
146 var acl *ACL
147
148 record(t, createFixture, func(client *Client) {
149 acl, err = client.CreateACL(&CreateACLInput{
150 Service: serviceId,
151 Version: version,
152 Name: fmt.Sprintf("test_acl_%s", aclNameSuffix),
153 })
154 })
155 if err != nil {
156 t.Fatal(err)
157 }
158 return acl
159 }
160
161 func deleteTestACL(t *testing.T, acl *ACL, deleteFixture string) {
162
163 var err error
164
165 record(t, deleteFixture, func(client *Client) {
166 err = client.DeleteACL(&DeleteACLInput{
167 Service: acl.ServiceID,
168 Version: acl.Version,
169 Name: acl.Name,
170 })
171 })
172 if err != nil {
173 t.Fatal(err)
174 }
175 }
176
177 func deleteTestService(t *testing.T, cleanupFixture string, serviceId string){
178
179 var err error
180
181 record(t, cleanupFixture, func(client *Client) {
182 err = client.DeleteService(&DeleteServiceInput{
183 ID: serviceId,
184 })
185 })
186 if err != nil {
187 t.Fatal(err)
188 }
189 }
190
191 // privatekey returns a ASN.1 DER encoded key suitable for testing.
192 func privateKey() string {
193 return `-----BEGIN PRIVATE KEY-----
194 MIICXAIBAAKBgQCukSu6ece/5jlgnNiLK7mIxJuLLeZ1FI/rn0PK8XTO+vSZjV+o
195 vXpiSiXavXv2XhQymAf/tOpDV+uBxIzM3E1wyQ2qUxFjZ1zJHqBlmK+QJeg7pKmD
196 gVQrzdtfUYGTlpizMiucWyYJYHNdfIkoD9N+wSw/cM/kxHa8fYtGsN3M7wIDAQAB
197 AoGAOXiBtPqy0HKzISOCBw92HZjcvI13+cOzPhdI8l9b3WixbnwkqiD3UbSnkcQg
198 M5P1glKbD4w4M8OWPTrAQBGnTJa2iA7z9IqTGL7dwhVnQ04NYq14CpPs+XCKwOxK
199 O0gEjgbjlPNyE3OsMNBFMB5rnsVEI8uUukBmm/h6l8x7HUECQQDCeatYbfZZ8ra3
200 twTCmuc4qEXMzLhxBq7Ogyst90mC1fhfxFKiXwu0WVJqeGSFrK1upHPDfPJ3JN75
201 CDskG9YzAkEA5csoxhmPblUOG3e/Vt8dzjPlk/ZTgxEOKPb86BUprsR2J1aJVHVH
202 EoZjlAj5yo7iNSphp4cVXJd8I+ZsYSeaVQJBAJIF+5N9lcG6Tlop0SgyWbWgHDEH
203 8uHjS7SCpxRvnsHf2gxGhGmpBkfX3dtWJNx+aQcv8kBx/Dlb9RR2irm1MSMCQCmM
204 xICdWovuoTBiRJymlzMTuy032v3V9aN+lVg5i2HocBzIzugQlJtK5XJ89P2lPE20
205 rhemmzw0v+OV5H7ktEkCQDqLUZyqnGHX6qV+8eJMafLyy1AUfzSkbuZ/nX6hed8T
206 cfzsfBxi4bN4JOkJcA77FpXDecX/GDwzRN+yfwNs3+0=
207 -----END PRIVATE KEY-----
208 `
209 }