From ff50c73269bad6cb5c6097039293556eabd3f96a Mon Sep 17 00:00:00 2001 From: Pavel Vorontsov Date: Fri, 27 Dec 2019 12:20:13 +0300 Subject: [PATCH] pass auth to wsdl request --- go.mod | 2 ++ go.sum | 1 + soap.go | 2 +- soap_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- wsdl.go | 29 ++++++++++++++++++++++------- wsdl_test.go | 9 ++++++++- 6 files changed, 78 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 9feb501..beaff6c 100644 --- a/go.mod +++ b/go.mod @@ -4,3 +4,5 @@ require ( golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 golang.org/x/text v0.3.0 // indirect ) + +go 1.13 diff --git a/go.sum b/go.sum index d586547..6f5ab0c 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,5 @@ golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 h1:ulvT7fqt0yHWzpJwI57MezWnYDVpCAYBVuYst/L+fAY= golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 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..c96f78d 100644 --- a/soap_test.go +++ b/soap_test.go @@ -1,7 +1,11 @@ package gosoap import ( + "fmt" + "io/ioutil" "net/http" + "net/http/httptest" + "os" "testing" ) @@ -153,7 +157,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.") @@ -218,3 +222,42 @@ func TestProcess_doRequest(t *testing.T) { t.Errorf("invalid WSDL") } } + +func TestClient_CallWithAuth(t *testing.T) { + testUser, testPass := "test_user", "test_pass" + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + user, pass, ok := r.BasicAuth() + if !ok { + t.Error("request must be with auth") + } + + if user != testUser || pass != testPass { + t.Errorf("username must %q, pass must be %q", testUser, testPass) + } + + dir, _ := os.Getwd() + + data, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", dir, "testdata/ipservice.wsdl")) + if err != nil { + t.Error(err) + } + + fmt.Fprintln(w, string(data)) + + })) + defer ts.Close() + + soap, err := SoapClient(ts.URL) + if err != nil { + t.Errorf("error not expected: %s", err) + } + + soap.Username = testUser + soap.Password = testPass + + _, err = soap.Call("test", Params{}) + if err != nil { + t.Errorf("error in soap call: %s", err) + } +} 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