五处逐字同形的三行(mediafetch.sanitizeErr / httpclient.transportCause / vision.fetchCause / telegram.callCause / weixin_ilink.urlFreeCause)收敛为 trace 包里的唯一实现,13 个调用点改为直接调用;5 份 body 只剩 1 份,5 个 未导出符号与其文件内注释整份删除(不留薄封装)。 行为等价:只剥 *url.Error、不新增包装,内层哨兵链(context 取消/超时、 *net.DNSError 等)原样透传,因此重试/降级/熔断的分类判据不变。两处刻意的 收紧: - 逐层剥:内层若还是 *url.Error 也一起剥(只剥一层时内层仍带同一段地址); - *url.Error 的 Err 为 nil(或链自引用)时不再回落到原串,退化成只含 Op 的 替身 —— 那种错误无法在不带地址的前提下原样返回,而地址里就是凭据。 顺带把 httpclient 的 user:pass@ 用例改成变异可打红:net/http 自己在 *url.Error 里就把密码打成 ***,「密码不出现」在剥壳与不剥壳下都成立(空跑), 用户名才是分水岭。 验收:go build ./... + go vet ./... + 指定包 go test -count=1 全绿; 变异验证(把 URLCause 改成透传)⇒ 五处「不含 URL」断言全红。
181 lines
7.5 KiB
Go
181 lines
7.5 KiB
Go
package httpclient
|
||
|
||
// 本文件钉住本客户端的两条出镜边界(M41 #10/#11 那一族):
|
||
//
|
||
// - **非 2xx 的响应体原文不进错误**:旧实现把体截到 512B 拼进 error,而调用方
|
||
// (KOOK 适配器)会把这条 error 直接落日志 —— 上游在体里回显什么,日志里就有
|
||
// 什么。现在只留状态码、来源主机,以及信封的 code/message(已清洗、已截断)。
|
||
// - **请求地址的凭据不进错误**:target 可能带 `?access_token=…` 或 `user:pass@`,
|
||
// 而 net/http 的 *url.Error 会把整条地址拼进错误串。
|
||
//
|
||
// 每条用例都是「把它改回去就变红」的形状:断言的是**错误文本**,也就是调用方实际
|
||
// 打进日志的那段字符串。
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"net/url"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// failingTransport 让 http.Client.Do 以传输层错误收场。选定它而不是拨一个真端口,
|
||
// 是因为只有传输层失败才会让 net/http 包出 **带完整地址** 的 *url.Error ——
|
||
// 那正是要钉住的泄漏形态。
|
||
type failingTransport struct{}
|
||
|
||
func (failingTransport) RoundTrip(*http.Request) (*http.Response, error) {
|
||
return nil, errors.New("dial tcp: connection refused")
|
||
}
|
||
|
||
// TestDoJSON_StatusErrorDoesNotEchoBodySentinel 4xx 体里放哨兵 ⇒ 错误里搜不到。
|
||
//
|
||
// 用非信封体(代理返回的 HTML 错误页就是这样):信封类的体按设计会保留
|
||
// code/message,非信封体则一个字节都不留。
|
||
func TestDoJSON_StatusErrorDoesNotEchoBodySentinel(t *testing.T) {
|
||
t.Parallel()
|
||
const sentinel = "SENTINEL-4XX-BODY"
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
w.WriteHeader(http.StatusBadGateway)
|
||
_, _ = w.Write([]byte("<html><body>bad gateway " + sentinel + "</body></html>"))
|
||
}))
|
||
defer srv.Close()
|
||
|
||
c := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()), WithLogger(discardLogger()))
|
||
_, err := c.DoJSON(context.Background(), http.MethodPost, "/message/create", map[string]any{"a": 1})
|
||
if err == nil {
|
||
t.Fatal("502 却报成功")
|
||
}
|
||
if !errors.Is(err, ErrAPI) {
|
||
t.Fatalf("err = %v, want ErrAPI", err)
|
||
}
|
||
if strings.Contains(err.Error(), sentinel) {
|
||
t.Fatalf("响应体原文进了错误(会随调用方落日志):%v", err)
|
||
}
|
||
// 可诊断性不能一起丢:来源主机与状态码必须还在,且带得出「体有多大」。
|
||
host, _, ok := strings.Cut(strings.TrimPrefix(srv.URL, "http://"), ":")
|
||
if !ok || !strings.Contains(err.Error(), host) {
|
||
t.Fatalf("错误里没有来源主机 %q:%v", host, err)
|
||
}
|
||
if !strings.Contains(err.Error(), "status=502") {
|
||
t.Fatalf("错误里没有状态码:%v", err)
|
||
}
|
||
if !strings.Contains(err.Error(), "body_bytes=") {
|
||
t.Fatalf("错误里没有响应体体积(相关性判断要用):%v", err)
|
||
}
|
||
}
|
||
|
||
// TestDoJSON_StatusErrorKeepsEnvelopeCode 信封体仍要给出 code/message:这是本客户端
|
||
// 唯一认识的诊断面,替换掉体原文之后不能连它一起丢。
|
||
func TestDoJSON_StatusErrorKeepsEnvelopeCode(t *testing.T) {
|
||
t.Parallel()
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
w.WriteHeader(http.StatusUnauthorized)
|
||
_, _ = w.Write([]byte(`{"code":40101,"message":"invalid token","data":null}`))
|
||
}))
|
||
defer srv.Close()
|
||
|
||
c := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()), WithLogger(discardLogger()))
|
||
_, err := c.DoJSON(context.Background(), http.MethodGet, "/gateway/index", nil)
|
||
if err == nil {
|
||
t.Fatal("401 却报成功")
|
||
}
|
||
if !strings.Contains(err.Error(), "code 40101") || !strings.Contains(err.Error(), "invalid token") {
|
||
t.Fatalf("信封的 code/message 丢了:%v", err)
|
||
}
|
||
}
|
||
|
||
// TestDoJSON_NonZeroCodeRedactsMessageSecrets 信封 message 是上游自由文本面:里面的
|
||
// 凭据形状必须被清洗掉,长度必须被截断。改回原样透传就变红。
|
||
func TestDoJSON_NonZeroCodeRedactsMessageSecrets(t *testing.T) {
|
||
t.Parallel()
|
||
const secret = "big-secret-value-123456"
|
||
body := `{"code":40101,"message":"invalid access_token=` + secret + `","data":null}`
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
_, _ = w.Write([]byte(body))
|
||
}))
|
||
defer srv.Close()
|
||
|
||
c := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()), WithLogger(discardLogger()), WithToken(secret))
|
||
_, err := c.DoJSON(context.Background(), http.MethodGet, "/gateway/index", nil)
|
||
if err == nil {
|
||
t.Fatal("非零 code 却报成功")
|
||
}
|
||
if strings.Contains(err.Error(), secret) {
|
||
t.Fatalf("信封 message 里的凭据原样进了错误:%v", err)
|
||
}
|
||
}
|
||
|
||
// TestGetJSON_TransportErrorOmitsTargetCredentials 传输层失败时,错误里不得出现目标
|
||
// 地址(含 query 里的凭据)—— net/http 会把整条 URL 放在 *url.Error 里。
|
||
func TestGetJSON_TransportErrorOmitsTargetCredentials(t *testing.T) {
|
||
t.Parallel()
|
||
const secret = "QUERY-SECRET-VALUE"
|
||
c := New(
|
||
WithBaseURL("http://api.example.com"),
|
||
WithHTTPClient(&http.Client{Transport: failingTransport{}}),
|
||
WithLogger(discardLogger()),
|
||
)
|
||
_, err := c.GetJSON(context.Background(), "/gateway/index", url.Values{"access_token": {secret}})
|
||
if err == nil {
|
||
t.Fatal("传输层失败却报成功")
|
||
}
|
||
if strings.Contains(err.Error(), secret) {
|
||
t.Fatalf("目标地址的凭据进了错误:%v", err)
|
||
}
|
||
if !strings.Contains(err.Error(), "api.example.com") || !strings.Contains(err.Error(), "connection refused") {
|
||
t.Fatalf("剥掉地址时把来源或原因一起丢了:%v", err)
|
||
}
|
||
}
|
||
|
||
// TestGetJSON_UserinfoNeverEntersError 绝对地址带 user:pass@ 时同样不得出镜。
|
||
//
|
||
// 断言用户名而不只是密码:net/http 自己在 *url.Error 里就把密码打成 xxxxx,因此
|
||
// 「密码不出现」在剥壳与不剥壳两种实现下都成立(这样的用例是空跑);用户名才是
|
||
// 剥壳与不剥壳的分水岭。
|
||
func TestGetJSON_UserinfoNeverEntersError(t *testing.T) {
|
||
t.Parallel()
|
||
c := New(
|
||
WithHTTPClient(&http.Client{Transport: failingTransport{}}),
|
||
WithLogger(discardLogger()),
|
||
)
|
||
_, err := c.GetJSON(context.Background(), "https://leakuser:leakpass@api.example.com/x", nil)
|
||
if err == nil {
|
||
t.Fatal("传输层失败却报成功")
|
||
}
|
||
msg := err.Error()
|
||
if strings.Contains(msg, "leakuser") || strings.Contains(msg, "leakpass") {
|
||
t.Fatalf("user:pass@ 进了错误:%v", msg)
|
||
}
|
||
if !strings.Contains(msg, "api.example.com") {
|
||
t.Fatalf("剥掉地址时把来源一起丢了:%v", msg)
|
||
}
|
||
}
|
||
|
||
// TestDoJSON_EnvelopeMessageIsBoundedByRedactText 钉住长度口径:信封 message 只有一道
|
||
// 清洗(trace.RedactText,80 rune + 省略号)。本片曾按 200 rune 自己再截一刀,那层
|
||
// 永远命中不了 —— 这条用例让「到底哪一层在截」变成可断言的事实。
|
||
func TestDoJSON_EnvelopeMessageIsBoundedByRedactText(t *testing.T) {
|
||
t.Parallel()
|
||
long := strings.Repeat("a", 300)
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
w.WriteHeader(http.StatusBadRequest)
|
||
_, _ = w.Write([]byte(`{"code":40101,"message":"` + long + `","data":null}`))
|
||
}))
|
||
defer srv.Close()
|
||
|
||
c := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()), WithLogger(discardLogger()))
|
||
_, err := c.DoJSON(context.Background(), http.MethodGet, "/gateway/index", nil)
|
||
if err == nil {
|
||
t.Fatal("401 却报成功")
|
||
}
|
||
if !strings.Contains(err.Error(), strings.Repeat("a", 80)+"…") {
|
||
t.Fatalf("信封 message 没有按 80 rune 截断:%v", err)
|
||
}
|
||
if strings.Contains(err.Error(), strings.Repeat("a", 81)) {
|
||
t.Fatalf("信封 message 超过了 80 rune 上限:%v", err)
|
||
}
|
||
}
|