github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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  	th.App.UpdateConfig(func(cfg *model.Config) {
    32  		cfg.ServiceSettings.ImageProxyType = nil
    33  	})
    34  
    35  	resp, err := th.Client.HttpClient.Do(r)
    36  	require.NoError(t, err)
    37  	assert.Equal(t, http.StatusNotFound, resp.StatusCode)
    38  
    39  	th.App.UpdateConfig(func(cfg *model.Config) {
    40  		cfg.ServiceSettings.ImageProxyType = model.NewString("atmos/camo")
    41  		cfg.ServiceSettings.ImageProxyOptions = model.NewString("foo")
    42  		cfg.ServiceSettings.ImageProxyURL = model.NewString("https://proxy.foo.bar")
    43  	})
    44  
    45  	r, err = http.NewRequest("GET", th.Client.ApiUrl+"/image?url="+originURL, nil)
    46  	require.NoError(t, err)
    47  	r.Header.Set(model.HEADER_AUTH, th.Client.AuthType+" "+th.Client.AuthToken)
    48  
    49  	resp, err = th.Client.HttpClient.Do(r)
    50  	require.NoError(t, err)
    51  	assert.Equal(t, http.StatusFound, resp.StatusCode)
    52  	assert.Equal(t, "https://proxy.foo.bar/004afe2ef382eb5f30c4490f793f8a8c5b33d8a2/687474703a2f2f666f6f2e6261722f62617a2e676966", resp.Header.Get("Location"))
    53  }