TestHandleStaticFile - ensure the static file handles properly
(t *testing.T)
| 414 | |
| 415 | // TestHandleStaticFile - ensure the static file handles properly |
| 416 | func 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 |
| 450 | func TestRouteStaticListingDir(t *testing.T) { |
nothing calls this directly
no test coverage detected