From 64d203491c5314205871f11a661a0b548a800db0 Mon Sep 17 00:00:00 2001 From: jordi collell Date: Fri, 8 May 2015 07:41:48 +0200 Subject: [PATCH] Some failing tests --- middleware/basicauth/basicauth_test.go | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 middleware/basicauth/basicauth_test.go diff --git a/middleware/basicauth/basicauth_test.go b/middleware/basicauth/basicauth_test.go new file mode 100644 index 00000000..7b551ba4 --- /dev/null +++ b/middleware/basicauth/basicauth_test.go @@ -0,0 +1,58 @@ +package basicauth + +import ( + "encoding/base64" + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/mholt/caddy/middleware" +) + +func TestBasicAuth(t *testing.T) { + + rw := BasicAuth{ + Next: middleware.HandlerFunc(contentHandler), + Rules: []Rule{ + {Username: "test", Password: "ttest", Resources: []string{"/testing"}}, + }, + } + + tests := []struct { + from string + result int + cred string + }{ + {"/testing", http.StatusOK, "test:ttest"}, + {"/testing", http.StatusUnauthorized, ""}, + + } + + //auth := "Basic " + base64.StdEncoding.EncodeToString([]byte("foo:bar")) + for i, test := range tests { + + + req, err := http.NewRequest("GET", test.from, nil) + if err != nil { + t.Fatalf("Test %d: Could not create HTTP request %v", i, err) + } + auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(test.cred)) + req.Header.Set("Authorization", auth) + + rec := httptest.NewRecorder() + rw.ServeHTTP(rec, req) + + if rec.Code != test.result { + t.Errorf("Test %d: Expected Header '%d' but was '%d'", + i, test.result, rec.Code) + } + + } + +} + +func contentHandler(w http.ResponseWriter, r *http.Request) (int, error) { + fmt.Fprintf(w, r.URL.String()) + return 0, nil +} \ No newline at end of file