From a7730466c389b33c0783633bb794f84ef7b8f099 Mon Sep 17 00:00:00 2001 From: Kelwing Date: Wed, 9 Feb 2022 00:30:14 -0500 Subject: [PATCH] prevent null pointer issue with contexts --- http_client.go | 4 ++++ http_client_test.go | 2 ++ 2 files changed, 6 insertions(+) diff --git a/http_client.go b/http_client.go index b84eebb..5002c7b 100644 --- a/http_client.go +++ b/http_client.go @@ -2,6 +2,7 @@ package rest import ( "bytes" + "context" "io" "io/ioutil" "net/http" @@ -22,6 +23,9 @@ type DefaultHTTPClient struct { } func (c *DefaultHTTPClient) Request(req *request) (*DiscordResponse, error) { + if req.ctx == nil { + req.ctx = context.Background() + } l := zerolog.Ctx(req.ctx) var reader io.Reader = nil if req.body != nil { diff --git a/http_client_test.go b/http_client_test.go index a320803..6d456b0 100644 --- a/http_client_test.go +++ b/http_client_test.go @@ -1,6 +1,7 @@ package rest import ( + "context" "io/ioutil" "net/http" "net/http/httptest" @@ -41,6 +42,7 @@ func TestDefaultHTTPClient_Request(t *testing.T) { method: http.MethodGet, body: []byte(`test request body`), contentType: JsonContentType, + ctx: context.Background(), }, wantResponse: &DiscordResponse{ StatusCode: 200,