MCPcopy
hub / github.com/gin-gonic/gin / TestRouteStaticFileFS

Function TestRouteStaticFileFS

routes_test.go:416–447  ·  view source on GitHub ↗

TestHandleStaticFile - ensure the static file handles properly

(t *testing.T)

Source from the content-addressed store, hash-verified

414
415// TestHandleStaticFile - ensure the static file handles properly
416func TestRouteStaticFileFS(t *testing.T) {
417 // SETUP file
418 testRoot, _ := os.Getwd()
419 f, err := os.CreateTemp(testRoot, "")
420 if err != nil {
421 t.Error(err)
422 }
423 defer os.Remove(f.Name())
424 _, err = f.WriteString("Gin Web Framework")
425 require.NoError(t, err)
426 f.Close()
427
428 dir, filename := filepath.Split(f.Name())
429 // SETUP gin
430 router := New()
431 router.Static("/using_static", dir)
432 router.StaticFileFS("/result_fs", filename, Dir(dir, false))
433
434 w := PerformRequest(router, http.MethodGet, "/using_static/"+filename)
435 w2 := PerformRequest(router, http.MethodGet, "/result_fs")
436
437 assert.Equal(t, w, w2)
438 assert.Equal(t, http.StatusOK, w.Code)
439 assert.Equal(t, "Gin Web Framework", w.Body.String())
440 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
441
442 w3 := PerformRequest(router, http.MethodHead, "/using_static/"+filename)
443 w4 := PerformRequest(router, http.MethodHead, "/result_fs")
444
445 assert.Equal(t, w3, w4)
446 assert.Equal(t, http.StatusOK, w3.Code)
447}
448
449// TestHandleStaticDir - ensure the root/sub dir handles properly
450func TestRouteStaticListingDir(t *testing.T) {

Callers

nothing calls this directly

Calls 12

NewFunction · 0.85
DirFunction · 0.85
PerformRequestFunction · 0.85
CloseMethod · 0.80
NameMethod · 0.65
WriteStringMethod · 0.65
StaticMethod · 0.65
StaticFileFSMethod · 0.65
StringMethod · 0.65
ErrorMethod · 0.45
GetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected