forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_test.go
65 lines (58 loc) · 1.38 KB
/
transport_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
package elasticsearch
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
const (
awsHmacHeader = "AWS4-HMAC-SHA256 Credential=accessKeyID"
awsAccessKey = "accessKeyID"
awsSecretKey = "secretAccessKey"
)
var mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expectAWSRequest := r.URL.Path == "/aws"
if isAWSRequest(r) != expectAWSRequest {
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Fprint(w, "{\"ok\":1}")
}))
func isAWSRequest(r *http.Request) bool {
return strings.HasPrefix(r.Header.Get("Authorization"), awsHmacHeader) &&
r.Header.Get("X-Amz-Content-Sha256") != "" &&
r.Header.Get("X-Amz-Date") != ""
}
var transportTests = []struct {
path string
c *http.Client
}{
{
"/aws",
&http.Client{Transport: newTransport(awsAccessKey, awsSecretKey)},
},
{
"/other",
&http.Client{Transport: newTransport("", "")},
},
}
func TestTransport(t *testing.T) {
defer mockServer.Close()
for _, tt := range transportTests {
req, err := http.NewRequest(
http.MethodGet,
fmt.Sprintf("%s%s", mockServer.URL, tt.path),
nil,
)
if err != nil {
t.Fatalf("unable to build request, %s", err)
}
resp, err := tt.c.Do(req)
if err != nil {
t.Errorf("failed to send request, %s", err)
} else if resp.StatusCode == http.StatusBadRequest {
t.Errorf("bad request sent for %s", tt.path)
}
}
}