-
Notifications
You must be signed in to change notification settings - Fork 0
/
apigateway_test.go
207 lines (161 loc) · 6.55 KB
/
apigateway_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
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
package apigateway
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/stretchr/testify/assert"
)
func TestReverseProxy(t *testing.T) {
t.Run("Test passthrough reverse proxy", func(t *testing.T) {
// Backend API
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "backend")
}))
defer backend.Close()
target, err := url.Parse(backend.URL)
assert.Nil(t, err)
proxy := NewPassthroughReverseProxy(target)
// Test gateway
gateway := httptest.NewServer(proxy)
defer gateway.Close()
// Request made for the API gateway should be proxied to backend API endpoint
res, err := http.Get(gateway.URL)
assert.Nil(t, err)
body, err := ioutil.ReadAll(res.Body)
assert.Nil(t, err)
res.Body.Close()
assert.Equal(t, "backend\n", string(body))
})
t.Run("Test response translator reverse proxy", func(t *testing.T) {
orderPayload := "{\"id\":\"3d58fd0c-0f1e-4d75-8961-873508095817\",\"customer\":{\"id\":\"b0f956d2-bc16-4275-8980-3442e97785f7\"},\"product\":{\"sku\": \"IPOD2008GREEN\"},\"quantity\":1}"
// Order API
order := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, orderPayload)
}))
defer order.Close()
mergedPayload := "{\"id\": \"3d58fd0c-0f1e-4d75-8961-873508095817\",\"customer\": {\"id\": \"b0f956d2-bc16-4275-8980-3442e97785f7\",\"name\": \"John Doe\",\"address\": \"123 Main St\",\"city\": \"Anytown\",\"country\": \"Brazil\"},\"product\": {\"sku\": \"IPOD2008GREEN\",\"title\": \"IPod Nano - 8gb\",\"name\": \"IPod Nano - 8gb - green\"},\"quantity\": 1}"
reponseTranslator := func(r *http.Response) error {
body, err := ioutil.ReadAll(r.Body)
assert.Nil(t, err)
r.Body.Close()
assert.Equal(t, orderPayload+"\n", string(body))
r.Body = ioutil.NopCloser(bytes.NewReader([]byte(mergedPayload)))
r.ContentLength = int64(len(mergedPayload))
r.Header.Set("Content-Length", strconv.Itoa(len(mergedPayload)))
return nil
}
orderURL, err := url.Parse(order.URL)
assert.Nil(t, err)
proxy := NewResponseTranslatorReverseProxy(orderURL, reponseTranslator)
// Test gateway
gateway := httptest.NewServer(proxy)
defer gateway.Close()
// Request made for the API gateway should be proxied to backend API endpoint
res, err := http.Get(gateway.URL)
assert.Nil(t, err)
body, err := ioutil.ReadAll(res.Body)
assert.Nil(t, err)
res.Body.Close()
assert.Equal(t, mergedPayload, string(body))
})
t.Run("Test request translator reverse proxy", func(t *testing.T) {
// Product API
product := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
assert.Nil(t, err)
r.Body.Close()
fmt.Fprintln(w, string(body))
}))
defer product.Close()
replacedPayload := "changed"
requestTranslator := func(r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
assert.Nil(t, err)
r.Body.Close()
assert.Equal(t, "original", string(body))
r.Body = ioutil.NopCloser(bytes.NewReader([]byte(replacedPayload)))
r.ContentLength = int64(len(replacedPayload))
r.Header.Set("Content-Length", strconv.Itoa(len(replacedPayload)))
}
productURL, err := url.Parse(product.URL)
assert.Nil(t, err)
proxy := NewRequestTranslatorReverseProxy(productURL, requestTranslator)
// Test gateway
gateway := httptest.NewServer(proxy)
defer gateway.Close()
// Request made for the API gateway should be changed and proxied to backend API endpoint
res, err := http.Post(gateway.URL, "application/json", bytes.NewReader([]byte("original")))
assert.Nil(t, err)
body, err := ioutil.ReadAll(res.Body)
assert.Nil(t, err)
res.Body.Close()
assert.Equal(t, replacedPayload+"\n", string(body))
})
t.Run("Test SNS passthrough reverse proxy", func(t *testing.T) {
var snsRequest string
endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
r.Body.Close()
snsRequest = string(body)
w.WriteHeader(http.StatusOK)
}))
defer endpoint.Close()
topicArn := "arn:aws:sns::000000000000:api_gateway_topic"
c := aws.NewConfig()
c.WithRegion("us-east-1")
c.WithCredentials(credentials.NewStaticCredentials("id", "key", ""))
c.WithDisableSSL(true)
c.WithEndpoint(endpoint.URL)
proxy := NewSNSPassthroughReverseProxy(topicArn, c)
// Test gateway
gateway := httptest.NewServer(proxy)
defer gateway.Close()
// Request made for the API gateway should be proxied to SNS
res, err := http.Post(gateway.URL, "application/json", bytes.NewReader([]byte("test_sns_sqs")))
assert.Nil(t, err)
assert.Equal(t, http.StatusAccepted, res.StatusCode)
assert.Equal(t, snsRequest, "Action=Publish&Message=%7B%22default%22%3A%22test_sns_sqs%22%7D&MessageStructure=json&TopicArn=arn%3Aaws%3Asns%3A%3A000000000000%3Aapi_gateway_topic&Version=2010-03-31")
})
t.Run("Test SNS request translator reverse proxy", func(t *testing.T) {
var snsRequest string
endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
r.Body.Close()
snsRequest = string(body)
w.WriteHeader(http.StatusOK)
}))
defer endpoint.Close()
requestTranslator := func(r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
assert.Nil(t, err)
r.Body.Close()
assert.Equal(t, "test_sns_sqs", string(body))
changedPayload := "changed_test_sns_sqs"
r.Body = ioutil.NopCloser(bytes.NewReader([]byte(changedPayload)))
r.ContentLength = int64(len(changedPayload))
r.Header.Set("Content-Length", strconv.Itoa(len(changedPayload)))
}
topicArn := "arn:aws:sns::000000000000:api_gateway_topic"
c := aws.NewConfig()
c.WithRegion("us-east-1")
c.WithCredentials(credentials.NewStaticCredentials("id", "key", ""))
c.WithDisableSSL(true)
c.WithEndpoint(endpoint.URL)
proxy := NewSNSRequestTranslatorReverseProxy(topicArn, c, requestTranslator)
// Test gateway
gateway := httptest.NewServer(proxy)
defer gateway.Close()
// Request made for the API gateway should be proxied to SNS
res, err := http.Post(gateway.URL, "application/json", bytes.NewReader([]byte("test_sns_sqs")))
assert.Nil(t, err)
assert.Equal(t, http.StatusAccepted, res.StatusCode)
assert.Equal(t, snsRequest, "Action=Publish&Message=%7B%22default%22%3A%22changed_test_sns_sqs%22%7D&MessageStructure=json&TopicArn=arn%3Aaws%3Asns%3A%3A000000000000%3Aapi_gateway_topic&Version=2010-03-31")
})
}