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

Function TestRouteStaticFile

routes_test.go:381–413  ·  view source on GitHub ↗

TestHandleStaticFile - ensure the static file handles properly

(t *testing.T)

Source from the content-addressed store, hash-verified

379
380// TestHandleStaticFile - ensure the static file handles properly
381func TestRouteStaticFile(t *testing.T) {
382 // SETUP file
383 testRoot, _ := os.Getwd()
384 f, err := os.CreateTemp(testRoot, "")
385 if err != nil {
386 t.Error(err)
387 }
388 defer os.Remove(f.Name())
389 _, err = f.WriteString("Gin Web Framework")
390 require.NoError(t, err)
391 f.Close()
392
393 dir, filename := filepath.Split(f.Name())
394
395 // SETUP gin
396 router := New()
397 router.Static("/using_static", dir)
398 router.StaticFile("/result", f.Name())
399
400 w := PerformRequest(router, http.MethodGet, "/using_static/"+filename)
401 w2 := PerformRequest(router, http.MethodGet, "/result")
402
403 assert.Equal(t, w, w2)
404 assert.Equal(t, http.StatusOK, w.Code)
405 assert.Equal(t, "Gin Web Framework", w.Body.String())
406 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
407
408 w3 := PerformRequest(router, http.MethodHead, "/using_static/"+filename)
409 w4 := PerformRequest(router, http.MethodHead, "/result")
410
411 assert.Equal(t, w3, w4)
412 assert.Equal(t, http.StatusOK, w3.Code)
413}
414
415// TestHandleStaticFile - ensure the static file handles properly
416func TestRouteStaticFileFS(t *testing.T) {

Callers

nothing calls this directly

Calls 11

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

Tested by

no test coverage detected