|
| 1 | +package frankenphp |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "log/slog" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func newSpoolContext(body []byte) *frankenPHPContext { |
| 16 | + fc := newFrankenPHPContext() |
| 17 | + fc.logger = slog.Default() |
| 18 | + fc.request = httptest.NewRequest("POST", "/", bytes.NewReader(body)) |
| 19 | + |
| 20 | + return fc |
| 21 | +} |
| 22 | + |
| 23 | +// TestSpoolRequestBodyInMemory drains a small body into memory, leaving no temp |
| 24 | +// file to clean up. |
| 25 | +func TestSpoolRequestBodyInMemory(t *testing.T) { |
| 26 | + body := bytes.Repeat([]byte("a"), 1024) |
| 27 | + fc := newSpoolContext(body) |
| 28 | + |
| 29 | + fc.spoolRequestBody() |
| 30 | + |
| 31 | + assert.True(t, fc.bodySpooled) |
| 32 | + assert.Nil(t, fc.cleanupBody, "a small body stays in memory") |
| 33 | + assert.Equal(t, int64(len(body)), fc.request.ContentLength) |
| 34 | + |
| 35 | + got, err := io.ReadAll(fc.request.Body) |
| 36 | + require.NoError(t, err) |
| 37 | + assert.Equal(t, body, got) |
| 38 | +} |
| 39 | + |
| 40 | +// TestSpoolRequestBodyToFile spills a body larger than the memory threshold to a |
| 41 | +// temp file, serves identical bytes, and removes the file on cleanup. |
| 42 | +func TestSpoolRequestBodyToFile(t *testing.T) { |
| 43 | + body := bytes.Repeat([]byte("b"), bodySpoolMemoryThreshold+4096) |
| 44 | + fc := newSpoolContext(body) |
| 45 | + |
| 46 | + fc.spoolRequestBody() |
| 47 | + |
| 48 | + assert.True(t, fc.bodySpooled) |
| 49 | + require.NotNil(t, fc.cleanupBody, "a large body spills to a temp file") |
| 50 | + assert.Equal(t, int64(len(body)), fc.request.ContentLength) |
| 51 | + |
| 52 | + got, err := io.ReadAll(fc.request.Body) |
| 53 | + require.NoError(t, err) |
| 54 | + assert.Equal(t, body, got) |
| 55 | + |
| 56 | + fc.cleanupBody() |
| 57 | +} |
| 58 | + |
| 59 | +// TestSpoolRequestBodySkipsStreaming leaves a body of unknown length untouched so |
| 60 | +// long-lived streaming uploads keep their live stream. |
| 61 | +func TestSpoolRequestBodySkipsStreaming(t *testing.T) { |
| 62 | + fc := newSpoolContext([]byte("streamed")) |
| 63 | + fc.request.ContentLength = -1 |
| 64 | + |
| 65 | + fc.spoolRequestBody() |
| 66 | + |
| 67 | + assert.False(t, fc.bodySpooled) |
| 68 | + got, err := io.ReadAll(fc.request.Body) |
| 69 | + require.NoError(t, err) |
| 70 | + assert.Equal(t, "streamed", string(got)) |
| 71 | +} |
| 72 | + |
| 73 | +// TestSpoolRequestBodyIdempotent does not re-drain an already spooled body: a |
| 74 | +// second call must not touch the already consumed stream. |
| 75 | +func TestSpoolRequestBodyIdempotent(t *testing.T) { |
| 76 | + body := bytes.Repeat([]byte("c"), 512) |
| 77 | + fc := newSpoolContext(body) |
| 78 | + |
| 79 | + require.NoError(t, fc.spoolRequestBody()) |
| 80 | + require.NoError(t, fc.spoolRequestBody()) |
| 81 | + |
| 82 | + assert.True(t, fc.bodySpooled) |
| 83 | + got, err := io.ReadAll(fc.request.Body) |
| 84 | + require.NoError(t, err) |
| 85 | + assert.Equal(t, body, got) |
| 86 | +} |
| 87 | + |
| 88 | +// TestSpoolRequestBodyRejectsOversized rejects a body that overruns a |
| 89 | +// request_body max_size limit (an http.MaxBytesReader) with 413 instead of |
| 90 | +// feeding PHP a truncated request. |
| 91 | +func TestSpoolRequestBodyRejectsOversized(t *testing.T) { |
| 92 | + fc := newSpoolContext(bytes.Repeat([]byte("d"), 4096)) |
| 93 | + rec := httptest.NewRecorder() |
| 94 | + fc.responseWriter = rec |
| 95 | + fc.request.Body = http.MaxBytesReader(rec, fc.request.Body, 1024) |
| 96 | + |
| 97 | + err := fc.spoolRequestBody() |
| 98 | + |
| 99 | + require.ErrorIs(t, err, ErrRequestBodyTooLarge) |
| 100 | + assert.False(t, fc.bodySpooled) |
| 101 | + assert.Equal(t, http.StatusRequestEntityTooLarge, rec.Code) |
| 102 | +} |
0 commit comments