Skip to content

Commit

Permalink
added option for logger funcion pointer to enable logging the xml gen…
Browse files Browse the repository at this point in the history
…erated for the request
  • Loading branch information
jweschler committed Jul 8, 2024
1 parent 686642b commit 1ddcf77
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 13 additions & 0 deletions soap/soap.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ type options struct {
httpHeaders map[string]string
mtom bool
mma bool
reqLogger func(s string) ()
}

var defaultOptions = options{
Expand Down Expand Up @@ -329,6 +330,12 @@ func WithMIMEMultipartAttachments() Option {
}
}

func WithReqLogger(logger func (s string) ()) Option {
return func(o *options) {
o.reqLogger = logger
}
}

// Client is soap client
type Client struct {
url string
Expand Down Expand Up @@ -447,6 +454,12 @@ func (s *Client) call(ctx context.Context, soapAction string, request, response
return err
}

if s.opts.reqLogger != nil {
buf := buffer.String()
buffer.WriteString(buf)
s.opts.reqLogger(buf)
}

req, err := http.NewRequest("POST", s.url, buffer)
if err != nil {
return err
Expand Down
9 changes: 8 additions & 1 deletion soap/soap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ func TestClient_Call(t *testing.T) {
}))
defer ts.Close()

client := NewClient(ts.URL)
logger := func (s string) {
wantedReq := `<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><Ping xmlns="http://example.com/service.xsd"><request><Message>Hi</Message></request></Ping></soap:Body></soap:Envelope>`
if s != wantedReq {
t.Errorf("created req %s expected %s", s, wantedReq)
}
}

client := NewClient(ts.URL, WithReqLogger(logger))
req := &Ping{Request: &PingRequest{Message: "Hi"}}
reply := &PingResponse{}
if err := client.Call("GetData", req, reply); err != nil {
Expand Down

0 comments on commit 1ddcf77

Please sign in to comment.