Skip to content

Commit

Permalink
fix(rest): RawHTTP 将忽略 HOST 报头只从请求地址获取 HOST 内容
Browse files Browse the repository at this point in the history
golang/go#61076 添加了对 HOST 的验证,
原有的实现不再可行。
  • Loading branch information
caixw committed Jul 21, 2023
1 parent 383ab1e commit ed2f2ef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
15 changes: 9 additions & 6 deletions rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/url"

"github.com/issue9/assert/v3"
)
Expand Down Expand Up @@ -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 的值。
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 4 additions & 6 deletions rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,16 @@ 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
`,
},
{
req: `POST /body HTTP/1.1
Host: {host}
req: `POST {host}/body HTTP/1.1
Host: 这行将被忽略
Content-Type: application/json
Content-Length: 8
Expand All @@ -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
Expand Down

0 comments on commit ed2f2ef

Please sign in to comment.