Skip to content

Commit a90c2c9

Browse files
authored
fix(job): check response for nil in (*CurlJob).DumpResponse (#117)
1 parent 45b7905 commit a90c2c9

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

job/curl_job.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package job
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78
"net/http/httputil"
@@ -69,7 +70,10 @@ func (cu *CurlJob) Description() string {
6970
func (cu *CurlJob) DumpResponse(body bool) ([]byte, error) {
7071
cu.Lock()
7172
defer cu.Unlock()
72-
return httputil.DumpResponse(cu.response, body)
73+
if cu.response != nil {
74+
return httputil.DumpResponse(cu.response, body)
75+
}
76+
return nil, errors.New("response is nil")
7377
}
7478

7579
// JobStatus returns the status of the CurlJob.

job/job_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func TestMultipleExecution(t *testing.T) {
6868

6969
// check very often that we've only run one job
7070
ticker := time.NewTicker(2 * time.Millisecond)
71+
loop:
7172
for i := 0; i < 1000; i++ {
7273
select {
7374
case <-ticker.C:
@@ -76,7 +77,7 @@ func TestMultipleExecution(t *testing.T) {
7677
}
7778
case <-ctx.Done():
7879
t.Error("should not have reached timeout")
79-
break
80+
break loop
8081
}
8182
}
8283

@@ -122,7 +123,16 @@ func TestCurlJob(t *testing.T) {
122123
}
123124
}
124125

125-
func TestCurlJobDescription(t *testing.T) {
126+
func TestCurlJob_DumpResponse(t *testing.T) {
127+
request, err := http.NewRequest(http.MethodGet, worldtimeapiURL, nil)
128+
assert.IsNil(t, err)
129+
httpJob := job.NewCurlJob(request)
130+
response, err := httpJob.DumpResponse(false)
131+
assert.IsNil(t, response)
132+
assert.ErrorContains(t, err, "response is nil")
133+
}
134+
135+
func TestCurlJob_Description(t *testing.T) {
126136
postRequest, err := http.NewRequest(
127137
http.MethodPost,
128138
worldtimeapiURL,

0 commit comments

Comments
 (0)