Skip to content

Commit

Permalink
Added DumpLogger interface to soap Config for custom logging
Browse files Browse the repository at this point in the history
  • Loading branch information
r6q authored and tiaguinho committed Nov 23, 2020
1 parent d1d0ad1 commit f4a9999
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
30 changes: 25 additions & 5 deletions soap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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 {
Expand Down
50 changes: 50 additions & 0 deletions soap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package gosoap

import (
"crypto/tls"
"fmt"
"log"
"net/http"
"regexp"
"testing"
)

Expand Down Expand Up @@ -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(`(<vatNumber>)[\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 {
Expand Down

0 comments on commit f4a9999

Please sign in to comment.