Skip to content

Commit

Permalink
fix: not applying headers to request
Browse files Browse the repository at this point in the history
  • Loading branch information
Moranilt committed Mar 4, 2024
1 parent d992315 commit ebe3f9c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (c *client) Post(ctx context.Context, url string, body []byte, headers map[
return nil, err
}

for key, value := range headers {
request.Header.Set(key, value)
}

request, cancel := c.setRequestTimeout(request)
res, err := c.client.Do(request)
if err != nil {
Expand All @@ -64,6 +68,11 @@ func (c *client) Get(ctx context.Context, url string, headers map[string]string)
if err != nil {
return nil, err
}

for key, value := range headers {
request.Header.Set(key, value)
}

request, cancel := c.setRequestTimeout(request)
res, err := c.client.Do(request)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func TestPost(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status OK, got %d", resp.StatusCode)
}

if resp.Request.Method != http.MethodPost {
t.Errorf("Expected method POST, got %s", resp.Request.Method)
}

if resp.Request.Header.Get("Content-Type") != "application/json" {
t.Errorf("Expected Content-Type application/json, got %s", resp.Request.Header.Get("Content-Type"))
}
}

func TestGet(t *testing.T) {
Expand Down Expand Up @@ -67,4 +75,12 @@ func TestGet(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status OK, got %d", resp.StatusCode)
}

if resp.Request.Method != http.MethodGet {
t.Errorf("Expected method GET, got %s", resp.Request.Method)
}

if resp.Request.Header.Get("Accept") != "application/json" {
t.Errorf("Expected Accept application/json, got %s", resp.Request.Header.Get("Accept"))
}
}

0 comments on commit ebe3f9c

Please sign in to comment.