diff --git a/soap.go b/soap.go index ab4932f..f28c3b7 100644 --- a/soap.go +++ b/soap.go @@ -82,7 +82,7 @@ func (c *Client) waitAndRefreshDefinitions(d time.Duration) { } func (c *Client) initWsdl() { - c.Definitions, c.definitionsErr = getWsdlDefinitions(c.wsdl) + c.Definitions, c.definitionsErr = c.getWsdlDefinitions() if c.definitionsErr == nil { c.URL = strings.TrimSuffix(c.Definitions.TargetNamespace, "/") } diff --git a/soap_test.go b/soap_test.go index 0664fe5..066df80 100644 --- a/soap_test.go +++ b/soap_test.go @@ -153,7 +153,7 @@ func TestClient_Call(t *testing.T) { t.Errorf("error: %+v", rw) } - c := &Client{} + c := &Client{HttpClient: http.DefaultClient} res, err = c.Call("", Params{}) if err == nil { t.Errorf("error expected but nothing got.") diff --git a/wsdl.go b/wsdl.go index 3e7bc85..d0812a4 100644 --- a/wsdl.go +++ b/wsdl.go @@ -2,11 +2,12 @@ package gosoap import ( "encoding/xml" - "golang.org/x/net/html/charset" "io" "net/http" "net/url" "os" + + "golang.org/x/net/html/charset" ) type wsdlDefinitions struct { @@ -155,28 +156,41 @@ type xsdMaxInclusive struct { Value string `xml:"value,attr"` } -func getWsdlBody(u string) (reader io.ReadCloser, err error) { - parse, err := url.Parse(u) +func (c *Client) getWsdlBody() (reader io.ReadCloser, err error) { + parse, err := url.Parse(c.wsdl) if err != nil { return nil, err } + if parse.Scheme == "file" { outFile, err := os.Open(parse.Path) if err != nil { return nil, err } + return outFile, nil } - r, err := http.Get(u) + + req, err := http.NewRequest("GET", c.wsdl, nil) + if err != nil { + return nil, err + } + + if c.Username != "" && c.Password != "" { + req.SetBasicAuth(c.Username, c.Password) + } + + resp, err := c.HttpClient.Do(req) if err != nil { return nil, err } - return r.Body, nil + + return resp.Body, nil } // getWsdlDefinitions sent request to the wsdl url and set definitions on struct -func getWsdlDefinitions(u string) (wsdl *wsdlDefinitions, err error) { - reader, err := getWsdlBody(u) +func (c *Client) getWsdlDefinitions() (wsdl *wsdlDefinitions, err error) { + reader, err := c.getWsdlBody() if err != nil { return nil, err } @@ -203,6 +217,7 @@ func (wsdl *wsdlDefinitions) GetSoapActionFromWsdlOperation(operation string) st } } } + return "" } diff --git a/wsdl_test.go b/wsdl_test.go index c826592..065cba1 100644 --- a/wsdl_test.go +++ b/wsdl_test.go @@ -2,6 +2,7 @@ package gosoap import ( "fmt" + "net/http" "os" "testing" ) @@ -49,7 +50,13 @@ func Test_getWsdlBody(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err := getWsdlBody(tt.args.u) + c := Client{ + HttpClient: http.DefaultClient, + wsdl: tt.args.u, + } + + _, err := c.getWsdlBody() + if (err != nil) != tt.wantErr { t.Errorf("getwsdlBody() error = %v, wantErr %v", err, tt.wantErr) return