-
Notifications
You must be signed in to change notification settings - Fork 26
/
httpMiddlewares_test.go
62 lines (43 loc) · 1.44 KB
/
httpMiddlewares_test.go
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
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_noIndexHeader(t *testing.T) {
h := noIndexHeader(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
// Do nothing
}))
req := httptest.NewRequest(http.MethodGet, "http://example.org", nil)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
res := rec.Result()
_ = res.Body.Close()
assert.Equal(t, "noindex", res.Header.Get("X-Robots-Tag"))
}
func Test_fixHTTPHandler(t *testing.T) {
var got *http.Request
h := fixHTTPHandler(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
got = r
}))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "http://example.org/übung", nil)
h.ServeHTTP(rec, req)
assert.Equal(t, "/übung", got.URL.Path)
assert.Equal(t, "", got.URL.RawPath)
req = httptest.NewRequest(http.MethodGet, "http://example.org/%C3%BCbung", nil)
h.ServeHTTP(rec, req)
assert.Equal(t, "/übung", got.URL.Path)
assert.Equal(t, "", got.URL.RawPath)
}
func Test_keepSelectedQueryParams(t *testing.T) {
var got *http.Request
h := keepSelectedQueryParams("size")(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
got = r
}))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "http://example.org/test?def=1234&size=123&abc=def", nil)
h.ServeHTTP(rec, req)
assert.Equal(t, "/test?size=123", got.URL.RequestURI())
}