Skip to content
Myr

context_file_test.go

This file

Path

context_file_test.go

35 lines · 933 bytes · Go

Last changed by

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

History of this file

Take it

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}