Add body from basic auth plugin.

Fixes issue by adding unit test to make sure the body from
the plugin is written correctly and proxied to the client.

Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis 2019-06-07 10:11:40 +01:00
parent ef811783fb
commit d6b3847fbd
2 changed files with 36 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package handlers
import ( import (
"context" "context"
"io"
"net/http" "net/http"
"time" "time"
) )
@ -36,5 +37,9 @@ func MakeExternalAuthHandler(next http.HandlerFunc, upstreamTimeout time.Duratio
copyHeaders(w.Header(), &res.Header) copyHeaders(w.Header(), &res.Header)
w.WriteHeader(res.StatusCode) w.WriteHeader(res.StatusCode)
if res.Body != nil {
io.Copy(w, res.Body)
}
} }
} }

View File

@ -1,6 +1,7 @@
package handlers package handlers
import ( import (
"bytes"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -30,6 +31,36 @@ func Test_External_Auth_Wrapper_FailsInvalidAuth(t *testing.T) {
} }
} }
func Test_External_Auth_Wrapper_FailsInvalidAuth_WritesBody(t *testing.T) {
wantBody := []byte(`invalid credentials`)
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
w.Write(wantBody)
}))
defer s.Close()
next := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
}
passBody := false
handler := MakeExternalAuthHandler(next, time.Second*5, s.URL, passBody)
req := httptest.NewRequest(http.MethodGet, s.URL, nil)
rr := httptest.NewRecorder()
handler(rr, req)
if rr.Code == http.StatusOK {
t.Errorf("Status incorrect, did not want: %d, but got %d", http.StatusOK, rr.Code)
}
if bytes.Compare(rr.Body.Bytes(), wantBody) != 0 {
t.Errorf("Body incorrect, want: %s, but got %s", []byte(wantBody), rr.Body)
}
}
func Test_External_Auth_Wrapper_PassesValidAuth(t *testing.T) { func Test_External_Auth_Wrapper_PassesValidAuth(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {