github.com/spreadshirt/mattermost-server@v5.3.2-0.20180927191755-a257d501df3d+incompatible/api4/image_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api4 5 6 import ( 7 "net/http" 8 "net/url" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 14 "github.com/mattermost/mattermost-server/model" 15 ) 16 17 func TestGetImage(t *testing.T) { 18 th := Setup().InitBasic() 19 defer th.TearDown() 20 21 th.Client.HttpClient.CheckRedirect = func(*http.Request, []*http.Request) error { 22 return http.ErrUseLastResponse 23 } 24 25 originURL := "http://foo.bar/baz.gif" 26 27 r, err := http.NewRequest("GET", th.Client.ApiUrl+"/image?url="+url.QueryEscape(originURL), nil) 28 require.NoError(t, err) 29 r.Header.Set(model.HEADER_AUTH, th.Client.AuthType+" "+th.Client.AuthToken) 30 31 imageProxyType := th.App.Config().ServiceSettings.ImageProxyType 32 imageProxyOptions := th.App.Config().ServiceSettings.ImageProxyOptions 33 imageProxyURL := th.App.Config().ServiceSettings.ImageProxyURL 34 defer func() { 35 th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.ImageProxyType = imageProxyType }) 36 th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.ImageProxyOptions = imageProxyOptions }) 37 th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.ImageProxyOptions = imageProxyURL }) 38 }() 39 40 th.App.UpdateConfig(func(cfg *model.Config) { 41 cfg.ServiceSettings.ImageProxyType = nil 42 }) 43 44 resp, err := th.Client.HttpClient.Do(r) 45 require.NoError(t, err) 46 assert.Equal(t, http.StatusNotFound, resp.StatusCode) 47 48 th.App.UpdateConfig(func(cfg *model.Config) { 49 cfg.ServiceSettings.ImageProxyType = model.NewString("atmos/camo") 50 cfg.ServiceSettings.ImageProxyOptions = model.NewString("foo") 51 cfg.ServiceSettings.ImageProxyURL = model.NewString("https://proxy.foo.bar") 52 }) 53 54 r, err = http.NewRequest("GET", th.Client.ApiUrl+"/image?url="+originURL, nil) 55 require.NoError(t, err) 56 r.Header.Set(model.HEADER_AUTH, th.Client.AuthType+" "+th.Client.AuthToken) 57 58 resp, err = th.Client.HttpClient.Do(r) 59 require.NoError(t, err) 60 assert.Equal(t, http.StatusFound, resp.StatusCode) 61 assert.Equal(t, "https://proxy.foo.bar/004afe2ef382eb5f30c4490f793f8a8c5b33d8a2/687474703a2f2f666f6f2e6261722f62617a2e676966", resp.Header.Get("Location")) 62 }