github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/services/imageproxy/imageproxy_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package imageproxy 5 6 import ( 7 "net/url" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestGetProxiedImageURL(t *testing.T) { 15 siteURL := "https://mattermost.example.com" 16 parsedURL, err := url.Parse(siteURL) 17 require.NoError(t, err) 18 19 imageURL := "http://www.mattermost.org/wp-content/uploads/2016/03/logoHorizontal.png" 20 proxiedURL := "https://mattermost.example.com/api/v4/image?url=http%3A%2F%2Fwww.mattermost.org%2Fwp-content%2Fuploads%2F2016%2F03%2FlogoHorizontal.png" 21 22 proxy := ImageProxy{siteURL: parsedURL} 23 24 for _, test := range []struct { 25 Name string 26 Input string 27 Expected string 28 }{ 29 { 30 Name: "should proxy an image", 31 Input: imageURL, 32 Expected: proxiedURL, 33 }, 34 { 35 Name: "should not proxy a relative image", 36 Input: "/static/logo.png", 37 Expected: "https://mattermost.example.com/static/logo.png", 38 }, 39 { 40 Name: "should bypass opaque URLs", 41 Input: "http:xyz123?query", 42 Expected: siteURL, 43 }, 44 { 45 Name: "should not proxy an image on the HungKnow server ", 46 Input: "https://mattermost.example.com/static/logo.png", 47 Expected: "https://mattermost.example.com/static/logo.png", 48 }, 49 { 50 Name: "should not proxy an image that has already been proxied", 51 Input: proxiedURL, 52 Expected: proxiedURL, 53 }, 54 { 55 Name: "should not bypass protocol relative URLs", 56 Input: "//mattermost.org/static/logo.png", 57 Expected: "https://mattermost.example.com/api/v4/image?url=https%3A%2F%2Fmattermost.org%2Fstatic%2Flogo.png", 58 }, 59 { 60 Name: "should not bypass if the host prefix is same", 61 Input: "https://mattermost.example.com.anothersite.com/static/logo.png", 62 Expected: "https://mattermost.example.com/api/v4/image?url=https%3A%2F%2Fmattermost.example.com.anothersite.com%2Fstatic%2Flogo.png", 63 }, 64 { 65 Name: "should not bypass for user auth URLs", 66 Input: "https://mattermost.example.com@anothersite.com/static/logo.png", 67 Expected: "https://mattermost.example.com/api/v4/image?url=https%3A%2F%2Fmattermost.example.com%40anothersite.com%2Fstatic%2Flogo.png", 68 }, 69 } { 70 t.Run(test.Name, func(t *testing.T) { 71 assert.Equal(t, test.Expected, proxy.GetProxiedImageURL(test.Input)) 72 }) 73 } 74 } 75 76 func TestGetUnproxiedImageURL(t *testing.T) { 77 siteURL := "https://mattermost.example.com" 78 79 imageURL := "http://www.mattermost.org/wp-content/uploads/2016/03/logoHorizontal.png" 80 proxiedURL := "https://mattermost.example.com/api/v4/image?url=http%3A%2F%2Fwww.mattermost.org%2Fwp-content%2Fuploads%2F2016%2F03%2FlogoHorizontal.png" 81 82 for _, test := range []struct { 83 Name string 84 Input string 85 Expected string 86 }{ 87 { 88 Name: "should remove proxy", 89 Input: proxiedURL, 90 Expected: imageURL, 91 }, 92 { 93 Name: "should not remove proxy from a relative image", 94 Input: "/static/logo.png", 95 Expected: "/static/logo.png", 96 }, 97 { 98 Name: "should not remove proxy from an image on the HungKnow server ", 99 Input: "https://mattermost.example.com/static/logo.png", 100 Expected: "https://mattermost.example.com/static/logo.png", 101 }, 102 { 103 Name: "should not remove proxy from a non-proxied image", 104 Input: imageURL, 105 Expected: imageURL, 106 }, 107 } { 108 t.Run(test.Name, func(t *testing.T) { 109 assert.Equal(t, test.Expected, getUnproxiedImageURL(test.Input, siteURL)) 110 }) 111 } 112 }