diff --git a/app/mf/mention.go b/app/mf/mention.go index 03a8f98..9e1a0a8 100644 --- a/app/mf/mention.go +++ b/app/mf/mention.go @@ -31,6 +31,8 @@ func (wm Mention) Domain() string { return rest.Domain(wm.Target) } +// Key returns a unique string representation of the mention for use in storage. +// TODO Profiling indicated that md5() consumes a lot of CPU power, so this could be replaced with db migration. func (wm Mention) Key() string { return fmt.Sprintf("%x", md5.Sum([]byte("source="+wm.Source+",target="+wm.Target))) } diff --git a/app/mf/microformats.go b/app/mf/microformats.go index a8de66a..cfcd18d 100644 --- a/app/mf/microformats.go +++ b/app/mf/microformats.go @@ -18,7 +18,7 @@ type IndiewebAuthor struct { Picture string `json:"picture"` } -func (ia IndiewebAuthor) Anonymize() { +func (ia *IndiewebAuthor) Anonymize() { ia.Picture = fmt.Sprintf("/pictures/%s", Anonymous) } diff --git a/app/webmention/handler_test.go b/app/webmention/handler_test.go new file mode 100644 index 0000000..350e8a2 --- /dev/null +++ b/app/webmention/handler_test.go @@ -0,0 +1,64 @@ +package webmention + +import ( + "brainbaking.com/go-jamming/common" + "brainbaking.com/go-jamming/db" + "github.com/stretchr/testify/assert" + "io/ioutil" + "net/http" + "net/http/httptest" + "net/url" + "sync" + "testing" +) + +func postWm(source string, target string) url.Values { + values := url.Values{} + values.Set("source", source) + values.Set("target", target) + return values +} + +var ( + cnf = common.Configure() + repo db.MentionRepo +) + +func init() { + cnf.ConString = ":memory:" + repo = db.NewMentionRepo(cnf) +} + +func TestHandlePostWithInvalidUrlsShouldReturnBadRequest(t *testing.T) { + ts := httptest.NewServer(HandlePost(cnf, repo)) + defer ts.Close() + res, err := http.PostForm(ts.URL, postWm("https://haha.be/woof/said/the/dog.txt", "https://pussies.nl/mycatjustthrewup/gottacleanup.html")) + assert.NoError(t, err) + + content, err := ioutil.ReadAll(res.Body) + defer res.Body.Close() + assert.NoError(t, err) + assert.Contains(t, string(content), "Bad Request") +} + +// Explicitly tests using actual live data, so this could fail if URLs are unreachable. +func TestHandlePostWithTestServer_Parallel(t *testing.T) { + ts := httptest.NewServer(HandlePost(cnf, repo)) + defer ts.Close() + var wg sync.WaitGroup + + for i := 0; i < 5; i++ { + wg.Add(1) + go func() { + defer wg.Done() + res, err := http.PostForm(ts.URL, postWm("https://jefklakscodex.com/articles/retrospectives/raven-shield-17-years-later/", "https://brainbaking.com/post/2020/10/building-a-core2duo-winxp-retro-pc/")) + assert.NoError(t, err) + + content, err := ioutil.ReadAll(res.Body) + defer res.Body.Close() + assert.NoError(t, err) + assert.Contains(t, string(content), "Thanks, bro. Will process this soon, pinky swear") + }() + } + wg.Wait() +} diff --git a/app/webmention/recv/receive_test.go b/app/webmention/recv/receive_test.go index e4aa070..42f0d05 100644 --- a/app/webmention/recv/receive_test.go +++ b/app/webmention/recv/receive_test.go @@ -76,7 +76,7 @@ func TestSaveAuthorPictureLocally(t *testing.T) { } } -func BenchmarkReceive(b *testing.B) { +func BenchmarkReceiveWithoutRestCalls(b *testing.B) { origLog := zerolog.GlobalLevel() zerolog.SetGlobalLevel(zerolog.Disabled) defer zerolog.SetGlobalLevel(origLog)