Skip to content

Commit

Permalink
add Request.SetHost and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Mar 14, 2024
1 parent 3034caf commit 4861700
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions httpx/header_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (h *RequestHeader) SetMethod(method string) {
h.hdr.SetMethod(method)
}

func (h *RequestHeader) SetHost(host string) {
h.hdr.SetHost(host)
}

func (h *RequestHeader) RequestURI() []byte { return h.hdr.RequestURI() }

func (h *RequestHeader) SetRequestURI(uri string) {
Expand Down
20 changes: 17 additions & 3 deletions httpx/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,21 @@ func TestRequestDisableSpecialHeaders(t *testing.T) {
func TestRequest(t *testing.T) {
var req RequestHeader
req.SetRequestURI("http://example.com")

var b bytes.Buffer
b.Write(req.Header())
req.SetHost("example.com")
req.SetMethod("PUT")
req.SetUserAgent("test")
req.SetContentType("text/plain")
// req.SetProtocol("HTTP/2.1")
result := string(req.Header())
b := bufio.NewReader(strings.NewReader(result))

var loop RequestHeader
err := loop.Read(b)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
resultLoopback := string(loop.Header())
if result != resultLoopback {
t.Errorf("loopback mismatch: %q got %q", result, resultLoopback)
}
}

0 comments on commit 4861700

Please sign in to comment.