|
| 1 | +// Copyright 2023 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package httplib |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +func TestServeContentByReader(t *testing.T) { |
| 19 | + data := "0123456789abcdef" |
| 20 | + |
| 21 | + test := func(t *testing.T, expectedStatusCode int, expectedContent string) { |
| 22 | + _, rangeStr, _ := strings.Cut(t.Name(), "_range_") |
| 23 | + r := &http.Request{Header: http.Header{}, Form: url.Values{}} |
| 24 | + if rangeStr != "" { |
| 25 | + r.Header.Set("Range", fmt.Sprintf("bytes=%s", rangeStr)) |
| 26 | + } |
| 27 | + reader := strings.NewReader(data) |
| 28 | + w := NewMockResponseWriter() |
| 29 | + err := ServeContentByReader(r, w, "test", int64(len(data)), reader) |
| 30 | + assert.Equal(t, expectedStatusCode, w.StatusCode) |
| 31 | + if expectedStatusCode == http.StatusPartialContent || expectedStatusCode == http.StatusOK { |
| 32 | + assert.Equal(t, fmt.Sprint(len(expectedContent)), w.Header().Get("Content-Length")) |
| 33 | + assert.Equal(t, expectedContent, w.BodyBuffer.String()) |
| 34 | + } else { |
| 35 | + assert.Error(t, err) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + t.Run("_range_", func(t *testing.T) { |
| 40 | + test(t, http.StatusOK, data) |
| 41 | + }) |
| 42 | + t.Run("_range_0-", func(t *testing.T) { |
| 43 | + test(t, http.StatusPartialContent, data) |
| 44 | + }) |
| 45 | + t.Run("_range_0-15", func(t *testing.T) { |
| 46 | + test(t, http.StatusPartialContent, data) |
| 47 | + }) |
| 48 | + t.Run("_range_1-", func(t *testing.T) { |
| 49 | + test(t, http.StatusPartialContent, data[1:]) |
| 50 | + }) |
| 51 | + t.Run("_range_1-3", func(t *testing.T) { |
| 52 | + test(t, http.StatusPartialContent, data[1:3+1]) |
| 53 | + }) |
| 54 | + t.Run("_range_16-", func(t *testing.T) { |
| 55 | + test(t, http.StatusRequestedRangeNotSatisfiable, "") |
| 56 | + }) |
| 57 | + t.Run("_range_1-99999", func(t *testing.T) { |
| 58 | + test(t, http.StatusPartialContent, data[1:]) |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func TestServeContentByReadSeeker(t *testing.T) { |
| 63 | + data := "0123456789abcdef" |
| 64 | + tmpFile := t.TempDir() + "/test" |
| 65 | + err := os.WriteFile(tmpFile, []byte(data), 0o644) |
| 66 | + assert.NoError(t, err) |
| 67 | + |
| 68 | + test := func(t *testing.T, expectedStatusCode int, expectedContent string) { |
| 69 | + _, rangeStr, _ := strings.Cut(t.Name(), "_range_") |
| 70 | + r := &http.Request{Header: http.Header{}, Form: url.Values{}} |
| 71 | + if rangeStr != "" { |
| 72 | + r.Header.Set("Range", fmt.Sprintf("bytes=%s", rangeStr)) |
| 73 | + } |
| 74 | + |
| 75 | + seekReader, err := os.OpenFile(tmpFile, os.O_RDONLY, 0o644) |
| 76 | + if !assert.NoError(t, err) { |
| 77 | + return |
| 78 | + } |
| 79 | + defer seekReader.Close() |
| 80 | + |
| 81 | + w := NewMockResponseWriter() |
| 82 | + _ = ServeContentByReadSeeker(r, w, "test", time.Time{}, seekReader) |
| 83 | + assert.Equal(t, expectedStatusCode, w.StatusCode) |
| 84 | + if expectedStatusCode == http.StatusPartialContent || expectedStatusCode == http.StatusOK { |
| 85 | + assert.Equal(t, fmt.Sprint(len(expectedContent)), w.Header().Get("Content-Length")) |
| 86 | + assert.Equal(t, expectedContent, w.BodyBuffer.String()) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + t.Run("_range_", func(t *testing.T) { |
| 91 | + test(t, http.StatusOK, data) |
| 92 | + }) |
| 93 | + t.Run("_range_0-", func(t *testing.T) { |
| 94 | + test(t, http.StatusPartialContent, data) |
| 95 | + }) |
| 96 | + t.Run("_range_0-15", func(t *testing.T) { |
| 97 | + test(t, http.StatusPartialContent, data) |
| 98 | + }) |
| 99 | + t.Run("_range_1-", func(t *testing.T) { |
| 100 | + test(t, http.StatusPartialContent, data[1:]) |
| 101 | + }) |
| 102 | + t.Run("_range_1-3", func(t *testing.T) { |
| 103 | + test(t, http.StatusPartialContent, data[1:3+1]) |
| 104 | + }) |
| 105 | + t.Run("_range_16-", func(t *testing.T) { |
| 106 | + test(t, http.StatusRequestedRangeNotSatisfiable, "") |
| 107 | + }) |
| 108 | + t.Run("_range_1-99999", func(t *testing.T) { |
| 109 | + test(t, http.StatusPartialContent, data[1:]) |
| 110 | + }) |
| 111 | +} |
0 commit comments