summaryrefslogtreecommitdiffstats
path: root/model/post_test.go
diff options
context:
space:
mode:
authorChris <ccbrown112@gmail.com>2018-01-22 15:32:50 -0600
committerGitHub <noreply@github.com>2018-01-22 15:32:50 -0600
commit599991ea731953f772824ce3ed1e591246aa004f (patch)
treefca0f556f12e56bdcefa74ac6794dec64e04e3fc /model/post_test.go
parenta8445775351c32f8a12081f60bda2099571b2758 (diff)
downloadchat-599991ea731953f772824ce3ed1e591246aa004f.tar.gz
chat-599991ea731953f772824ce3ed1e591246aa004f.tar.bz2
chat-599991ea731953f772824ce3ed1e591246aa004f.zip
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
Diffstat (limited to 'model/post_test.go')
-rw-r--r--model/post_test.go127
1 files changed, 127 insertions, 0 deletions
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](</url\<1\>\\> "title")`,
+ Expected: `![foo](<rewritten:/url\<1\>\\> "title")`,
+ },
+ "MultipleLines": {
+ Markdown: `![foo](
+ </url\<1\>\\>
+ "title"
+ )`,
+ Expected: `![foo](
+ <rewritten:/url\<1\>\\>
+ "title"
+ )`,
+ },
+ "ReferenceLink": {
+ Markdown: `[foo]: </url\<1\>\\> "title"
+ [foo]`,
+ Expected: `[foo]: </url\<1\>\\> "title"
+ [foo]`,
+ },
+ "ReferenceImage": {
+ Markdown: `[foo]: </url\<1\>\\> "title"
+ ![foo]`,
+ Expected: `[foo]: <rewritten:/url\<1\>\\> "title"
+ ![foo]`,
+ },
+ "MultipleReferenceImages": {
+ Markdown: `[foo]: </url1> "title"
+ [bar]: </url2>
+ [baz]: /url3 "title"
+ [qux]: /url4
+ ![foo]![qux]`,
+ Expected: `[foo]: <rewritten:/url1> "title"
+ [bar]: </url2>
+ [baz]: /url3 "title"
+ [qux]: rewritten:/url4
+ ![foo]![qux]`,
+ },
+ "DuplicateReferences": {
+ Markdown: `[foo]: </url1> "title"
+ [foo]: </url2>
+ [foo]: /url3 "title"
+ [foo]: /url4
+ ![foo]![foo]![foo]`,
+ Expected: `[foo]: <rewritten:/url1> "title"
+ [foo]: </url2>
+ [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
+ })
+ }
+}