|
| 1 | +package httpserver |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func TestUploadOverSandboxLimit(t *testing.T) { |
| 14 | + dir := t.TempDir() |
| 15 | + srv, err := New(&Options{Folder: dir, EnableUpload: true, Sandbox: true, MaxFileSize: 1}) |
| 16 | + if err != nil { |
| 17 | + t.Fatal(err) |
| 18 | + } |
| 19 | + ts := httptest.NewServer(srv.uploadlayer(http.NotFoundHandler())) |
| 20 | + defer ts.Close() |
| 21 | + |
| 22 | + body := bytes.Repeat([]byte("a"), 2*1024*1024) |
| 23 | + req, err := http.NewRequest(http.MethodPut, ts.URL+"/big.bin", bytes.NewReader(body)) |
| 24 | + if err != nil { |
| 25 | + t.Fatal(err) |
| 26 | + } |
| 27 | + resp, err := http.DefaultClient.Do(req) |
| 28 | + if err != nil { |
| 29 | + t.Fatal(err) |
| 30 | + } |
| 31 | + _ = resp.Body.Close() |
| 32 | + if resp.StatusCode != http.StatusRequestEntityTooLarge { |
| 33 | + t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusRequestEntityTooLarge) |
| 34 | + } |
| 35 | + if _, err := os.Stat(filepath.Join(dir, "big.bin")); err == nil { |
| 36 | + t.Error("oversized upload was written") |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestUploadChunkedOverSandboxLimit(t *testing.T) { |
| 41 | + dir := t.TempDir() |
| 42 | + srv, err := New(&Options{Folder: dir, EnableUpload: true, Sandbox: true, MaxFileSize: 1}) |
| 43 | + if err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + ts := httptest.NewServer(srv.uploadlayer(http.NotFoundHandler())) |
| 47 | + defer ts.Close() |
| 48 | + |
| 49 | + body := io.MultiReader(bytes.NewReader(bytes.Repeat([]byte("a"), 2*1024*1024))) |
| 50 | + req, err := http.NewRequest(http.MethodPut, ts.URL+"/big.bin", body) |
| 51 | + if err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + resp, err := http.DefaultClient.Do(req) |
| 55 | + if err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + _ = resp.Body.Close() |
| 59 | + if resp.StatusCode != http.StatusRequestEntityTooLarge { |
| 60 | + t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusRequestEntityTooLarge) |
| 61 | + } |
| 62 | +} |
0 commit comments