|
| 1 | +package echo |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "testing/iotest" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +// mockReadFromWriter implements io.ReaderFrom to trigger the optimization path |
| 17 | +type mockReadFromWriter struct { |
| 18 | + *httptest.ResponseRecorder |
| 19 | + readFromCalled bool |
| 20 | +} |
| 21 | + |
| 22 | +func (m *mockReadFromWriter) ReadFrom(r io.Reader) (int64, error) { |
| 23 | + m.readFromCalled = true |
| 24 | + // Simulate sendfile/optimized copy |
| 25 | + return io.Copy(m.ResponseRecorder, r) |
| 26 | +} |
| 27 | + |
| 28 | +// mockSimpleResponseWriter ONLY implements http.ResponseWriter (no ReadFrom) |
| 29 | +// This is used as a control group to force the original non-optimized path. |
| 30 | +type mockSimpleResponseWriter struct { |
| 31 | + *httptest.ResponseRecorder |
| 32 | +} |
| 33 | + |
| 34 | +const readFromTestFile = "readfrom_test_data.txt" |
| 35 | + |
| 36 | +func TestContext_File_ReadFrom_Optimization(t *testing.T) { |
| 37 | + e := New() |
| 38 | + tmpDir := t.TempDir() |
| 39 | + content := "hello optimization parity check content" |
| 40 | + err := os.WriteFile(filepath.Join(tmpDir, readFromTestFile), []byte(content), 0644) |
| 41 | + assert.NoError(t, err) |
| 42 | + e.Filesystem = os.DirFS(tmpDir) |
| 43 | + |
| 44 | + t.Run("Verify optimization triggers and parity", func(t *testing.T) { |
| 45 | + // Use e.NewContext and c.File for end-to-end functional parity check. |
| 46 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 47 | + |
| 48 | + // 1. Optimized Path Group |
| 49 | + recOpt := httptest.NewRecorder() |
| 50 | + mwOpt := &mockReadFromWriter{ResponseRecorder: recOpt} |
| 51 | + cOpt := e.NewContext(req, mwOpt) |
| 52 | + assert.NoError(t, cOpt.File(readFromTestFile)) |
| 53 | + resOpt := cOpt.Response().(*Response) |
| 54 | + |
| 55 | + // 2. Original Path Group (Control) |
| 56 | + recOri := httptest.NewRecorder() |
| 57 | + mwOri := &mockSimpleResponseWriter{ResponseRecorder: recOri} |
| 58 | + cOri := e.NewContext(req, mwOri) |
| 59 | + assert.NoError(t, cOri.File(readFromTestFile)) |
| 60 | + resOri := cOri.Response().(*Response) |
| 61 | + |
| 62 | + // ASSERTIONS: |
| 63 | + assert.True(t, mwOpt.readFromCalled, "Optimized path MUST trigger ReadFrom") |
| 64 | + assert.Equal(t, recOri.Code, recOpt.Code, "httptest.Recorder Code parity") |
| 65 | + assert.Equal(t, recOri.Body.String(), recOpt.Body.String(), "Body content parity") |
| 66 | + |
| 67 | + // Echo Response State Parity |
| 68 | + assert.Equal(t, resOri.Status, resOpt.Status, "Response.Status parity") |
| 69 | + assert.Equal(t, resOri.Size, resOpt.Size, "Response.Size parity") |
| 70 | + assert.Equal(t, resOri.Committed, resOpt.Committed, "Response.Committed parity") |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("ReadFrom: Custom Status already set", func(t *testing.T) { |
| 74 | + // Manually construct the wrapper to bypass http.ServeContent's side effects |
| 75 | + // and surgically verify the Status/Before-hook bridging logic in ReadFrom. |
| 76 | + rec := httptest.NewRecorder() |
| 77 | + mw := &mockReadFromWriter{ResponseRecorder: rec} |
| 78 | + res := &Response{ResponseWriter: mw} |
| 79 | + w := &responseWithReadFrom{res} |
| 80 | + |
| 81 | + res.Status = http.StatusCreated |
| 82 | + n, err := w.ReadFrom(strings.NewReader("test data")) |
| 83 | + assert.NoError(t, err) |
| 84 | + assert.Equal(t, int64(9), n) |
| 85 | + assert.Equal(t, http.StatusCreated, rec.Code) |
| 86 | + assert.True(t, res.Committed) |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("ReadFrom: Already committed", func(t *testing.T) { |
| 90 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 91 | + rec := httptest.NewRecorder() |
| 92 | + mw := &mockReadFromWriter{ResponseRecorder: rec} |
| 93 | + c := e.NewContext(req, mw) |
| 94 | + |
| 95 | + c.Response().WriteHeader(http.StatusAccepted) // Commit here |
| 96 | + assert.NoError(t, c.File(readFromTestFile)) |
| 97 | + |
| 98 | + assert.True(t, mw.readFromCalled) |
| 99 | + assert.Equal(t, http.StatusAccepted, rec.Code) |
| 100 | + // Body should still be written because ServeContent continues after WriteHeader |
| 101 | + assert.Contains(t, rec.Body.String(), "hello optimization") |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("ReadFrom: IO Error during Copy", func(t *testing.T) { |
| 105 | + // Directly test the wrapper to verify state updates (Size, Committed) |
| 106 | + // when an error occurs during the transfer. |
| 107 | + errReader := iotest.ErrReader(io.ErrUnexpectedEOF) |
| 108 | + |
| 109 | + res := &Response{ResponseWriter: &mockReadFromWriter{ResponseRecorder: httptest.NewRecorder()}} |
| 110 | + w := &responseWithReadFrom{res} |
| 111 | + |
| 112 | + n, err := w.ReadFrom(errReader) |
| 113 | + assert.ErrorIs(t, err, io.ErrUnexpectedEOF) |
| 114 | + assert.Equal(t, int64(0), n) |
| 115 | + assert.Equal(t, int64(0), res.Size) |
| 116 | + assert.True(t, res.Committed) |
| 117 | + }) |
| 118 | + |
| 119 | + t.Run("Hook Compatibility: Before hook triggers on ReadFrom", func(t *testing.T) { |
| 120 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 121 | + rec := httptest.NewRecorder() |
| 122 | + mw := &mockReadFromWriter{ResponseRecorder: rec} |
| 123 | + c := e.NewContext(req, mw) |
| 124 | + |
| 125 | + beforeTriggered := false |
| 126 | + c.Response().(*Response).Before(func() { |
| 127 | + beforeTriggered = true |
| 128 | + }) |
| 129 | + |
| 130 | + assert.NoError(t, c.File(readFromTestFile)) |
| 131 | + assert.True(t, mw.readFromCalled) |
| 132 | + assert.True(t, beforeTriggered, "Before hook must be called even on ReadFrom path") |
| 133 | + }) |
| 134 | + |
| 135 | + t.Run("Hook Compatibility: After hook disables ReadFrom", func(t *testing.T) { |
| 136 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 137 | + rec := httptest.NewRecorder() |
| 138 | + mw := &mockReadFromWriter{ResponseRecorder: rec} |
| 139 | + c := e.NewContext(req, mw) |
| 140 | + |
| 141 | + afterCalls := 0 |
| 142 | + c.Response().(*Response).After(func() { |
| 143 | + afterCalls++ |
| 144 | + }) |
| 145 | + |
| 146 | + assert.NoError(t, c.File(readFromTestFile)) |
| 147 | + assert.False(t, mw.readFromCalled, "ReadFrom must be DISABLED when After hooks exist") |
| 148 | + assert.True(t, afterCalls > 0, "After hooks must be triggered via standard Write path") |
| 149 | + }) |
| 150 | + |
| 151 | + t.Run("Error Parity: 416 Invalid Range", func(t *testing.T) { |
| 152 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 153 | + req.Header.Set("Range", "bytes=100-200") |
| 154 | + |
| 155 | + rec := httptest.NewRecorder() |
| 156 | + mw := &mockReadFromWriter{ResponseRecorder: rec} |
| 157 | + c := e.NewContext(req, mw) |
| 158 | + |
| 159 | + assert.NoError(t, c.File(readFromTestFile)) |
| 160 | + assert.Equal(t, http.StatusRequestedRangeNotSatisfiable, rec.Code) |
| 161 | + assert.Equal(t, http.StatusRequestedRangeNotSatisfiable, c.Response().(*Response).Status) |
| 162 | + }) |
| 163 | +} |
0 commit comments