Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added DumpLogger interface to soap Config for custom logging #71

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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