loginsrv

Unnamed repository; edit this file 'description' to name the repository.
git clone git@jamesshield.xyz:repos/loginsrv.git
Log | Files | Refs | README | LICENSE

commit 94bd65b2a76d7a98d9fea5bf12da20a0e91be68f
parent e272a7b640082cb950397a4464062686051ee147
Author: magikstm <myskina@gmail.com>
Date:   Thu,  9 Nov 2017 21:46:03 -0500

go fmt and added some comments

Diffstat:
Mcaddy/handler.go | 2+-
Mcaddy/handler_test.go | 59+++++++++++++++++++++++++++++------------------------------
2 files changed, 30 insertions(+), 31 deletions(-)

diff --git a/caddy/handler.go b/caddy/handler.go @@ -36,6 +36,6 @@ func (h *CaddyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, e h.loginHandler.ServeHTTP(w, r) return 0, nil } - + return h.next.ServeHTTP(w, r) } diff --git a/caddy/handler_test.go b/caddy/handler_test.go @@ -11,16 +11,16 @@ import ( "time" ) -func Test_ServeHTTP_200(t *testing.T) { +func Test_ServeHTTP_200(t *testing.T) { //Set the ServeHTTP *http.Request r, err := http.NewRequest("GET", "/", nil) if err != nil { t.Fatalf("Unable to create request: %v", err) } - + //Set the ServeHTTP http.ResponseWriter w := httptest.NewRecorder() - + //Set the CaddyHandler config configh := login.DefaultConfig() configh.Backends = login.Options{"simple": {"bob": "secret"}} @@ -28,16 +28,16 @@ func Test_ServeHTTP_200(t *testing.T) { if err != nil { t.Errorf("Expected nil error, got: %v", err) } - + //Set the CaddyHandler that will use ServeHTTP h := &CaddyHandler{ - next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { - return http.StatusOK, nil // not t.Fatalf, or we will not see what other methods yield + next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { + return http.StatusOK, nil // not t.Fatalf, or we will not see what other methods yield }), - config: login.DefaultConfig(), - loginHandler: loginh, + config: login.DefaultConfig(), + loginHandler: loginh, } - + //Set user token userInfo := model.UserInfo{Sub: "bob", Expiry: time.Now().Add(time.Second).Unix()} token := jwt.NewWithClaims(jwt.SigningMethodHS512, userInfo) @@ -45,26 +45,26 @@ func Test_ServeHTTP_200(t *testing.T) { if err != nil { t.Errorf("Expected nil error, got: %v", err) } - + //Set cookie for user token on the ServeHTTP http.ResponseWriter - cookie := http.Cookie{Name: "jwt_token",Value: validToken, HttpOnly: true} - http.SetCookie(w, &cookie) - + cookie := http.Cookie{Name: "jwt_token", Value: validToken, HttpOnly: true} + http.SetCookie(w, &cookie) + //Add the cookie to the request r.AddCookie(&cookie) - + //Test that cookie is a valid token - _ ,valid := loginh.GetToken(r) + _, valid := loginh.GetToken(r) if !valid { t.Errorf("loginHandler cookie is not valid") } - + status, err := h.ServeHTTP(w, r) - + if err != nil { t.Errorf("Expected nil error, got: %v", err) } - + if status != 200 { t.Errorf("Expected returned status code to be %d, got %d", 0, status) } @@ -76,10 +76,10 @@ func Test_ServeHTTP_login(t *testing.T) { if err != nil { t.Fatalf("Unable to create request: %v", err) } - + //Set the ServeHTTP http.ResponseWriter w := httptest.NewRecorder() - + //Set the CaddyHandler config configh := login.DefaultConfig() configh.Backends = login.Options{"simple": {"bob": "secret"}} @@ -87,23 +87,23 @@ func Test_ServeHTTP_login(t *testing.T) { if err != nil { t.Errorf("Expected nil error, got: %v", err) } - + //Set the CaddyHandler that will use ServeHTTP h := &CaddyHandler{ - next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { - return http.StatusOK, nil // not t.Fatalf, or we will not see what other methods yield + next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { + return http.StatusOK, nil // not t.Fatalf, or we will not see what other methods yield }), - config: login.DefaultConfig(), - loginHandler: loginh, + config: login.DefaultConfig(), + loginHandler: loginh, } - + status, err := h.ServeHTTP(w, r) - + if err != nil { t.Errorf("Expected nil error, got: %v", err) } - + if status != 0 { t.Errorf("Expected returned status code to be %d, got %d", 0, status) } -}- \ No newline at end of file +}