-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader_test.go
More file actions
51 lines (42 loc) · 1.22 KB
/
header_test.go
File metadata and controls
51 lines (42 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"context"
"github.com/remind101/pkg/httpx"
)
func TestHeader(t *testing.T) {
tests := []struct {
header http.Header
key string
val string
}{
{http.Header{http.CanonicalHeaderKey("X-Some-Version"): []string{"versionversion"}}, "X-Some-Version", "versionversion"},
{http.Header{http.CanonicalHeaderKey("X-Client-Geo"): []string{"clientgeo"}}, "X-Client-Geo", "clientgeo"},
{http.Header{http.CanonicalHeaderKey("Foo"): []string{"1234"}}, "Bar", ""},
}
for _, tt := range tests {
m := ExtractHeader(
httpx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
data := httpx.Header(ctx, tt.key)
if got, want := data, tt.val; got != want {
t.Fatalf("%s => %s; want %s", tt.key, got, want)
}
data = httpx.Header(r.Context(), tt.key)
if got, want := data, tt.val; got != want {
t.Fatalf("%s => %s; want %s", tt.key, got, want)
}
return nil
}),
tt.key,
)
ctx := context.Background()
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
req.Header = tt.header
if err := m.ServeHTTPContext(ctx, resp, req); err != nil {
t.Fatal(err)
}
}
}