-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
146 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package webclient | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
// JsonContentType is the default mime type for JSON | ||
const JsonContentType = "application/json" | ||
|
||
// DefaultJsonClient is the singleton instance of WebClient using http.DefaultClient | ||
var DefaultJsonClient = NewJSONClient() | ||
|
||
// GetJson fetches url using GET and unmarshals into the passed response using DefaultJsonClient | ||
func GetJson(url string, response interface{}) error { | ||
return DefaultJsonClient.Get(url, response) | ||
} | ||
|
||
// PostJson sends request as JSON and unmarshals the response JSON into the supplied struct using DefaultJsonClient | ||
func PostJson(url string, request interface{}, response interface{}) error { | ||
return DefaultJsonClient.Post(url, request, response) | ||
} | ||
|
||
// NewJSONClient returns a WebClient using the default http.Client and JSON serialization | ||
func NewJSONClient() WebClient { | ||
var c client | ||
c = client{ | ||
headers: http.Header{ | ||
"Content-Type": []string{JsonContentType}, | ||
}, | ||
parse: json.Unmarshal, | ||
write: func(v interface{}) ([]byte, error) { | ||
return json.MarshalIndent(v, "", c.indent) | ||
}, | ||
} | ||
return &c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package webclient_test | ||
|
||
import ( | ||
"github.com/containrrr/shoutrrr/pkg/common/webclient" | ||
"net/http" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("ClientService", func() { | ||
|
||
When("getting the web client from an empty service", func() { | ||
It("should return an initialized web client", func() { | ||
service := &webclient.ClientService{} | ||
Expect(service.WebClient()).ToNot(BeNil()) | ||
}) | ||
}) | ||
|
||
When("getting the http client from an empty service", func() { | ||
It("should return an initialized http client", func() { | ||
service := &webclient.ClientService{} | ||
Expect(service.HTTPClient()).ToNot(BeNil()) | ||
}) | ||
}) | ||
|
||
When("no certs have been added", func() { | ||
It("should use nil as the certificate pool", func() { | ||
service := &webclient.ClientService{} | ||
tp := service.HTTPClient().Transport.(*http.Transport) | ||
Expect(tp.TLSClientConfig.RootCAs).To(BeNil()) | ||
}) | ||
}) | ||
|
||
When("a custom cert have been added", func() { | ||
It("should use a custom certificate pool", func() { | ||
service := &webclient.ClientService{} | ||
|
||
// Adding an empty cert should fail | ||
addedOk := service.AddTrustedRootCertificate([]byte{}) | ||
Expect(addedOk).To(BeFalse()) | ||
|
||
tp := service.HTTPClient().Transport.(*http.Transport) | ||
Expect(tp.TLSClientConfig.RootCAs).ToNot(BeNil()) | ||
}) | ||
}) | ||
}) |