Skip to content

Commit

Permalink
feat(publisher): add SetClient func
Browse files Browse the repository at this point in the history
Add SetClient func to allow dsl users to control aspects of requests to the
Broker API, such as timeouts, headers, and Transport.
  • Loading branch information
Deseao authored and Alex Haynes committed May 10, 2018
1 parent 5730aba commit 9d5d1ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 10 additions & 2 deletions dsl/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PactName struct {
// Publisher is the API to send Pact files to a Pact Broker.
type Publisher struct {
request types.PublishRequest
client *http.Client
}

// validate the publish requests.
Expand Down Expand Up @@ -71,7 +72,9 @@ func (p *Publisher) validate() error {

// call sends a message to the Pact Broker.
func (p *Publisher) call(method string, url string, content []byte) error {
client := &http.Client{}
if p.client == nil {
p.client = &http.Client{}
}
var req *http.Request
var err error
req, err = http.NewRequest(method, url, bytes.NewReader(content))
Expand All @@ -85,7 +88,7 @@ func (p *Publisher) call(method string, url string, content []byte) error {
req.SetBasicAuth(p.request.BrokerUsername, p.request.BrokerPassword)
}

res, err := client.Do(req)
res, err := p.client.Do(req)
if err != nil {
return err
}
Expand Down Expand Up @@ -191,3 +194,8 @@ func (p *Publisher) tagRequest(consumerName string, request types.PublishRequest

return nil
}

// SetClient allows dsl users to configure the http.Client used when publishing Pacts
func (p *Publisher) SetClient(client *http.Client) {
p.client = client
}
9 changes: 9 additions & 0 deletions dsl/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,12 @@ func TestPublish_tagRequestFail(t *testing.T) {
t.Fatalf("Expected error but got none")
}
}

func TestPublish_SetClient(t *testing.T) {
p := &Publisher{}
client := &http.Client{}
p.SetClient(client)
if p.client != client {
t.Fatalf("SetClient Failed To Set Client On Publisher")
}
}

0 comments on commit 9d5d1ab

Please sign in to comment.