Home / Function/ TestContextFile() — gin Function Reference

TestContextFile() — gin Function Reference

Architecture documentation for the TestContextFile() function in context_test.go from the gin codebase.

Entity Profile

Dependency Diagram

graph TD
  4cb1672a_9d57_e61f_21f5_b606f0e80d84["TestContextFile()"]
  ebe0ae48_a62b_a38f_5bac_5bbbd96fc508["context_test.go"]
  4cb1672a_9d57_e61f_21f5_b606f0e80d84 -->|defined in| ebe0ae48_a62b_a38f_5bac_5bbbd96fc508
  style 4cb1672a_9d57_e61f_21f5_b606f0e80d84 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

context_test.go lines 80–150

func TestContextFile(t *testing.T) {
	// Test serving an existing file
	t.Run("serve existing file", func(t *testing.T) {
		// Create a temporary test file
		testFile := "testdata/test_file.txt"

		w := httptest.NewRecorder()
		c, _ := CreateTestContext(w)
		c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)

		c.File(testFile)

		assert.Equal(t, http.StatusOK, w.Code)
		assert.Contains(t, w.Body.String(), "This is a test file")
		assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
	})

	// Test serving a non-existent file
	t.Run("serve non-existent file", func(t *testing.T) {
		w := httptest.NewRecorder()
		c, _ := CreateTestContext(w)
		c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)

		c.File("non_existent_file.txt")

		assert.Equal(t, http.StatusNotFound, w.Code)
	})

	// Test serving a directory (should return 200 with directory listing or 403 Forbidden)
	t.Run("serve directory", func(t *testing.T) {
		w := httptest.NewRecorder()
		c, _ := CreateTestContext(w)
		c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)

		c.File(".")

		// Directory serving can return either 200 (with listing) or 403 (forbidden)
		assert.True(t, w.Code == http.StatusOK || w.Code == http.StatusForbidden)
	})

	// Test with HEAD request
	t.Run("HEAD request", func(t *testing.T) {
		testFile := "testdata/test_file.txt"

		w := httptest.NewRecorder()
		c, _ := CreateTestContext(w)
		c.Request = httptest.NewRequest(http.MethodHead, "/test", nil)

		c.File(testFile)

		assert.Equal(t, http.StatusOK, w.Code)
		assert.Empty(t, w.Body.String()) // HEAD request should not return body
		assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
	})

	// Test with Range request
	t.Run("Range request", func(t *testing.T) {
		testFile := "testdata/test_file.txt"

		w := httptest.NewRecorder()
		c, _ := CreateTestContext(w)
		c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
		c.Request.Header.Set("Range", "bytes=0-10")

		c.File(testFile)

		assert.Equal(t, http.StatusPartialContent, w.Code)
		assert.Equal(t, "bytes", w.Header().Get("Accept-Ranges"))
		assert.Contains(t, w.Header().Get("Content-Range"), "bytes 0-10")
	})
}

Domain

Subdomains

Defined In

Frequently Asked Questions

What does TestContextFile() do?
TestContextFile() is a function in the gin codebase, defined in context_test.go.
Where is TestContextFile() defined?
TestContextFile() is defined in context_test.go at line 80.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free