Skip to content

Commit

Permalink
Add SetHeaders Method
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Nov 10, 2023
1 parent 452dc1b commit 4844c19
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
25 changes: 12 additions & 13 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,6 @@ func TestRequest(t *testing.T) {
}
}

func TestRequest_Header(t *testing.T) {
{
var req = Post("http://%s", nextAddr()).SetEncoder(FormEncoder)
var typ = req.Header().Get("Content-Type")
assert.Equal(t, typ, MimeForm)
}
{
var req = Get("http://%s", nextAddr())
var typ = req.Header().Get("Content-Type")
assert.Equal(t, typ, MimeJson)
}
}

func TestRequest_SetContext(t *testing.T) {
addr := nextAddr()
srv := &http.Server{Addr: addr}
Expand Down Expand Up @@ -385,3 +372,15 @@ func TestResponse(t *testing.T) {
assert.Error(t, resp.Err())
})
}

func TestRequest_SetHeaders(t *testing.T) {
var h = http.Header{}
h.Set("Content-Type", MimeJson)
h.Set("cookie", "123")
var req = Get("https://api.github.com").
SetHeader("Cookie", "456").
SetHeader("encoding", "none").
SetHeaders(h)
assert.Equal(t, req.headers.Get("cookie"), "123")
assert.Equal(t, req.headers.Get("encoding"), "none")
}
11 changes: 7 additions & 4 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ func (c *Request) SetHeader(k, v string) *Request {
return c
}

// Header 获取请求头
// Get request header
func (c *Request) Header() http.Header {
return c.headers
// SetHeaders 批量设置请求头
// Set Request Header
func (c *Request) SetHeaders(headers http.Header) *Request {
for k, v := range headers {
c.headers[k] = v
}
return c
}

// SetContext 设置请求上下文
Expand Down

0 comments on commit 4844c19

Please sign in to comment.