aboutsummaryrefslogtreecommitdiffstats
path: root/modules/httplib
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-05-13 22:04:57 +0800
committerGitHub <noreply@github.com>2023-05-13 16:04:57 +0200
commita94a8d0ab1f6c9d63ae607dfec866c47ae3e4b5c (patch)
tree84c1e5e4f3423ed952b573ab1403844de2951cb7 /modules/httplib
parentf74501609246646a3ab62b37a1e7469d6fea63b7 (diff)
downloadgitea-a94a8d0ab1f6c9d63ae607dfec866c47ae3e4b5c.tar.gz
gitea-a94a8d0ab1f6c9d63ae607dfec866c47ae3e4b5c.zip
Use standard HTTP library to serve files (#24693)
`http.ServeFile/ServeContent` handles `If-xxx`, `Content-Length`, `Range` and `Etag` correctly After this PR, storage files (eg: avatar) could be responded with correct Content-Length.
Diffstat (limited to 'modules/httplib')
-rw-r--r--modules/httplib/mock.go35
-rw-r--r--modules/httplib/serve_test.go13
2 files changed, 7 insertions, 41 deletions
diff --git a/modules/httplib/mock.go b/modules/httplib/mock.go
deleted file mode 100644
index 7d284e86fb..0000000000
--- a/modules/httplib/mock.go
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright 2023 The Gitea Authors. All rights reserved.
-// SPDX-License-Identifier: MIT
-
-package httplib
-
-import (
- "bytes"
- "net/http"
-)
-
-type MockResponseWriter struct {
- header http.Header
-
- StatusCode int
- BodyBuffer bytes.Buffer
-}
-
-func (m *MockResponseWriter) Header() http.Header {
- return m.header
-}
-
-func (m *MockResponseWriter) Write(bytes []byte) (int, error) {
- if m.StatusCode == 0 {
- m.StatusCode = http.StatusOK
- }
- return m.BodyBuffer.Write(bytes)
-}
-
-func (m *MockResponseWriter) WriteHeader(statusCode int) {
- m.StatusCode = statusCode
-}
-
-func NewMockResponseWriter() *MockResponseWriter {
- return &MockResponseWriter{header: http.Header{}}
-}
diff --git a/modules/httplib/serve_test.go b/modules/httplib/serve_test.go
index 0768f1c713..fed4611d21 100644
--- a/modules/httplib/serve_test.go
+++ b/modules/httplib/serve_test.go
@@ -6,6 +6,7 @@ package httplib
import (
"fmt"
"net/http"
+ "net/http/httptest"
"net/url"
"os"
"strings"
@@ -25,12 +26,12 @@ func TestServeContentByReader(t *testing.T) {
r.Header.Set("Range", fmt.Sprintf("bytes=%s", rangeStr))
}
reader := strings.NewReader(data)
- w := NewMockResponseWriter()
+ w := httptest.NewRecorder()
ServeContentByReader(r, w, "test", int64(len(data)), reader)
- assert.Equal(t, expectedStatusCode, w.StatusCode)
+ assert.Equal(t, expectedStatusCode, w.Code)
if expectedStatusCode == http.StatusPartialContent || expectedStatusCode == http.StatusOK {
assert.Equal(t, fmt.Sprint(len(expectedContent)), w.Header().Get("Content-Length"))
- assert.Equal(t, expectedContent, w.BodyBuffer.String())
+ assert.Equal(t, expectedContent, w.Body.String())
}
}
@@ -76,12 +77,12 @@ func TestServeContentByReadSeeker(t *testing.T) {
}
defer seekReader.Close()
- w := NewMockResponseWriter()
+ w := httptest.NewRecorder()
ServeContentByReadSeeker(r, w, "test", time.Time{}, seekReader)
- assert.Equal(t, expectedStatusCode, w.StatusCode)
+ assert.Equal(t, expectedStatusCode, w.Code)
if expectedStatusCode == http.StatusPartialContent || expectedStatusCode == http.StatusOK {
assert.Equal(t, fmt.Sprint(len(expectedContent)), w.Header().Get("Content-Length"))
- assert.Equal(t, expectedContent, w.BodyBuffer.String())
+ assert.Equal(t, expectedContent, w.Body.String())
}
}