Skip to content

Commit af17706

Browse files
authored
Add Work.RequestFunc (#149)
* Remove unused variable in test * Add Work.RequestFunc With this commit, people using hey as a library can generate different requests, rather than repeating the same request each time. This can be useful for simulating a variety of requests or replaying traffic.
1 parent f3676ef commit af17706

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

requester/requester.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ type Work struct {
5353

5454
RequestBody []byte
5555

56+
// RequestFunc is a function to generate requests. If it is nil, then
57+
// Request and RequestData are cloned for each request.
58+
RequestFunc func() *http.Request
59+
5660
// N is the total number of requests to make.
5761
N int
5862

@@ -146,7 +150,12 @@ func (b *Work) makeRequest(c *http.Client) {
146150
var code int
147151
var dnsStart, connStart, resStart, reqStart, delayStart time.Duration
148152
var dnsDuration, connDuration, resDuration, reqDuration, delayDuration time.Duration
149-
req := cloneRequest(b.Request, b.RequestBody)
153+
var req *http.Request
154+
if b.RequestFunc != nil {
155+
req = b.RequestFunc()
156+
} else {
157+
req = cloneRequest(b.Request, b.RequestBody)
158+
}
150159
trace := &httptrace.ClientTrace{
151160
DNSStart: func(info httptrace.DNSStartInfo) {
152161
dnsStart = now()

requester/requester_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ func TestQps(t *testing.T) {
7373
}
7474

7575
func TestRequest(t *testing.T) {
76-
var uri, contentType, some, method, auth string
76+
var uri, contentType, some, auth string
7777
handler := func(w http.ResponseWriter, r *http.Request) {
7878
uri = r.RequestURI
79-
method = r.Method
8079
contentType = r.Header.Get("Content-type")
8180
some = r.Header.Get("X-some")
8281
auth = r.Header.Get("Authorization")

0 commit comments

Comments
 (0)