diff --git a/soap/soap.go b/soap/soap.go index 838a703..6e76dc6 100644 --- a/soap/soap.go +++ b/soap/soap.go @@ -248,6 +248,7 @@ type options struct { httpHeaders map[string]string mtom bool mma bool + reqLogger func(s string) () } var defaultOptions = options{ @@ -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 @@ -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 diff --git a/soap/soap_test.go b/soap/soap_test.go index b436df5..db7bc87 100644 --- a/soap/soap_test.go +++ b/soap/soap_test.go @@ -66,7 +66,14 @@ func TestClient_Call(t *testing.T) { })) defer ts.Close() - client := NewClient(ts.URL) + logger := func (s string) { + wantedReq := `Hi` + 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 {