Skip to content
Myr

test(context): add comprehensive unit tests for `Context.File()` method (#4307)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* test: add comprehensive unit tests for Context.File() method - Add TestContextFile with multiple test scenarios in context_test.go - Add simplified tests in context_file_test.go - Cover file serving, 404 handling, directory access, HEAD requests, and Range requests - All tests pass successfully * fix: resolve gci formatting issues in test files * fix: correct test file paths and add test file to gin directory * move test_file.txt to testdata directory as suggested by maintainer * fix: update test file paths to use testdata directory

authored by Leon cap · committed by GitHub · parent 9708475 · Browse files · 3 files+110 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+110 added−0 removed

  • other:+110 added−0 removed

3 files+110 added−0 removed

added:testdata/test_file.txt+2 added
1This is a test file for Context.File() method testing.2It contains some sample content to verify file serving functionality.
added:context_file_test.go+35 added
1package gin23import (4 "net/http"5 "net/http/httptest"6 "testing"78 "github.com/stretchr/testify/assert"9)1011// TestContextFileSimple tests the Context.File() method with a simple case12func TestContextFileSimple(t *testing.T) {13 // Test serving an existing file14 testFile := "testdata/test_file.txt"15 w := httptest.NewRecorder()16 c, _ := CreateTestContext(w)17 c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)1819 c.File(testFile)2021 assert.Equal(t, http.StatusOK, w.Code)22 assert.Contains(t, w.Body.String(), "This is a test file")23 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))24}2526// TestContextFileNotFound tests serving a non-existent file27func TestContextFileNotFound(t *testing.T) {28 w := httptest.NewRecorder()29 c, _ := CreateTestContext(w)30 c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)3132 c.File("non_existent_file.txt")3334 assert.Equal(t, http.StatusNotFound, w.Code)35}
modified:context_test.go+73 added
73 unmodified lines
74 panic(err.Error())75 }76}7778// TestContextFile tests the Context.File() method79func TestContextFile(t *testing.T) {80 // Test serving an existing file81 t.Run("serve existing file", func(t *testing.T) {82 // Create a temporary test file83 testFile := "testdata/test_file.txt"8485 w := httptest.NewRecorder()86 c, _ := CreateTestContext(w)87 c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)8889 c.File(testFile)9091 assert.Equal(t, http.StatusOK, w.Code)92 assert.Contains(t, w.Body.String(), "This is a test file")93 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))94 })9596 // Test serving a non-existent file97 t.Run("serve non-existent file", func(t *testing.T) {98 w := httptest.NewRecorder()99 c, _ := CreateTestContext(w)100 c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)101102 c.File("non_existent_file.txt")103104 assert.Equal(t, http.StatusNotFound, w.Code)105 })106107 // Test serving a directory (should return 200 with directory listing or 403 Forbidden)108 t.Run("serve directory", func(t *testing.T) {109 w := httptest.NewRecorder()110 c, _ := CreateTestContext(w)111 c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)112113 c.File(".")114115 // Directory serving can return either 200 (with listing) or 403 (forbidden)116 assert.True(t, w.Code == http.StatusOK || w.Code == http.StatusForbidden)117 })118119 // Test with HEAD request120 t.Run("HEAD request", func(t *testing.T) {121 testFile := "testdata/test_file.txt"122123 w := httptest.NewRecorder()124 c, _ := CreateTestContext(w)125 c.Request = httptest.NewRequest(http.MethodHead, "/test", nil)126127 c.File(testFile)128129 assert.Equal(t, http.StatusOK, w.Code)130 assert.Empty(t, w.Body.String()) // HEAD request should not return body131 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))132 })133134 // Test with Range request135 t.Run("Range request", func(t *testing.T) {136 testFile := "testdata/test_file.txt"137138 w := httptest.NewRecorder()139 c, _ := CreateTestContext(w)140 c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)141 c.Request.Header.Set("Range", "bytes=0-10")142143 c.File(testFile)144145 assert.Equal(t, http.StatusPartialContent, w.Code)146 assert.Equal(t, "bytes", w.Header().Get("Accept-Ranges"))147 assert.Contains(t, w.Header().Get("Content-Range"), "bytes 0-10")148 })149}150151func TestContextFormFile(t *testing.T) {152 buf := new(bytes.Buffer)153 mw := multipart.NewWriter(buf)154 w, err := mw.CreateFormFile("file", "test")
3402 unmodified lines