Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass auth to wsdl request #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
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
45 changes: 44 additions & 1 deletion soap_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package gosoap

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
)

Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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)
}
}
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