MCPcopy Index your code
hub / github.com/coder/coder / TestExtractWorkspaceProxyParam

Function TestExtractWorkspaceProxyParam

coderd/httpmw/workspaceproxy_test.go:191–301  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

189}
190
191func TestExtractWorkspaceProxyParam(t *testing.T) {
192 t.Parallel()
193
194 successHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
195 // Only called if the API key passes through the handler.
196 httpapi.Write(context.Background(), rw, http.StatusOK, codersdk.Response{
197 Message: "It worked!",
198 })
199 })
200
201 t.Run("OKName", func(t *testing.T) {
202 t.Parallel()
203 var (
204 db, _ = dbtestutil.NewDB(t)
205 r = httptest.NewRequest("GET", "/", nil)
206 rw = httptest.NewRecorder()
207
208 proxy, _ = dbgen.WorkspaceProxy(t, db, database.WorkspaceProxy{})
209 )
210
211 routeContext := chi.NewRouteContext()
212 routeContext.URLParams.Add("workspaceproxy", proxy.Name)
213 r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, routeContext))
214
215 httpmw.ExtractWorkspaceProxyParam(db, uuid.NewString(), nil)(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
216 // Checks that it exists on the context!
217 _ = httpmw.WorkspaceProxyParam(request)
218 successHandler.ServeHTTP(writer, request)
219 })).ServeHTTP(rw, r)
220 res := rw.Result()
221 defer res.Body.Close()
222 require.Equal(t, http.StatusOK, res.StatusCode)
223 })
224
225 t.Run("OKID", func(t *testing.T) {
226 t.Parallel()
227 var (
228 db, _ = dbtestutil.NewDB(t)
229 r = httptest.NewRequest("GET", "/", nil)
230 rw = httptest.NewRecorder()
231
232 proxy, _ = dbgen.WorkspaceProxy(t, db, database.WorkspaceProxy{})
233 )
234
235 routeContext := chi.NewRouteContext()
236 routeContext.URLParams.Add("workspaceproxy", proxy.ID.String())
237 r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, routeContext))
238
239 httpmw.ExtractWorkspaceProxyParam(db, uuid.NewString(), nil)(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
240 // Checks that it exists on the context!
241 _ = httpmw.WorkspaceProxyParam(request)
242 successHandler.ServeHTTP(writer, request)
243 })).ServeHTTP(rw, r)
244 res := rw.Result()
245 defer res.Body.Close()
246 require.Equal(t, http.StatusOK, res.StatusCode)
247 })
248

Callers

nothing calls this directly

Calls 15

WriteFunction · 0.92
NewDBFunction · 0.92
WorkspaceProxyFunction · 0.92
WorkspaceProxyParamFunction · 0.92
WithContextMethod · 0.80
RunMethod · 0.65
AddMethod · 0.65
ContextMethod · 0.65
CloseMethod · 0.65
NewMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected