Skip to content

Commit

Permalink
Update client and add a new option
Browse files Browse the repository at this point in the history
  • Loading branch information
bozd4g committed Dec 4, 2022
1 parent ca611b0 commit bc995a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
16 changes: 8 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
type (
// Client is a struct who has BaseUrl property
Client struct {
baseUrl string
headers map[string]Header
query map[string]string
body []byte
timeout time.Duration

httpClient *http.Client
defaultOpts []ClientOption
baseUrl string
httpClient *http.Client

headers map[string]Header
query map[string]string
body []byte
transport *http.Transport
timeout time.Duration
}

// Clienter is a interface who calls the methods
Expand Down
11 changes: 10 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package gohttpclient

import "time"
import (
"net/http"
"time"
)

type (
Option func(c *Client)
ClientOption Option
)

func WithCustomHttpClient(client *http.Client) ClientOption {
return func(c *Client) {
c.httpClient = client
}
}

func WithDefaultHeaders() ClientOption {
return func(c *Client) {
if c.headers == nil {
Expand Down
15 changes: 15 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gohttpclient

import (
"context"
"net/http"
"testing"
"time"

Expand All @@ -21,6 +22,20 @@ func (s *TestOptionSuite) SetupSuite() {
s.ctx = context.Background()
}

func (s *TestOptionSuite) Test_WithCustomHttpClient_ShouldRunSuccesfully() {
// Arrange
baseUrl := "http://localhost:8080"
httpClient := &http.Client{}
client := New(baseUrl)

// Act
WithCustomHttpClient(httpClient)(client)

// Assert
s.Assert().NotNil(client.httpClient)
s.Assert().Equal(httpClient, client.httpClient)
}

func (s *TestOptionSuite) Test_WithDefaultHeaders_ShouldRunSuccesfully() {
// Arrange
baseUrl := "http://localhost:8080"
Expand Down

0 comments on commit bc995a1

Please sign in to comment.