Files
MeBox/internal/helper/http_test.go
T
truewhile 2b2dc697ce bug处理
2026-10-08 11:13:10 +08:00

86 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package helper
import (
"bytes"
"compress/gzip"
"net/http"
"testing"
)
// 本文件:压缩相关的回归测试。
//
// 背景:预设头里曾显式写 `Accept-Encoding: gzip, deflate, br`,导致 net/http
// 不再自动解压(它只在调用方没设置该头时才解压),压缩字节直接进入规则层。
// TestHeaderPresetsAcceptIsWildcard 预设 Accept 不得首选 text/html。
//
// 背景:DRF(Django REST framework)等会做内容协商的后端,在 Accept 首选
// text/html 时返回「可浏览 HTML 页面」而不是 JSON,书源 JSONPath 因此永远
// 解析出 0 条(拷贝轻小说源在 MeBox 搜不到、在阅读 App 能搜到的根因)。
func TestHeaderPresetsAcceptIsWildcard(t *testing.T) {
got := HTTPHeaderPresets()["Accept"]
if got != "*/*" {
t.Fatalf("Accept 预设应为 */*(对齐 legado/OkHttp),实际 %q", got)
}
}
// TestHeaderPresetsDoNotSetAcceptEncoding 预设头不得设置 Accept-Encoding。
// 一旦设置,net/http 的透明解压就失效,压缩响应会以原始字节交给上层。
func TestHeaderPresetsDoNotSetAcceptEncoding(t *testing.T) {
for k := range HTTPHeaderPresets() {
if http.CanonicalHeaderKey(k) == "Accept-Encoding" {
t.Fatal("HTTPHeaderPresets 不应设置 Accept-Encoding(会让 net/http 放弃自动解压)")
}
}
}
// TestStripAcceptEncoding 任何来源带来的 Accept-Encoding 都应被清掉。
func TestStripAcceptEncoding(t *testing.T) {
h := http.Header{}
h.Set("Accept-Encoding", "br, gzip")
StripAcceptEncoding(h)
if got := h.Get("Accept-Encoding"); got != "" {
t.Fatalf("Accept-Encoding 未清理: %q", got)
}
}
// TestDecompressBody gzip / deflate 响应体应被还原成明文。
func TestDecompressBody(t *testing.T) {
payload := `{"version":"20260926"}`
t.Run("gzip", func(t *testing.T) {
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
_, _ = gz.Write([]byte(payload))
_ = gz.Close()
resp := &http.Response{Header: http.Header{}}
resp.Header.Set("Content-Encoding", "gzip")
if got := string(DecompressBody(resp, buf.Bytes())); got != payload {
t.Fatalf("gzip 解压 = %q", got)
}
})
t.Run("未压缩原样返回", func(t *testing.T) {
resp := &http.Response{Header: http.Header{}}
if got := string(DecompressBody(resp, []byte(payload))); got != payload {
t.Fatalf("未压缩响应被改动: %q", got)
}
})
t.Run("坏数据不 panic", func(t *testing.T) {
resp := &http.Response{Header: http.Header{}}
resp.Header.Set("Content-Encoding", "gzip")
raw := []byte("not-gzip")
if got := string(DecompressBody(resp, raw)); got != "not-gzip" {
t.Fatalf("解压失败时应原样返回,实际 %q", got)
}
})
t.Run("nil resp", func(t *testing.T) {
if got := string(DecompressBody(nil, []byte(payload))); got != payload {
t.Fatalf("nil resp 应原样返回,实际 %q", got)
}
})
}