diff --git a/app/pingback/send/send.go b/app/pingback/send/send.go index 8fa1c4b..4769ec0 100644 --- a/app/pingback/send/send.go +++ b/app/pingback/send/send.go @@ -1,9 +1,50 @@ package send import ( + "github.com/rs/zerolog/log" "github.com/wgroeneveld/go-jamming/app/mf" + "github.com/wgroeneveld/go-jamming/rest" + "strings" ) -func SendPingbackToEndpoint(endpoint string, mention mf.Mention) { - // do stuff +type xml string + +var body xml = ` + + pingback.ping + + + {$source} + + + {$target} + + +` + +func (theXml xml) replace(key string, value string) xml { + return xml(strings.ReplaceAll(theXml.String(), key, value)) +} + +func (theXml xml) String() string { + return string(theXml) +} + +func (theXml xml) fill(mention mf.Mention) string { + return theXml. + replace("{$source}", mention.Source). + replace("{$target}", mention.Target). + String() +} + +type Sender struct { + RestClient rest.Client +} + +func (sender *Sender) SendPingbackToEndpoint(endpoint string, mention mf.Mention) { + err := sender.RestClient.Post(endpoint, "text/xml", body.fill(mention)) + if err != nil { + log.Err(err).Str("wm", mention.String()).Msg("Unable to send pingback") + } + log.Info().Str("wm", mention.String()).Msg("Pingback sent") } diff --git a/app/pingback/send/send_test.go b/app/pingback/send/send_test.go new file mode 100644 index 0000000..6f59313 --- /dev/null +++ b/app/pingback/send/send_test.go @@ -0,0 +1,38 @@ +package send + +import ( + "github.com/stretchr/testify/assert" + "github.com/wgroeneveld/go-jamming/app/mf" + "github.com/wgroeneveld/go-jamming/mocks" + "testing" +) + +func TestSendPingbackToEndpoint(t *testing.T) { + var capturedBody string + sender := Sender{ + RestClient: &mocks.RestClientMock{ + PostFunc: func(url string, contentType string, body string) error { + capturedBody = body + return nil + }, + }, + } + expectedXml := ` + + pingback.ping + + + src + + + target + + +` + + sender.SendPingbackToEndpoint("http://dingdong.com/pingback", mf.Mention{ + Source: "src", + Target: "target", + }) + assert.Equal(t, expectedXml, capturedBody) +} \ No newline at end of file diff --git a/app/webmention/send/send.go b/app/webmention/send/send.go index 46d64be..2de63dd 100644 --- a/app/webmention/send/send.go +++ b/app/webmention/send/send.go @@ -3,8 +3,18 @@ package send import ( "github.com/wgroeneveld/go-jamming/app/mf" "github.com/wgroeneveld/go-jamming/app/pingback/send" + "github.com/wgroeneveld/go-jamming/common" + "github.com/wgroeneveld/go-jamming/rest" ) -func mention() { - send.SendPingbackToEndpoint("endpoint", mf.Mention{}) +type Sender struct { + RestClient rest.Client + Conf *common.Config +} + +func mention() { + pingbackSender := &send.Sender{ + RestClient: nil, + } + pingbackSender.SendPingbackToEndpoint("endpoint", mf.Mention{}) } diff --git a/mocks/restclient.go b/mocks/restclient.go index 87aeaea..e7159d0 100644 --- a/mocks/restclient.go +++ b/mocks/restclient.go @@ -12,6 +12,7 @@ import ( type RestClientMock struct { GetFunc func(string) (*http.Response, error) GetBodyFunc func(string) (string, error) + PostFunc func(string, string, string) error } // although these are still requied to match the rest.Client interface. @@ -22,6 +23,10 @@ func (m *RestClientMock) GetBody(url string) (string, error) { return m.GetBodyFunc(url) } +func (m *RestClientMock) Post(url string, contentType string, body string) error { + return m.PostFunc(url, contentType, body) +} + func RelPathGetBodyFunc(t *testing.T, relPath string) func(string) (string, error) { return func(url string) (string, error) { // url: https://brainbaking.com/something-something.html diff --git a/rest/client.go b/rest/client.go index fe391e2..e7a9a09 100644 --- a/rest/client.go +++ b/rest/client.go @@ -3,18 +3,28 @@ package rest import ( "fmt" + "io/ioutil" "net/http" - "io/ioutil" + "strings" ) type Client interface { Get(url string) (*http.Response, error) + Post(url string, contentType string, body string) error GetBody(url string) (string, error) } type HttpClient struct { } +func (client *HttpClient) Post(url string, contenType string, body string) error { + _, err := http.Post(url, contenType, strings.NewReader(body)) + if err != nil { + return err + } + return nil +} + // something like this? https://freshman.tech/snippets/go/http-response-to-string/ func (client *HttpClient) GetBody(url string) (string, error) { resp, geterr := http.Get(url)