Skip to content

Commit

Permalink
Add tests for existing functionality
Browse files Browse the repository at this point in the history
Adds tests for the existing REST client functionality

Signed-off-by: Kim Christensen <[email protected]>
  • Loading branch information
kichristensen authored and tsandall committed Oct 25, 2018
1 parent 74b36fb commit 318bb20
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions plugins/rest/rest_test.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
package rest

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

Expand Down Expand Up @@ -71,3 +76,96 @@ func TestNew(t *testing.T) {
}

}

func TestValidUrl(t *testing.T) {
ts := testServer{
t: t,
expMethod: "GET",
expPath: "/test",
}
ts.start()
defer ts.stop()
config := fmt.Sprintf(`{
"name": "foo",
"url": %q,
}`, ts.server.URL)
client, err := New([]byte(config))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
ctx := context.Background()
if _, err := client.Do(ctx, "GET", "test"); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}

func TestBearerToken(t *testing.T) {
ts := testServer{
t: t,
expBearerScheme: "Acmecorp-Token",
expBearerToken: "secret",
}
ts.start()
defer ts.stop()
config := fmt.Sprintf(`{
"name": "foo",
"url": %q,
"credentials": {
"bearer": {
"scheme": "Acmecorp-Token",
"token": "secret"
}
}
}`, ts.server.URL)
client, err := New([]byte(config))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
ctx := context.Background()
if _, err := client.Do(ctx, "GET", "test"); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}

type testServer struct {
t *testing.T
server *httptest.Server
expPath string
expMethod string
expBearerToken string
expBearerScheme string
tls bool
}

func (t *testServer) handle(w http.ResponseWriter, r *http.Request) {
if t.expMethod != "" && t.expMethod != r.Method {
t.t.Fatalf("Expected method %v, got %v", t.expMethod, r.Method)
}
if t.expPath != "" && t.expPath != r.URL.Path {
t.t.Fatalf("Expected path %q, got %q", t.expPath, r.URL.Path)
}
if (t.expBearerToken != "" || t.expBearerScheme != "") && len(r.Header["Authorization"]) == 0 {
t.t.Fatal("Expected bearer token, but didn't get any")
}
auth := r.Header["Authorization"][0]
if t.expBearerScheme != "" && !strings.HasPrefix(auth, t.expBearerScheme) {
t.t.Fatalf("Expected bearer scheme %q, got authorization header %q", t.expBearerScheme, auth)
}
if t.expBearerToken != "" && !strings.HasSuffix(auth, t.expBearerToken) {
t.t.Fatalf("Expected bearer token %q, got authorization header %q", t.expBearerToken, auth)
}
w.WriteHeader(200)
}

func (t *testServer) start() {
t.server = httptest.NewUnstartedServer(http.HandlerFunc(t.handle))
if t.tls {
t.server.StartTLS()
} else {
t.server.Start()
}
}

func (t *testServer) stop() {
t.server.Close()
}

0 comments on commit 318bb20

Please sign in to comment.