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

Support passing context in Call #95

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
25 changes: 18 additions & 7 deletions soap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gosoap

import (
"bytes"
"context"
"encoding/xml"
"errors"
"fmt"
Expand Down Expand Up @@ -100,19 +101,29 @@ type Client struct {
config *Config
}

// Call call's the method m with Params p
func (c *Client) CallContext(ctx context.Context, m string, p SoapParams) (res *Response, err error) {
return c.Do(ctx, NewRequest(m, p))
}

// Call call's the method m with Params p
func (c *Client) Call(m string, p SoapParams) (res *Response, err error) {
return c.Do(NewRequest(m, p))
return c.CallContext(context.Background(), m, p)
}

// CallByStruct call's by struct
func (c *Client) CallByStruct(s RequestStruct) (res *Response, err error) {
func (c *Client) CallByStructContext(ctx context.Context, s RequestStruct) (res *Response, err error) {
req, err := NewRequestByStruct(s)
if err != nil {
return nil, err
}

return c.Do(req)
return c.Do(ctx, req)
}

// CallByStruct call's by struct
func (c *Client) CallByStruct(s RequestStruct) (res *Response, err error) {
return c.CallByStructContext(context.Background(), s)
}

func (c *Client) waitAndRefreshDefinitions(d time.Duration) {
Expand Down Expand Up @@ -145,7 +156,7 @@ func (c *Client) SetWSDL(wsdl string) {
}

// Do Process Soap Request
func (c *Client) Do(req *Request) (res *Response, err error) {
func (c *Client) Do(ctx context.Context, req *Request) (res *Response, err error) {
c.onDefinitionsRefresh.Wait()
c.onRequest.Add(1)
defer c.onRequest.Done()
Expand Down Expand Up @@ -185,7 +196,7 @@ func (c *Client) Do(req *Request) (res *Response, err error) {
return nil, err
}

b, err := p.doRequest(c.Definitions.Services[0].Ports[0].SoapAddresses[0].Location)
b, err := p.doRequest(ctx, c.Definitions.Services[0].Ports[0].SoapAddresses[0].Location)
if err != nil {
return nil, ErrorWithPayload{err, p.Payload}
}
Expand Down Expand Up @@ -221,8 +232,8 @@ type process struct {

// doRequest makes new request to the server using the c.Method, c.URL and the body.
// body is enveloped in Do method
func (p *process) doRequest(url string) ([]byte, error) {
req, err := http.NewRequest("POST", url, bytes.NewBuffer(p.Payload))
func (p *process) doRequest(ctx context.Context, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(p.Payload))
if err != nil {
return nil, err
}
Expand Down
25 changes: 22 additions & 3 deletions soap_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package gosoap

import (
"context"
"crypto/tls"
"errors"
"fmt"
"log"
"net/http"
"net/http/httptest"
"regexp"
"testing"
"time"
)

var (
Expand Down Expand Up @@ -291,22 +295,37 @@ func TestProcess_doRequest(t *testing.T) {
},
}

_, err := c.doRequest("")
_, err := c.doRequest(context.Background(), "")
if err == nil {
t.Errorf("body is empty")
}

_, err = c.doRequest("://teste.")
_, err = c.doRequest(context.Background(), "://teste.")
if err == nil {
t.Errorf("invalid WSDL")
}

_, err = c.doRequest("https://google.com/non-existent-url")
_, err = c.doRequest(context.Background(), "https://google.com/non-existent-url")
if err == nil {
t.Errorf("err can't be nil")
}

if err != nil && err.Error() != "unexpected status code: 404 Not Found" {
t.Errorf("unexpected error: %s", err)
}

doneC := make(chan struct{})
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-doneC
}))
defer ts.Close()

ctx, cancelF := context.WithTimeout(context.Background(), time.Second)
defer cancelF()
_, err = c.doRequest(ctx, ts.URL)

if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("request didn't timeout")
}
close(doneC)
}