handler_test.go (3916B)
1 package caddy 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 "time" 8 9 "github.com/dgrijalva/jwt-go" 10 "github.com/caddyserver/caddy/caddyhttp/httpserver" 11 "github.com/tarent/loginsrv/login" 12 "github.com/tarent/loginsrv/model" 13 ) 14 15 //Tests a page while being logged in as a user (doesn't test that the {user} replacer changes) 16 func Test_ServeHTTP_200(t *testing.T) { 17 //Set the ServeHTTP *http.Request 18 r, err := http.NewRequest("GET", "/", nil) 19 if err != nil { 20 t.Fatalf("Unable to create request: %v", err) 21 } 22 23 /** 24 TODO: This will only work with the caddy master branch or the next caddy release 25 26 // Associate a replacer with the request: 27 r = r.WithContext(context.WithValue(context.Background(), httpserver.ReplacerCtxKey, httpserver.NewReplacer(r, nil, "-"))) 28 */ 29 30 //Set the ServeHTTP http.ResponseWriter 31 w := httptest.NewRecorder() 32 33 //Set the CaddyHandler config 34 configh := login.DefaultConfig() 35 configh.Backends = login.Options{"simple": {"bob": "secret"}} 36 loginh, err := login.NewHandler(configh) 37 if err != nil { 38 t.Errorf("Expected nil error, got: %v", err) 39 } 40 41 //Set the CaddyHandler that will use ServeHTTP 42 h := &CaddyHandler{ 43 next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { 44 return http.StatusOK, nil // not t.Fatalf, or we will not see what other methods yield 45 }), 46 config: login.DefaultConfig(), 47 loginHandler: loginh, 48 } 49 50 //Set user token 51 userInfo := model.UserInfo{Sub: "bob", Expiry: time.Now().Add(time.Second).Unix()} 52 token := jwt.NewWithClaims(jwt.SigningMethodHS512, userInfo) 53 validToken, err := token.SignedString([]byte(h.config.JwtSecret)) 54 if err != nil { 55 t.Errorf("Expected nil error, got: %v", err) 56 } 57 58 //Set cookie for user token on the ServeHTTP http.ResponseWriter 59 cookie := http.Cookie{Name: "jwt_token", Value: validToken, HttpOnly: true} 60 http.SetCookie(w, &cookie) 61 62 //Add the cookie to the request 63 r.AddCookie(&cookie) 64 65 //Test that cookie is a valid token 66 _, valid := loginh.GetToken(r) 67 if !valid { 68 t.Errorf("loginHandler cookie is not valid") 69 } 70 71 status, err := h.ServeHTTP(w, r) 72 73 if err != nil { 74 t.Errorf("Expected nil error, got: %v", err) 75 } 76 77 if status != 200 { 78 t.Errorf("Expected returned status code to be %d, got %d", 0, status) 79 } 80 81 /** 82 TODO: This will only work with the caddy master branch or the next caddy release 83 84 85 // Check that the replacer now is able to substitute the user variable in log lines 86 replacer, replacerOk := r.Context().Value(httpserver.ReplacerCtxKey).(httpserver.Replacer) 87 if !replacerOk { 88 t.Errorf("no replacer associated with request") 89 90 } else { 91 replacement := replacer.Replace("{user}") 92 if replacement != "bob" { 93 t.Errorf(`wrong replacement: expected "bob", but got %q`, replacement) 94 } 95 } 96 */ 97 } 98 99 //Tests the login page without being logged as a user (doesn't test that the {user} replacer stays as-is) 100 func Test_ServeHTTP_login(t *testing.T) { 101 //Set the ServeHTTP *http.Request 102 r, err := http.NewRequest("GET", "/login", nil) 103 if err != nil { 104 t.Fatalf("Unable to create request: %v", err) 105 } 106 107 //Set the ServeHTTP http.ResponseWriter 108 w := httptest.NewRecorder() 109 110 //Set the CaddyHandler config 111 configh := login.DefaultConfig() 112 configh.Backends = login.Options{"simple": {"bob": "secret"}} 113 loginh, err := login.NewHandler(configh) 114 if err != nil { 115 t.Errorf("Expected nil error, got: %v", err) 116 } 117 118 //Set the CaddyHandler that will use ServeHTTP 119 h := &CaddyHandler{ 120 next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { 121 return http.StatusOK, nil // not t.Fatalf, or we will not see what other methods yield 122 }), 123 config: login.DefaultConfig(), 124 loginHandler: loginh, 125 } 126 127 status, err := h.ServeHTTP(w, r) 128 129 if err != nil { 130 t.Errorf("Expected nil error, got: %v", err) 131 } 132 133 if status != 0 { 134 t.Errorf("Expected returned status code to be %d, got %d", 0, status) 135 } 136 }