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
Этот коммит содержится в:
@@ -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: ``,
|
||||
Expected: ``,
|
||||
},
|
||||
"SpacedURL": {
|
||||
Markdown: ``,
|
||||
Expected: ``,
|
||||
},
|
||||
"Title": {
|
||||
Markdown: ``,
|
||||
Expected: ``,
|
||||
},
|
||||
"Parentheses": {
|
||||
Markdown: ` "title")`,
|
||||
Expected: ` "title")`,
|
||||
},
|
||||
"AngleBrackets": {
|
||||
Markdown: ``,
|
||||
Expected: ``,
|
||||
},
|
||||
"MultipleLines": {
|
||||
Markdown: ``,
|
||||
Expected: ``,
|
||||
},
|
||||
"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
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user