From 1ddcf77f9ebf68366724ac4a8fbd0708ed4f3eef Mon Sep 17 00:00:00 2001 From: Jordan Weschler Date: Mon, 8 Jul 2024 14:22:36 -0400 Subject: [PATCH] added option for logger funcion pointer to enable logging the xml generated for the request --- soap/soap.go | 13 +++++++++++++ soap/soap_test.go | 9 ++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/soap/soap.go b/soap/soap.go index 838a7032..6e76dc64 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 b436df54..db7bc87a 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 {