This repository has been archived by the owner on May 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
request_forwarder_test.go
85 lines (66 loc) · 2.11 KB
/
request_forwarder_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
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"testing"
"time"
"gotest.tools/assert"
)
func credentialFor(serviceName string) string {
return fmt.Sprintf("Credential=AKIAIOSFODNN7EXAMPLE/20181117/us-east-1/%s/aws4_request", serviceName)
}
// A simplified version of the <LocalstackSingleEndpoint> http handler
func reRequest(res http.ResponseWriter, req *http.Request) {
done := make(chan bool)
reReq := &Request{
ResponseWriter: res,
Request: req,
Done: done,
}
backend := BackendFor(reReq.Request, Backend{"", "9001"})
go forward(reReq, backend)
<-done
}
func TestForward_noLocalstackBackendDetected(t *testing.T) {
go http.ListenAndServe(":9001", DefaultBackend{})
time.Sleep(150 * time.Millisecond)
req, err := http.NewRequest("POST", "/", bytes.NewBuffer([]byte("")))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(reRequest)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("wrong status code returned: got %v want %v",
status, http.StatusBadRequest)
}
expectedBody := "No localstack backend for this request"
assert.Equal(t, expectedBody, rr.Body.String())
}
func TestForward_supportedLocalstackBackends(t *testing.T) {
services := []string{"s3", "lambda", "apigateway", "dynamodb", "kinesis", "sns", "sqs"}
expectedBodyRegx := `^Post http://.*:\d{4}/: dial tcp .*:\d{4}: .*: connection refused$`
r := regexp.MustCompile(expectedBodyRegx)
for _, service := range services {
req, err := http.NewRequest("POST", "/", bytes.NewBuffer([]byte("")))
if err != nil {
t.Fatal(err)
}
req.Header.Add("Authorization", credentialFor(service))
rr := httptest.NewRecorder()
handler := http.HandlerFunc(reRequest)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusBadGateway {
t.Errorf("wrong status code returned: got %v want %v",
status, http.StatusBadRequest)
}
if !r.MatchString(rr.Body.String()) {
t.Errorf("response body differes: expected body to match (%v) got(%v)",
expectedBodyRegx, rr.Body.String())
}
}
}