From f4a99995a898b6a2de86e74d0942ffc4cfa89c0d Mon Sep 17 00:00:00 2001 From: robertst0 <17678057+robertst0@users.noreply.github.com> Date: Thu, 19 Nov 2020 21:05:55 +0200 Subject: [PATCH] Added DumpLogger interface to soap Config for custom logging --- soap.go | 30 +++++++++++++++++++++++++----- soap_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/soap.go b/soap.go index 5aabb6b..aa52bee 100644 --- a/soap.go +++ b/soap.go @@ -24,14 +24,30 @@ type SoapParams interface{} type Params map[string]interface{} type ArrayParams [][2]interface{} +type DumpLogger interface { + LogRequest(method string, dump []byte) + LogResponse(method string, dump []byte) +} + +type fmtLogger struct{} + +func (l *fmtLogger) LogRequest(method string, dump []byte) { + fmt.Printf("Request:\n%v\n----\n", string(dump)) +} + +func (l *fmtLogger) LogResponse(method string, dump []byte) { + fmt.Printf("Response:\n%v\n----\n", string(dump)) +} + // Config config the Client type Config struct { - Dump bool + Dump bool + Logger DumpLogger } // SoapClient return new *Client to handle the requests with the WSDL func SoapClient(wsdl string, httpClient *http.Client) (*Client, error) { - return SoapClientWithConfig(wsdl, httpClient, &Config{Dump: false}) + return SoapClientWithConfig(wsdl, httpClient, &Config{Dump: false, Logger: &fmtLogger{}}) } // SoapClientWithConfig return new *Client to handle the requests with the WSDL @@ -45,6 +61,10 @@ func SoapClientWithConfig(wsdl string, httpClient *http.Client, config *Config) httpClient = &http.Client{} } + if config.Logger == nil { + config.Logger = &fmtLogger{} + } + c := &Client{ wsdl: wsdl, config: config, @@ -55,7 +75,7 @@ func SoapClientWithConfig(wsdl string, httpClient *http.Client, config *Config) return c, nil } -// Client struct hold all the informations about WSDL, +// Client struct hold all the information about WSDL, // request and response of the server type Client struct { HTTPClient *http.Client @@ -209,7 +229,7 @@ func (p *process) doRequest(url string) ([]byte, error) { if err != nil { return nil, err } - fmt.Printf("Request:\n%v\n----\n", string(dump)) + p.Client.config.Logger.LogRequest(p.Request.Method, dump) } if p.Client.Username != "" && p.Client.Password != "" { @@ -233,7 +253,7 @@ func (p *process) doRequest(url string) ([]byte, error) { if err != nil { return nil, err } - fmt.Printf("Response:\n%v\n----\n", string(dump)) + p.Client.config.Logger.LogResponse(p.Request.Method, dump) } if resp.StatusCode < 200 || resp.StatusCode >= 400 { diff --git a/soap_test.go b/soap_test.go index 085b990..90481b2 100644 --- a/soap_test.go +++ b/soap_test.go @@ -2,7 +2,10 @@ package gosoap import ( "crypto/tls" + "fmt" + "log" "net/http" + "regexp" "testing" ) @@ -195,6 +198,53 @@ func TestClient_Call(t *testing.T) { } } +type customLogger struct{} + +func (c customLogger) LogRequest(method string, dump []byte) { + var re = regexp.MustCompile(`()[\s\S]*?(<\/vatNumber>)`) + maskedResponse := re.ReplaceAllString(string(dump), `${1}XXX${2}`) + + log.Println(fmt.Sprintf("%s request: %s", method, maskedResponse)) +} + +func (c customLogger) LogResponse(method string, dump []byte) { + if method == "checkVat" { + return + } + + log.Println(fmt.Sprintf("Response: %s", dump)) +} + +func TestClient_Call_WithCustomLogger(t *testing.T) { + soap, err := SoapClientWithConfig("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl", + nil, + &Config{Dump: true, Logger: &customLogger{}}, + ) + if err != nil { + t.Errorf("error not expected: %s", err) + } + + var res *Response + + res, err = soap.CallByStruct(CheckVatRequest{ + CountryCode: "IE", + VatNumber: "6388047V", + }) + if err != nil { + t.Errorf("error in soap call: %s", err) + } + + res.Unmarshal(&rv) + if rv.CountryCode != "IE" { + t.Errorf("error: %+v", rv) + } + + _, err = soap.CallByStruct(nil) + if err == nil { + t.Error("err can't be nil") + } +} + func TestClient_CallByStruct(t *testing.T) { soap, err := SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl", nil) if err != nil {