-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsign_v4_test.go
91 lines (83 loc) · 2.46 KB
/
sign_v4_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
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package aws
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
awsauth "github.com/smartystreets/go-aws-auth"
"github.com/olivere/elastic/v7"
)
func TestSigningClient(t *testing.T) {
var req *http.Request
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
req = r // capture the HTTP request
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{
"name" : "Qg28M36",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "rwHa7BBnRC2h8KoDfCbmuQ",
"version" : {
"number" : "6.3.2",
"build_flavor" : "oss",
"build_type" : "tar",
"build_hash" : "053779d",
"build_date" : "2018-07-20T05:20:23.451332Z",
"build_snapshot" : false,
"lucene_version" : "7.3.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}`)
return
default:
w.WriteHeader(http.StatusInternalServerError)
return
}
}))
defer ts.Close()
cred := awsauth.Credentials{
AccessKeyID: "dev",
SecretAccessKey: "secret",
}
signingClient := NewV4SigningClient(cred)
// Create a simple Ping request via Elastic
client, err := elastic.NewClient(
elastic.SetURL(ts.URL),
elastic.SetHttpClient(signingClient),
elastic.SetHealthcheck(false),
elastic.SetSniff(false),
)
if err != nil {
t.Fatal(err)
}
res, code, err := client.Ping(ts.URL).Do(context.Background())
if err != nil {
t.Fatal(err)
}
if want, have := http.StatusOK, code; want != have {
t.Fatalf("want Status=%d, have %d", want, have)
}
if want, have := "You Know, for Search", res.TagLine; want != have {
t.Fatalf("want TagLine=%q, have %q", want, have)
}
// Check the request recorded in the HTTP test server (see above)
if req == nil {
t.Fatal("expected to capture HTTP request")
}
if have := req.Header.Get("Authorization"); have == "" {
t.Fatal("expected Authorization header")
}
if have := req.Header.Get("X-Amz-Date"); have == "" {
t.Fatal("expected X-Amz-Date header")
}
if want, have := `e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855`, req.Header.Get("X-Amz-Content-Sha256"); want != have {
t.Fatalf("want header of X-Amz-Content-Sha256=%q, have %q", want, have)
}
}