mirror of
https://github.com/openfaas/faas.git
synced 2025-06-11 01:36:47 +00:00
Add unit test for returning headers
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
parent
3edf499fac
commit
49b7c03285
@ -60,9 +60,20 @@ func makeLogger(next http.Handler) func(w http.ResponseWriter, r *http.Request)
|
|||||||
next.ServeHTTP(rr, r)
|
next.ServeHTTP(rr, r)
|
||||||
log.Printf("Validated request %d.\n", rr.Code)
|
log.Printf("Validated request %d.\n", rr.Code)
|
||||||
|
|
||||||
|
resHeader := rr.Header()
|
||||||
|
copyHeaders(w.Header(), &resHeader)
|
||||||
|
|
||||||
w.WriteHeader(rr.Code)
|
w.WriteHeader(rr.Code)
|
||||||
if rr.Body != nil {
|
if rr.Body != nil {
|
||||||
w.Write(rr.Body.Bytes())
|
w.Write(rr.Body.Bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func copyHeaders(destination http.Header, source *http.Header) {
|
||||||
|
for k, v := range *source {
|
||||||
|
vClone := make([]string, len(v))
|
||||||
|
copy(vClone, v)
|
||||||
|
(destination)[k] = vClone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
29
auth/basic-auth/main_test.go
Normal file
29
auth/basic-auth/main_test.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_makeLogger(t *testing.T) {
|
||||||
|
handler := http.HandlerFunc(makeLogger(http.HandlerFunc(
|
||||||
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("X-Unit-Test", "true")
|
||||||
|
})))
|
||||||
|
|
||||||
|
s := httptest.NewServer(handler)
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodGet, s.URL, nil)
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
got := rr.Header().Get("X-Unit-Test")
|
||||||
|
want := "true"
|
||||||
|
if want != got {
|
||||||
|
t.Errorf("Header X-Unit-Test, want: %s, got %s", want, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user