Skip to content

Commit

Permalink
Add tests for sub-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
olivere committed Aug 18, 2018
1 parent 57a99c7 commit 934bd89
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 1 deletion.
7 changes: 6 additions & 1 deletion aws/sign_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ import (

// NewV4SigningClient returns an *http.Client that will sign all requests with AWS V4 Signing.
func NewV4SigningClient(credentials awsauth.Credentials) *http.Client {
return NewV4SigningClientWithHTTPClient(credentials, http.DefaultClient)
}

// NewV4SigningClientWithHTTPClient returns an *http.Client that will sign all requests with AWS V4 Signing.
func NewV4SigningClientWithHTTPClient(credentials awsauth.Credentials, httpClient *http.Client) *http.Client {
return &http.Client{
Transport: V4Transport{
HTTPClient: http.DefaultClient,
HTTPClient: httpClient,
Credentials: credentials,
},
}
Expand Down
90 changes: 90 additions & 0 deletions aws/sign_v4_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 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"

"github.com/olivere/elastic"
"github.com/smartystreets/go-aws-auth"
)

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)
}
}
102 changes: 102 additions & 0 deletions trace/opentracing/transport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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 opentracing

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"

"github.com/olivere/elastic"
)

func TestTransport(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
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()

// Mock tracer
tracer := mocktracer.New()
opentracing.InitGlobalTracer(tracer)

// Setup a simple transport
tr := NewTransport()
httpClient := &http.Client{
Transport: tr,
}

// Create a simple Ping request via Elastic
client, err := elastic.NewClient(
elastic.SetURL(ts.URL),
elastic.SetHttpClient(httpClient),
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 data written into tracer
spans := tracer.FinishedSpans()
if want, have := 1, len(spans); want != have {
t.Fatalf("want %d finished spans, have %d", want, have)
}
span := spans[0]
if want, have := "PerformRequest", span.OperationName; want != have {
t.Fatalf("want Span.OperationName=%q, have %q", want, have)
}
if want, have := "github.com/olivere/elastic", span.Tag("component"); want != have {
t.Fatalf("want component tag=%q, have %q", want, have)
}
if want, have := ts.URL+"/", span.Tag("http.url"); want != have {
t.Fatalf("want http.url tag=%q, have %q", want, have)
}
if want, have := "GET", span.Tag("http.method"); want != have {
t.Fatalf("want http.method tag=%q, have %q", want, have)
}
if want, have := uint16(http.StatusOK), span.Tag("http.status_code"); want != have {
t.Fatalf("want http.status_code tag=%v (%T), have %v (%T)", want, want, have, have)
}
}

0 comments on commit 934bd89

Please sign in to comment.