Debian Codegraph @

aws/sign_v4_test.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package aws
6
7 import (
8 "context"
9 "fmt"
10 "net/http"
11 "net/http/httptest"
12 "testing"
13
14 "github.com/smartystreets/go-aws-auth"
15
16 "gopkg.in/olivere/elastic.v5"
17 )
18
19 func TestSigningClient(t *testing.T) {
20 var req *http.Request
21 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22 switch r.URL.Path {
23 case "/":
24 req = r // capture the HTTP request
25 w.WriteHeader(http.StatusOK)
26 fmt.Fprintln(w, `{
27 "name" : "n5VFSHr",
28 "cluster_name" : "elasticsearch",
29 "cluster_uuid" : "8nwVxw7bTRuzfBT45QIRWA",
30 "version" : {
31 "number" : "5.6.10",
32 "build_hash" : "b727a60",
33 "build_date" : "2018-06-06T15:48:34.860Z",
34 "build_snapshot" : false,
35 "lucene_version" : "6.6.1"
36 },
37 "tagline" : "You Know, for Search"
38 }`)
39 return
40 default:
41 w.WriteHeader(http.StatusInternalServerError)
42 return
43 }
44 }))
45 defer ts.Close()
46
47 cred := awsauth.Credentials{
48 AccessKeyID: "dev",
49 SecretAccessKey: "secret",
50 }
51 signingClient := NewV4SigningClient(cred)
52
53 // Create a simple Ping request via Elastic
54 client, err := elastic.NewClient(
55 elastic.SetURL(ts.URL),
56 elastic.SetHttpClient(signingClient),
57 elastic.SetHealthcheck(false),
58 elastic.SetSniff(false),
59 )
60 if err != nil {
61 t.Fatal(err)
62 }
63 res, code, err := client.Ping(ts.URL).Do(context.Background())
64 if err != nil {
65 t.Fatal(err)
66 }
67 if want, have := http.StatusOK, code; want != have {
68 t.Fatalf("want Status=%d, have %d", want, have)
69 }
70 if want, have := "You Know, for Search", res.TagLine; want != have {
71 t.Fatalf("want TagLine=%q, have %q", want, have)
72 }
73
74 // Check the request recorded in the HTTP test server (see above)
75 if req == nil {
76 t.Fatal("expected to capture HTTP request")
77 }
78 if have := req.Header.Get("Authorization"); have == "" {
79 t.Fatal("expected Authorization header")
80 }
81 if have := req.Header.Get("X-Amz-Date"); have == "" {
82 t.Fatal("expected X-Amz-Date header")
83 }
84 if want, have := `e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855`, req.Header.Get("X-Amz-Content-Sha256"); want != have {
85 t.Fatalf("want header of X-Amz-Content-Sha256=%q, have %q", want, have)
86 }
87 }