From 599991ea731953f772824ce3ed1e591246aa004f Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 22 Jan 2018 15:32:50 -0600 Subject: PLT-3383: image proxy support (#7991) * image proxy support * go vet fix, remove mistakenly added coverage file * fix test compile error * add validation to config settings and documentation to model functions * add message_source field to post --- model/post_test.go | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) (limited to 'model/post_test.go') diff --git a/model/post_test.go b/model/post_test.go index 6a908887d..5d5e7c9ec 100644 --- a/model/post_test.go +++ b/model/post_test.go @@ -4,6 +4,7 @@ package model import ( + "io/ioutil" "strings" "testing" @@ -173,3 +174,129 @@ func TestPostSanitizeProps(t *testing.T) { t.Fatal("should not be nil") } } + +var markdownSample, markdownSampleWithRewrittenImageURLs string + +func init() { + bytes, err := ioutil.ReadFile("testdata/markdown-sample.md") + if err != nil { + panic(err) + } + markdownSample = string(bytes) + + bytes, err = ioutil.ReadFile("testdata/markdown-sample-with-rewritten-image-urls.md") + if err != nil { + panic(err) + } + markdownSampleWithRewrittenImageURLs = string(bytes) +} + +func TestRewriteImageURLs(t *testing.T) { + for name, tc := range map[string]struct { + Markdown string + Expected string + }{ + "Empty": { + Markdown: ``, + Expected: ``, + }, + "NoImages": { + Markdown: `foo`, + Expected: `foo`, + }, + "Link": { + Markdown: `[foo](/url)`, + Expected: `[foo](/url)`, + }, + "Image": { + Markdown: `![foo](/url)`, + Expected: `![foo](rewritten:/url)`, + }, + "SpacedURL": { + Markdown: `![foo]( /url )`, + Expected: `![foo]( rewritten:/url )`, + }, + "Title": { + Markdown: `![foo](/url "title")`, + Expected: `![foo](rewritten:/url "title")`, + }, + "Parentheses": { + Markdown: `![foo](/url(1) "title")`, + Expected: `![foo](rewritten:/url\(1\) "title")`, + }, + "AngleBrackets": { + Markdown: `![foo](\\> "title")`, + Expected: `![foo](\\> "title")`, + }, + "MultipleLines": { + Markdown: `![foo]( + \\> + "title" + )`, + Expected: `![foo]( + \\> + "title" + )`, + }, + "ReferenceLink": { + Markdown: `[foo]: \\> "title" + [foo]`, + Expected: `[foo]: \\> "title" + [foo]`, + }, + "ReferenceImage": { + Markdown: `[foo]: \\> "title" + ![foo]`, + Expected: `[foo]: \\> "title" + ![foo]`, + }, + "MultipleReferenceImages": { + Markdown: `[foo]: "title" + [bar]: + [baz]: /url3 "title" + [qux]: /url4 + ![foo]![qux]`, + Expected: `[foo]: "title" + [bar]: + [baz]: /url3 "title" + [qux]: rewritten:/url4 + ![foo]![qux]`, + }, + "DuplicateReferences": { + Markdown: `[foo]: "title" + [foo]: + [foo]: /url3 "title" + [foo]: /url4 + ![foo]![foo]![foo]`, + Expected: `[foo]: "title" + [foo]: + [foo]: /url3 "title" + [foo]: /url4 + ![foo]![foo]![foo]`, + }, + "TrailingURL": { + Markdown: "![foo]\n\n[foo]: /url", + Expected: "![foo]\n\n[foo]: rewritten:/url", + }, + "Sample": { + Markdown: markdownSample, + Expected: markdownSampleWithRewrittenImageURLs, + }, + } { + t.Run(name, func(t *testing.T) { + assert.Equal(t, tc.Expected, RewriteImageURLs(tc.Markdown, func(url string) string { + return "rewritten:" + url + })) + }) + } +} + +var rewriteImageURLsSink string + +func BenchmarkRewriteImageURLs(b *testing.B) { + for i := 0; i < b.N; i++ { + rewriteImageURLsSink = RewriteImageURLs(markdownSample, func(url string) string { + return "rewritten:" + url + }) + } +} -- cgit v1.2.3-1-g7c22