app/urls_test.go
84 lines · 3251 bytes
1package app
2
3import (
4 "strings"
5 "testing"
6
7 "github.com/krazywarez/devianter"
8)
9
10func TestProxiedMediaURLCarriesTokenAndFilenameAsQuery(t *testing.T) {
11 uri := CFG.URI
12 CFG.URI = "/"
13 defer func() { CFG.URI = uri }()
14
15 raw := "https://images-wixmp-abc.wixmp.com/f/u/x.png/v1/fit/w_640,h_364/x_by_y.png?token=tok.en.sig"
16 got := proxiedMediaURL("http://localhost", raw, "x_by_y.png")
17 want := "http://localhost/media/file/abc/f/u/x.png/v1/fit/w_640,h_364/x_by_y.png?filename=x_by_y.png&token=tok.en.sig"
18 if got != want {
19 t.Errorf("got %s\nwant %s", got, want)
20 }
21 if proxiedMediaURL("http://localhost", "https://example.com/x.png", "x.png") != "" {
22 t.Error("a non-wixmp URL was proxied")
23 }
24}
25
26func TestParseMediaMatchesTheProxyRoute(t *testing.T) {
27 proxy, uri := CFG.Proxy, CFG.URI
28 CFG.Proxy, CFG.URI = true, "/"
29 defer func() { CFG.Proxy, CFG.URI = proxy, uri }()
30
31 d := fullviewDeviation()
32 d.Media.Token = []string{"tok.en.sig"}
33 got := ParseMedia("http://localhost", d.Media)
34 if !strings.HasPrefix(got, "http://localhost/media/file/abc/") || !strings.Contains(got, "token=tok.en.sig") || !strings.Contains(got, "filename=") {
35 t.Errorf("proxied media URL is %q", got)
36 }
37
38 CFG.Proxy = false
39 if got := ParseMedia("http://localhost", d.Media); !strings.HasPrefix(got, "https://images-wixmp-abc.wixmp.com/") {
40 t.Errorf("with proxying off got %q, want the wixmp URL", got)
41 }
42}
43
44func TestConvertDeviantArtURLToSkunkyArt(t *testing.T) {
45 uri := CFG.URI
46 CFG.URI = "/"
47 defer func() { CFG.URI = uri }()
48
49 cases := map[string]string{
50 "https://www.deviantart.com/alice/art/Title-123": "http://localhost/post/alice/Title-123",
51 "https://alice.deviantart.com/art/Title-123": "http://localhost/post/alice/Title-123",
52 "https://www.deviantart.com/stash/01t1te6losnc": "",
53 "https://sta.sh/01t1te6losnc": "",
54 "https://www.deviantart.com/alice": "",
55 "https://example.com/alice/art/Title-123": "",
56 "https://www.deviantart.com/alice/art/Title-123?comment": "http://localhost/post/alice/Title-123",
57 }
58 for in, want := range cases {
59 if got := ConvertDeviantArtURLToSkunkyArt("http://localhost", in); got != want {
60 t.Errorf("%s: got %q, want %q", in, got, want)
61 }
62 }
63}
64
65// TestLegacyEmoticonIsServedThroughTheInstance is the regression test for #6's
66// HTML half: the emoticon name used to be cut out of the URL by fixed offsets,
67// which only fit one URL shape.
68func TestLegacyEmoticonIsServedThroughTheInstance(t *testing.T) {
69 uri := CFG.URI
70 CFG.URI = "/"
71 defer func() { CFG.URI = uri }()
72
73 var d devianter.Text
74 d.Html.Markup = `hi <img src="https://e.deviantart.net/emoticons/s/smile.gif" title=":) (Smile)"> and <img src="https://e.deviantart.net/emoticons/letters/l/love.gif" title="Love"> not <img src="https://example.com/x.png">`
75 out := ParseDescription("http://localhost", d)
76 for _, want := range []string{`src="http://localhost/media/emojitar/smile?type=e"`, `src="http://localhost/media/emojitar/love?type=e"`, `alt=":) (Smile)"`} {
77 if !strings.Contains(out, want) {
78 t.Errorf("output lacks %q:\n%s", want, out)
79 }
80 }
81 if strings.Contains(out, "example.com") {
82 t.Errorf("foreign image carried over:\n%s", out)
83 }
84}