Skip to content

Commit

Permalink
fix(job): check response for nil in (*CurlJob).DumpResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Mar 8, 2024
1 parent 45b7905 commit a0f16e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 5 additions & 1 deletion job/curl_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package job

import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -69,7 +70,10 @@ func (cu *CurlJob) Description() string {
func (cu *CurlJob) DumpResponse(body bool) ([]byte, error) {
cu.Lock()
defer cu.Unlock()
return httputil.DumpResponse(cu.response, body)
if cu.response != nil {
return httputil.DumpResponse(cu.response, body)
}
return nil, errors.New("response is nil")
}

// JobStatus returns the status of the CurlJob.
Expand Down
14 changes: 12 additions & 2 deletions job/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func TestMultipleExecution(t *testing.T) {

// check very often that we've only run one job
ticker := time.NewTicker(2 * time.Millisecond)
loop:
for i := 0; i < 1000; i++ {
select {
case <-ticker.C:
Expand All @@ -76,7 +77,7 @@ func TestMultipleExecution(t *testing.T) {
}
case <-ctx.Done():
t.Error("should not have reached timeout")
break
break loop
}
}

Expand Down Expand Up @@ -122,7 +123,16 @@ func TestCurlJob(t *testing.T) {
}
}

func TestCurlJobDescription(t *testing.T) {
func TestCurlJob_DumpResponse(t *testing.T) {
request, err := http.NewRequest(http.MethodGet, worldtimeapiURL, nil)
assert.IsNil(t, err)
httpJob := job.NewCurlJob(request)
response, err := httpJob.DumpResponse(false)
assert.IsNil(t, response)
assert.ErrorContains(t, err, "response is nil")
}

func TestCurlJob_Description(t *testing.T) {
postRequest, err := http.NewRequest(
http.MethodPost,
worldtimeapiURL,
Expand Down

0 comments on commit a0f16e8

Please sign in to comment.