mirror of
https://github.com/miniflux/v2.git
synced 2025-08-26 18:21:01 +00:00
refactor(response): simplify switch-case and remove unnecessary defer
- b.body can never be of type error, so let's remove it from the switch-case construct. - there is no need to use defer when the only return statement is two lines after.
This commit is contained in:
parent
7c29166ef9
commit
3acb888309
2 changed files with 3 additions and 27 deletions
|
@ -84,8 +84,6 @@ func (b *Builder) Write() {
|
||||||
b.compress(v)
|
b.compress(v)
|
||||||
case string:
|
case string:
|
||||||
b.compress([]byte(v))
|
b.compress([]byte(v))
|
||||||
case error:
|
|
||||||
b.compress([]byte(v.Error()))
|
|
||||||
case io.Reader:
|
case io.Reader:
|
||||||
// Compression not implemented in this case
|
// Compression not implemented in this case
|
||||||
b.writeHeaders()
|
b.writeHeaders()
|
||||||
|
@ -117,24 +115,24 @@ func (b *Builder) compress(data []byte) {
|
||||||
b.writeHeaders()
|
b.writeHeaders()
|
||||||
|
|
||||||
brotliWriter := brotli.NewWriterV2(b.w, brotli.DefaultCompression)
|
brotliWriter := brotli.NewWriterV2(b.w, brotli.DefaultCompression)
|
||||||
defer brotliWriter.Close()
|
|
||||||
brotliWriter.Write(data)
|
brotliWriter.Write(data)
|
||||||
|
brotliWriter.Close()
|
||||||
return
|
return
|
||||||
case strings.Contains(acceptEncoding, "gzip"):
|
case strings.Contains(acceptEncoding, "gzip"):
|
||||||
b.headers["Content-Encoding"] = "gzip"
|
b.headers["Content-Encoding"] = "gzip"
|
||||||
b.writeHeaders()
|
b.writeHeaders()
|
||||||
|
|
||||||
gzipWriter := gzip.NewWriter(b.w)
|
gzipWriter := gzip.NewWriter(b.w)
|
||||||
defer gzipWriter.Close()
|
|
||||||
gzipWriter.Write(data)
|
gzipWriter.Write(data)
|
||||||
|
gzipWriter.Close()
|
||||||
return
|
return
|
||||||
case strings.Contains(acceptEncoding, "deflate"):
|
case strings.Contains(acceptEncoding, "deflate"):
|
||||||
b.headers["Content-Encoding"] = "deflate"
|
b.headers["Content-Encoding"] = "deflate"
|
||||||
b.writeHeaders()
|
b.writeHeaders()
|
||||||
|
|
||||||
flateWriter, _ := flate.NewWriter(b.w, -1)
|
flateWriter, _ := flate.NewWriter(b.w, -1)
|
||||||
defer flateWriter.Close()
|
|
||||||
flateWriter.Write(data)
|
flateWriter.Write(data)
|
||||||
|
flateWriter.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
package response // import "miniflux.app/v2/internal/http/response"
|
package response // import "miniflux.app/v2/internal/http/response"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -105,27 +104,6 @@ func TestBuildResponseWithAttachment(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildResponseWithError(t *testing.T) {
|
|
||||||
r, err := http.NewRequest("GET", "/", nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
|
|
||||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
New(w, r).WithBody(errors.New("Some error")).Write()
|
|
||||||
})
|
|
||||||
|
|
||||||
handler.ServeHTTP(w, r)
|
|
||||||
|
|
||||||
expectedBody := `Some error`
|
|
||||||
actualBody := w.Body.String()
|
|
||||||
if actualBody != expectedBody {
|
|
||||||
t.Fatalf(`Unexpected body, got %s instead of %s`, actualBody, expectedBody)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBuildResponseWithByteBody(t *testing.T) {
|
func TestBuildResponseWithByteBody(t *testing.T) {
|
||||||
r, err := http.NewRequest("GET", "/", nil)
|
r, err := http.NewRequest("GET", "/", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue