From ed2f2ef0d3c0efd1c2340176377998be517a1438 Mon Sep 17 00:00:00 2001 From: caixw Date: Fri, 21 Jul 2023 13:25:02 +0800 Subject: [PATCH] =?UTF-8?q?fix(rest):=20RawHTTP=20=E5=B0=86=E5=BF=BD?= =?UTF-8?q?=E7=95=A5=20HOST=20=E6=8A=A5=E5=A4=B4=E5=8F=AA=E4=BB=8E?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=9C=B0=E5=9D=80=E8=8E=B7=E5=8F=96=20HOST?= =?UTF-8?q?=20=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/golang/go/issues/61076 添加了对 HOST 的验证, 原有的实现不再可行。 --- rest/rest.go | 15 +++++++++------ rest/rest_test.go | 10 ++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/rest/rest.go b/rest/rest.go index b8312d0..f548d45 100644 --- a/rest/rest.go +++ b/rest/rest.go @@ -9,7 +9,6 @@ import ( "io" "net/http" "net/http/httptest" - "net/url" "github.com/issue9/assert/v3" ) @@ -48,7 +47,14 @@ func (srv *Server) RawHTTP(req, resp string) *Server { // RawHTTP 通过原始数据进行比较请求和返回数据是符合要求 // -// reqRaw 表示原始的请求数据; +// reqRaw 表示原始的请求数据。其格式如下: +// +// POST https://example.com/path HTTP/1.1 +// +// text +// +// 会忽略 HOST 报头,而是应该将主机部分直接写在请求地址中。 +// // respRaw 表示返回之后的原始数据; // // NOTE: 仅判断状态码、报头和实际内容是否相同,而不是直接比较两个 http.Response 的值。 @@ -98,10 +104,7 @@ func readRaw(a *assert.Assertion, reqRaw, respRaw string) (*http.Request, *http. r, err := http.ReadRequest(bufio.NewReader(bytes.NewBufferString(reqRaw))) a.NotError(err).NotNil(r) - u, err := url.Parse(r.Host + r.URL.String()) - a.NotError(err).NotNil(u) - r.RequestURI = "" - r.URL = u + r.RequestURI = "" // 作为 client 不需要此值 return r, resp } diff --git a/rest/rest_test.go b/rest/rest_test.go index ba1e431..efb358a 100644 --- a/rest/rest_test.go +++ b/rest/rest_test.go @@ -106,8 +106,7 @@ var raw = []*struct { req, resp string }{ { - req: `GET /get HTTP/1.1 -Host: {host} + req: `GET {host}/get HTTP/1.1 `, resp: `HTTP/1.1 201 @@ -115,8 +114,8 @@ Host: {host} `, }, { - req: `POST /body HTTP/1.1 -Host: {host} + req: `POST {host}/body HTTP/1.1 +Host: 这行将被忽略 Content-Type: application/json Content-Length: 8 @@ -131,8 +130,7 @@ Content-Type: application/json;charset=utf-8 `, }, { - req: `DELETE /body?page=5 HTTP/1.0 -Host: {host} + req: `DELETE {host}/body?page=5 HTTP/1.0 Content-Type: application/xml Content-Length: 23