mirror of
https://github.com/aclindsa/ofxgo.git
synced 2026-01-04 21:11:18 -05:00
Allow use of custom http.Client.
One purpose for this is to allow specifying a proxy.
This commit is contained in:
@@ -23,6 +23,8 @@ type BasicClient struct {
|
|||||||
CarriageReturn bool
|
CarriageReturn bool
|
||||||
// Set User-Agent header to this string, if not empty
|
// Set User-Agent header to this string, if not empty
|
||||||
UserAgent string
|
UserAgent string
|
||||||
|
|
||||||
|
HTTPClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// OfxVersion returns the OFX specification version this BasicClient will marshal
|
// OfxVersion returns the OFX specification version this BasicClient will marshal
|
||||||
@@ -70,6 +72,11 @@ func (c *BasicClient) RawRequest(URL string, r io.Reader) (*http.Response, error
|
|||||||
return nil, errors.New("Refusing to send OFX request with possible plain-text password over non-https protocol")
|
return nil, errors.New("Refusing to send OFX request with possible plain-text password over non-https protocol")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
httpClient := c.HTTPClient
|
||||||
|
if httpClient == nil {
|
||||||
|
httpClient = http.DefaultClient
|
||||||
|
}
|
||||||
|
|
||||||
request, err := http.NewRequest("POST", URL, r)
|
request, err := http.NewRequest("POST", URL, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -79,7 +86,7 @@ func (c *BasicClient) RawRequest(URL string, r io.Reader) (*http.Response, error
|
|||||||
if c.UserAgent != "" {
|
if c.UserAgent != "" {
|
||||||
request.Header.Set("User-Agent", c.UserAgent)
|
request.Header.Set("User-Agent", c.UserAgent)
|
||||||
}
|
}
|
||||||
response, err := http.DefaultClient.Do(request)
|
response, err := httpClient.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
31
basic_client_test.go
Normal file
31
basic_client_test.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package ofxgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBasicClient_HTTPClient(t *testing.T) {
|
||||||
|
c := &BasicClient{
|
||||||
|
HTTPClient: &http.Client{
|
||||||
|
Transport: &http.Transport{
|
||||||
|
Dial: func(network, addr string) (net.Conn, error) {
|
||||||
|
return nil, errors.New("bad test client")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
_, err := c.Request(&Request{
|
||||||
|
URL: "https://test",
|
||||||
|
Signon: SignonRequest{
|
||||||
|
UserID: "test",
|
||||||
|
UserPass: "test",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "bad test client") {
|
||||||
|
t.Fatalf("expected error containing 'bad test client', got: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user