Skip to content

Commit

Permalink
Merge pull request #5 from delphinus/feature/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
delphinus authored May 20, 2017
2 parents e404353 + 0266894 commit 0f5e9ee
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 33 deletions.
122 changes: 93 additions & 29 deletions digestRequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,127 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/abbot/go-http-auth"
"github.com/pkg/errors"
"golang.org/x/net/context"
)

// startServer is written with referring to
func testRequest(h http.HandlerFunc, setClient func(context.Context) context.Context) error {
ctx := context.Background()

if setClient != nil {
ctx = setClient(ctx)
}

ts := httptest.NewServer(h)
defer ts.Close()

r := New(ctx, "john", "hello")

req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
return errors.Wrap(err, "error in NewRequest")
}

resp, err := r.Do(req)
if err != nil {
return errors.Wrap(err, "error in Do")
}
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
return errors.Errorf("error status code: %s", resp.Status)
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "error in ReadAll")
}

if string(b) != "OK" {
return errors.Errorf("invalid body: %s", string(b))
}

return nil
}

// digestHandler is written with referring to
// https://github.com/abbot/go-http-auth/blob/master/examples/digest.go
func startServer(ctx context.Context) *httptest.Server {
var digestHandler = func() http.HandlerFunc {
a := auth.NewDigestAuthenticator("example.com", func(user, realm string) string {
if user == "john" {
return "b98e16cbc3d01734b264adba7baa3bf9" // password is "hello"
}
return ""
})
ts := httptest.NewServer(a.Wrap(func(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
return a.Wrap(func(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
fmt.Fprintf(w, "OK")
}))

go func() {
<-ctx.Done()
ts.Close()
}()
})
}()

return ts
func TestDigestRequestWithClient(t *testing.T) {
err := testRequest(digestHandler, func(ctx context.Context) context.Context {
return ContextWithClient(ctx, http.DefaultClient)
})
if err != nil {
t.Errorf("error in testRequest: %v", err)
}
}

func TestDigestRequest(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

ctx = ContextWithClient(ctx, http.DefaultClient)
ts := startServer(ctx)
func TestDigestRequestWithoutClient(t *testing.T) {
if err := testRequest(digestHandler, nil); err != nil {
t.Errorf("error in testRequest: %v", err)
}
}

r := New(ctx, "john", "hello")
func testNormalRequest(writer func(w http.ResponseWriter)) error {
return testRequest(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
writer(w)
}),
nil,
)
}

req, err := http.NewRequest("GET", ts.URL, nil)
func TestNormalRequest(t *testing.T) {
err := testNormalRequest(func(w http.ResponseWriter) {
fmt.Fprintf(w, "OK")
})
if err != nil {
t.Errorf("error in NewRequest: %v", err)
t.Errorf("error in testRequest: %v", err)
}
}

resp, err := r.Do(req)
if err != nil {
t.Errorf("error in Do: %v", err)
func TestNormalRequestWithUnauthorizedError(t *testing.T) {
err := testNormalRequest(func(w http.ResponseWriter) {
http.Error(w, "OK", http.StatusUnauthorized)
})
if !strings.Contains(err.Error(), "headers do not have Www-Authenticate") {
t.Errorf("different error: %v", err)
}
defer func() { _ = resp.Body.Close() }()
}

if resp.StatusCode != http.StatusOK {
t.Errorf("error status code: %s", resp.Status)
func TestNormalRequestWithInvalidHeaders(t *testing.T) {
err := testNormalRequest(func(w http.ResponseWriter) {
w.Header().Set(wwwAuthenticate, "hoge")
http.Error(w, "OK", http.StatusUnauthorized)
})
if !strings.Contains(err.Error(), "header is invalid") {
t.Errorf("different error: %v", err)
}
}

b, err := ioutil.ReadAll(resp.Body)
func TestInvalidRequests(t *testing.T) {
req, err := http.NewRequest("GET", "", nil) // invalid request
if err != nil {
t.Errorf("error in ReadAll: %v", err)
t.Fatalf("error in NewRequest: %v", err)
}

if string(b) != "OK" {
t.Errorf("invalid body: %s", string(b))
_, err = New(context.Background(), "", "").Do(req)
if err == nil {
t.Fatalf("no error")
}
}
11 changes: 8 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ import:
- context
testImport:
- package: github.com/abbot/go-http-auth
version: ^0.3.0
version: d45c47bedec736d172957bd394786b76626fa8ac

0 comments on commit 0f5e9ee

Please sign in to comment.