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 33a9bae99e60665d0d977904927ffbd9990b0a14
parent b3c9c9352e1ab823e9cd9456c7f7a474634aa528
Author: Sebastian Mancke <s.mancke@tarent.de>
Date:   Sun, 12 Nov 2017 21:11:09 +0100

Check for replacement of the user in caddy handler.

Diffstat:
Mcaddy/handler_test.go | 16++++++++++++++++
1 file changed, 16 insertions(+), 0 deletions(-)

diff --git a/caddy/handler_test.go b/caddy/handler_test.go @@ -1,6 +1,7 @@ package caddy import ( + "context" "github.com/dgrijalva/jwt-go" "github.com/mholt/caddy/caddyhttp/httpserver" "github.com/tarent/loginsrv/login" @@ -19,6 +20,9 @@ func Test_ServeHTTP_200(t *testing.T) { t.Fatalf("Unable to create request: %v", err) } + // Associate a replacer with the request: + r = r.WithContext(context.WithValue(context.Background(), httpserver.ReplacerCtxKey, httpserver.NewReplacer(r, nil, "-"))) + //Set the ServeHTTP http.ResponseWriter w := httptest.NewRecorder() @@ -69,6 +73,18 @@ func Test_ServeHTTP_200(t *testing.T) { if status != 200 { t.Errorf("Expected returned status code to be %d, got %d", 0, status) } + + // Check that the replacer now is able to substitute the user variable in log lines + replacer, replacerOk := r.Context().Value(httpserver.ReplacerCtxKey).(httpserver.Replacer) + if !replacerOk { + t.Errorf("no replacer associated with request") + + } else { + replacement := replacer.Replace("{user}") + if replacement != "bob" { + t.Errorf(`wrong replacement: expected "bob", but got %q`, replacement) + } + } } //Tests the login page without being logged as a user (doesn't test that the {user} replacer stays as-is)