MCPcopy Create free account
hub / github.com/Shopify/goose / TestUseModule

Function TestUseModule

srvutil/servlet_test.go:107–131  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

105const fooKey ctxKey = iota
106
107func TestUseModule(t *testing.T) {
108 r := mux.NewRouter()
109 s := FuncServlet("/foo", func(res http.ResponseWriter, req *http.Request) {
110 ctx := req.Context()
111 foo := ctx.Value(fooKey)
112 res.Write([]byte(fmt.Sprintf("foo: %s", foo)))
113 })
114
115 s = UseServlet(s, func(next http.Handler) http.Handler {
116 return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
117 ctx := req.Context()
118 ctx = context.WithValue(ctx, fooKey, "bar")
119 req = req.WithContext(ctx)
120 next.ServeHTTP(res, req)
121 })
122 })
123 s.RegisterRouting(r)
124
125 res := httptest.NewRecorder()
126 req, err := http.NewRequest("GET", "/foo", nil)
127 assert.NoError(t, err)
128 r.ServeHTTP(res, req)
129
130 assert.Equal(t, "foo: bar", res.Body.String())
131}
132
133func TestUseModuleCombine(t *testing.T) {
134 s1 := HandlerServlet("/ok", h)

Callers

nothing calls this directly

Calls 7

FuncServletFunction · 0.85
UseServletFunction · 0.85
ValueMethod · 0.65
RegisterRoutingMethod · 0.65
WriteMethod · 0.45
ServeHTTPMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected