pingback sending, beginning of webmention sending

This commit is contained in:
Wouter Groeneveld 2021-04-09 14:59:12 +02:00
parent 257666439a
commit 7f2c540448
5 changed files with 109 additions and 5 deletions

View File

@ -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 = `<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>pingback.ping</methodName>
<params>
<param>
<value><string>{$source}</string></value>
</param>
<param>
<value><string>{$target}</string></value>
</param>
</params>
</methodCall>`
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")
}

View File

@ -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 := `<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>pingback.ping</methodName>
<params>
<param>
<value><string>src</string></value>
</param>
<param>
<value><string>target</string></value>
</param>
</params>
</methodCall>`
sender.SendPingbackToEndpoint("http://dingdong.com/pingback", mf.Mention{
Source: "src",
Target: "target",
})
assert.Equal(t, expectedXml, capturedBody)
}

View File

@ -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{})
}

View File

@ -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

View File

@ -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)