github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/services/imageproxy/imageproxy_test.go (about) 1 // Copyright (c) 2017-present Xenia, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package imageproxy 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestGetProxiedImageURL(t *testing.T) { 13 siteURL := "https://xenia.example.com" 14 15 imageURL := "http://www.xenia.org/wp-content/uploads/2016/03/logoHorizontal.png" 16 proxiedURL := "https://xenia.example.com/api/v4/image?url=http%3A%2F%2Fwww.xenia.org%2Fwp-content%2Fuploads%2F2016%2F03%2FlogoHorizontal.png" 17 18 for _, test := range []struct { 19 Name string 20 Input string 21 Expected string 22 }{ 23 { 24 Name: "should proxy an image", 25 Input: imageURL, 26 Expected: proxiedURL, 27 }, 28 { 29 Name: "should not proxy a relative image", 30 Input: "/static/logo.png", 31 Expected: "/static/logo.png", 32 }, 33 { 34 Name: "should not proxy an image on the Xenia server", 35 Input: "https://xenia.example.com/static/logo.png", 36 Expected: "https://xenia.example.com/static/logo.png", 37 }, 38 { 39 Name: "should not proxy an image that has already been proxied", 40 Input: proxiedURL, 41 Expected: proxiedURL, 42 }, 43 } { 44 t.Run(test.Name, func(t *testing.T) { 45 assert.Equal(t, test.Expected, getProxiedImageURL(test.Input, siteURL)) 46 }) 47 } 48 } 49 50 func TestGetUnproxiedImageURL(t *testing.T) { 51 siteURL := "https://xenia.example.com" 52 53 imageURL := "http://www.xenia.org/wp-content/uploads/2016/03/logoHorizontal.png" 54 proxiedURL := "https://xenia.example.com/api/v4/image?url=http%3A%2F%2Fwww.xenia.org%2Fwp-content%2Fuploads%2F2016%2F03%2FlogoHorizontal.png" 55 56 for _, test := range []struct { 57 Name string 58 Input string 59 Expected string 60 }{ 61 { 62 Name: "should remove proxy", 63 Input: proxiedURL, 64 Expected: imageURL, 65 }, 66 { 67 Name: "should not remove proxy from a relative image", 68 Input: "/static/logo.png", 69 Expected: "/static/logo.png", 70 }, 71 { 72 Name: "should not remove proxy from an image on the Xenia server", 73 Input: "https://xenia.example.com/static/logo.png", 74 Expected: "https://xenia.example.com/static/logo.png", 75 }, 76 { 77 Name: "should not remove proxy from a non-proxied image", 78 Input: imageURL, 79 Expected: imageURL, 80 }, 81 } { 82 t.Run(test.Name, func(t *testing.T) { 83 assert.Equal(t, test.Expected, getUnproxiedImageURL(test.Input, siteURL)) 84 }) 85 } 86 }