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