summaryrefslogtreecommitdiffstats
path: root/api4/image_test.go
blob: aa3619cdd29bb8e5fe2dcc2b5ce29202bab62206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package api4

import (
	"net/http"
	"net/url"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/mattermost/mattermost-server/model"
)

func TestGetImage(t *testing.T) {
	th := Setup().InitBasic()
	defer th.TearDown()

	th.Client.HttpClient.CheckRedirect = func(*http.Request, []*http.Request) error {
		return http.ErrUseLastResponse
	}

	originURL := "http://foo.bar/baz.gif"

	r, err := http.NewRequest("GET", th.Client.ApiUrl+"/image?url="+url.QueryEscape(originURL), nil)
	require.NoError(t, err)
	r.Header.Set(model.HEADER_AUTH, th.Client.AuthType+" "+th.Client.AuthToken)

	th.App.UpdateConfig(func(cfg *model.Config) {
		cfg.ServiceSettings.ImageProxyType = nil
	})

	resp, err := th.Client.HttpClient.Do(r)
	require.NoError(t, err)
	assert.Equal(t, http.StatusNotFound, resp.StatusCode)

	th.App.UpdateConfig(func(cfg *model.Config) {
		cfg.ServiceSettings.ImageProxyType = model.NewString("atmos/camo")
		cfg.ServiceSettings.ImageProxyOptions = model.NewString("foo")
		cfg.ServiceSettings.ImageProxyURL = model.NewString("https://proxy.foo.bar")
	})

	r, err = http.NewRequest("GET", th.Client.ApiUrl+"/image?url="+originURL, nil)
	require.NoError(t, err)
	r.Header.Set(model.HEADER_AUTH, th.Client.AuthType+" "+th.Client.AuthToken)

	resp, err = th.Client.HttpClient.Do(r)
	require.NoError(t, err)
	assert.Equal(t, http.StatusFound, resp.StatusCode)
	assert.Equal(t, "https://proxy.foo.bar/004afe2ef382eb5f30c4490f793f8a8c5b33d8a2/687474703a2f2f666f6f2e6261722f62617a2e676966", resp.Header.Get("Location"))
}