Skip to content

Commit

Permalink
pass auth to wsdl request
Browse files Browse the repository at this point in the history
  • Loading branch information
pvorontsovd committed Dec 27, 2019
1 parent a53d7a4 commit edfd61b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion soap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/")
}
Expand Down
2 changes: 1 addition & 1 deletion soap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
29 changes: 22 additions & 7 deletions wsdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -203,6 +217,7 @@ func (wsdl *wsdlDefinitions) GetSoapActionFromWsdlOperation(operation string) st
}
}
}

return ""
}

Expand Down
9 changes: 8 additions & 1 deletion wsdl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gosoap

import (
"fmt"
"net/http"
"os"
"testing"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit edfd61b

Please sign in to comment.