MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / TestConditional

Function TestConditional

pkg/webmiddleware/webmiddleware_test.go:56–84  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

54}
55
56func TestConditional(t *testing.T) {
57 a := assertions.New(t)
58
59 flag := false
60 middleware := Conditional(
61 func(next http.Handler) http.Handler {
62 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
63 flag = true
64 next.ServeHTTP(w, r)
65 })
66 },
67 func(r *http.Request) bool {
68 return r.Header.Get("X-Condition") == "foo"
69 },
70 )
71 r := httptest.NewRequest(http.MethodGet, "/", nil)
72 r.Header.Set("X-Condition", "bar")
73 rec := httptest.NewRecorder()
74 middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
75 a.So(flag, should.Equal, false)
76 })).ServeHTTP(rec, r)
77
78 r = httptest.NewRequest(http.MethodGet, "/", nil)
79 r.Header.Set("X-Condition", "foo")
80 rec = httptest.NewRecorder()
81 middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
82 a.So(flag, should.Equal, true)
83 })).ServeHTTP(rec, r)
84}

Callers

nothing calls this directly

Calls 5

ConditionalFunction · 0.85
NewMethod · 0.65
GetMethod · 0.65
SetMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected