novelty.go

Single word (yes/no) site for app engine in go
git clone https://wehaveforgeathome.hates.computer/novelty.go.git
Log | Files | Refs | LICENSE

commit e1976b1606be1831108eb2e801525d7011260545
parent 5f48577e2d8fa3fe451372780d5224a3d06ede7f
Author: Ryan Wolf <rwolf@borderstylo.com>
Date:   Thu, 12 Apr 2012 21:30:33 -0700

formatting

Diffstat:
Mbasicauth/basicauth.go | 44++++++++++++++++++++++----------------------
Mbasicauth/basicauth_test.go | 70+++++++++++++++++++++++++++++++++++-----------------------------------
Mnovelty/novelty.go | 6+++---
3 files changed, 60 insertions(+), 60 deletions(-)

diff --git a/basicauth/basicauth.go b/basicauth/basicauth.go @@ -1,29 +1,29 @@ package basicauth import ( - "encoding/base64" - "errors" - "strings" + "encoding/base64" + "errors" + "strings" ) func Decode(h string) (u string, p string, e error) { - prefix := "Basic " - if !strings.HasPrefix(h, prefix) { - e = errors.New("Bad Request") - return - } - h = h[6:] - auth, err := base64.StdEncoding.DecodeString(h) - if err != nil { - e = errors.New("Bad Request") - return - } - fields := strings.Split(string(auth), ":") - if len(fields) != 2 { - e = errors.New("Bad Request") - return - } - u = fields[0] - p = fields[1] - return + prefix := "Basic " + if !strings.HasPrefix(h, prefix) { + e = errors.New("Bad Request") + return + } + h = h[6:] + auth, err := base64.StdEncoding.DecodeString(h) + if err != nil { + e = errors.New("Bad Request") + return + } + fields := strings.Split(string(auth), ":") + if len(fields) != 2 { + e = errors.New("Bad Request") + return + } + u = fields[0] + p = fields[1] + return } diff --git a/basicauth/basicauth_test.go b/basicauth/basicauth_test.go @@ -1,60 +1,60 @@ package basicauth import ( - "encoding/base64" - "testing" + "encoding/base64" + "testing" ) func TestDecodeEmpty(t *testing.T) { - if _, _, err := Decode(""); err == nil { - t.Error("Should fail on empty") - } + if _, _, err := Decode(""); err == nil { + t.Error("Should fail on empty") + } } func TestDecodeMissingPrefix(t *testing.T) { - if _, _, err := Decode("Bees"); err == nil { - t.Error("Should fail on missing prefix") - } + if _, _, err := Decode("Bees"); err == nil { + t.Error("Should fail on missing prefix") + } } func TestDecodeEmptySuffix(t *testing.T) { - if _, _, err := Decode("Basic "); err == nil { - t.Error("Should fail on empty suffix") - } + if _, _, err := Decode("Basic "); err == nil { + t.Error("Should fail on empty suffix") + } } func TestDecodeInvalidSuffix(t *testing.T) { - if _, _, err := Decode("Basic !*@&#@"); err == nil { - t.Error("Should fail on invalid suffix") - } + if _, _, err := Decode("Basic !*@&#@"); err == nil { + t.Error("Should fail on invalid suffix") + } } func TestDecodeNoDelimiter(t *testing.T) { - a := base64.StdEncoding.EncodeToString([]byte("bees")) - if _, _, err := Decode(a); err == nil { - t.Error("Should fail on no delmiter") - } + a := base64.StdEncoding.EncodeToString([]byte("bees")) + if _, _, err := Decode(a); err == nil { + t.Error("Should fail on no delmiter") + } } func TestDecodeTooManyDelimiters(t *testing.T) { - a := base64.StdEncoding.EncodeToString([]byte("b:e:s")) - if _, _, err := Decode(a); err == nil { - t.Error("Should fail on too many delimiters") - } + a := base64.StdEncoding.EncodeToString([]byte("b:e:s")) + if _, _, err := Decode(a); err == nil { + t.Error("Should fail on too many delimiters") + } } func TestDecodeValidAuth(t *testing.T) { - u := "ryan" - p := "clowns" - a := base64.StdEncoding.EncodeToString([]byte(u + ":" + p)) - username, password, err := Decode("Basic " + a) - if err != nil { - t.Error("Error should be nil") - } - if username != u { - t.Errorf("Wrong username. Expected '%s' got '%s'", u, username) - } - if password != p { - t.Errorf("Wrong password. Expected '%s' got '%s'", p, password) - } + u := "ryan" + p := "clowns" + a := base64.StdEncoding.EncodeToString([]byte(u + ":" + p)) + username, password, err := Decode("Basic " + a) + if err != nil { + t.Error("Error should be nil") + } + if username != u { + t.Errorf("Wrong username. Expected '%s' got '%s'", u, username) + } + if password != p { + t.Errorf("Wrong password. Expected '%s' got '%s'", p, password) + } } diff --git a/novelty/novelty.go b/novelty/novelty.go @@ -3,7 +3,7 @@ package novelty import ( "appengine" "appengine/datastore" - "basicauth" + "basicauth" "html/template" "net/http" ) @@ -38,8 +38,8 @@ func getAnswer(w http.ResponseWriter, r *http.Request) { func authorized(r *http.Request) bool { h := r.Header.Get("Authorization") - _, password, err := basicauth.Decode(h) - if err != nil { + _, password, err := basicauth.Decode(h) + if err != nil { return false } c := appengine.NewContext(r)