github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/services/imageproxy/atmos_camo_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  	"io/ioutil"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  
    12  	"github.com/xzl8028/xenia-server/model"
    13  	"github.com/xzl8028/xenia-server/services/httpservice"
    14  	"github.com/xzl8028/xenia-server/utils/testutils"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func makeTestAtmosCamoProxy() *ImageProxy {
    20  	configService := &testutils.StaticConfigService{
    21  		Cfg: &model.Config{
    22  			ServiceSettings: model.ServiceSettings{
    23  				SiteURL:                             model.NewString("https://xenia.example.com"),
    24  				AllowedUntrustedInternalConnections: model.NewString("127.0.0.1"),
    25  			},
    26  			ImageProxySettings: model.ImageProxySettings{
    27  				Enable:                  model.NewBool(true),
    28  				ImageProxyType:          model.NewString(model.IMAGE_PROXY_TYPE_ATMOS_CAMO),
    29  				RemoteImageProxyURL:     model.NewString("http://images.example.com"),
    30  				RemoteImageProxyOptions: model.NewString("7e5f3fab20b94782b43cdb022a66985ef28ba355df2c5d5da3c9a05e4b697bac"),
    31  			},
    32  		},
    33  	}
    34  
    35  	return MakeImageProxy(configService, httpservice.MakeHTTPService(configService), nil)
    36  }
    37  
    38  func TestAtmosCamoBackend_GetImage(t *testing.T) {
    39  	imageURL := "http://www.xenia.org/wp-content/uploads/2016/03/logoHorizontalWhite.png"
    40  	proxiedURL := "http://images.example.com/62183a1cf0a4927c3b56d249366c2745e34ffe63/687474703a2f2f7777772e6d61747465726d6f73742e6f72672f77702d636f6e74656e742f75706c6f6164732f323031362f30332f6c6f676f486f72697a6f6e74616c57686974652e706e67"
    41  
    42  	proxy := makeTestAtmosCamoProxy()
    43  
    44  	recorder := httptest.NewRecorder()
    45  	request, _ := http.NewRequest(http.MethodGet, "", nil)
    46  	proxy.GetImage(recorder, request, imageURL)
    47  	resp := recorder.Result()
    48  
    49  	assert.Equal(t, http.StatusFound, resp.StatusCode)
    50  	assert.Equal(t, proxiedURL, resp.Header.Get("Location"))
    51  }
    52  
    53  func TestAtmosCamoBackend_GetImageDirect(t *testing.T) {
    54  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    55  		w.Header().Set("Cache-Control", "max-age=2592000, private")
    56  		w.Header().Set("Content-Type", "image/png")
    57  		w.Header().Set("Content-Length", "10")
    58  
    59  		w.WriteHeader(http.StatusOK)
    60  		w.Write([]byte("1111111111"))
    61  	})
    62  
    63  	mock := httptest.NewServer(handler)
    64  	defer mock.Close()
    65  
    66  	proxy := makeTestAtmosCamoProxy()
    67  	proxy.ConfigService.(*testutils.StaticConfigService).Cfg.ImageProxySettings.RemoteImageProxyURL = model.NewString(mock.URL)
    68  
    69  	body, contentType, err := proxy.GetImageDirect("https://example.com/image.png")
    70  
    71  	assert.Nil(t, err)
    72  	assert.Equal(t, "image/png", contentType)
    73  
    74  	require.NotNil(t, body)
    75  	respBody, _ := ioutil.ReadAll(body)
    76  	assert.Equal(t, []byte("1111111111"), respBody)
    77  }
    78  
    79  func TestGetAtmosCamoImageURL(t *testing.T) {
    80  	imageURL := "http://www.xenia.org/wp-content/uploads/2016/03/logoHorizontal.png"
    81  	proxiedURL := "http://images.example.com/5b6f6661516bc837b89b54566eb619d14a5c3eca/687474703a2f2f7777772e6d61747465726d6f73742e6f72672f77702d636f6e74656e742f75706c6f6164732f323031362f30332f6c6f676f486f72697a6f6e74616c2e706e67"
    82  
    83  	defaultSiteURL := "https://xenia.example.com"
    84  	proxyURL := "http://images.example.com"
    85  	options := "7e5f3fab20b94782b43cdb022a66985ef28ba355df2c5d5da3c9a05e4b697bac"
    86  
    87  	for _, test := range []struct {
    88  		Name     string
    89  		Input    string
    90  		SiteURL  string
    91  		Expected string
    92  	}{
    93  		{
    94  			Name:     "should proxy image",
    95  			Input:    imageURL,
    96  			SiteURL:  defaultSiteURL,
    97  			Expected: proxiedURL,
    98  		},
    99  		{
   100  			Name:     "should proxy image when no site URL is set",
   101  			Input:    imageURL,
   102  			SiteURL:  "",
   103  			Expected: proxiedURL,
   104  		},
   105  		{
   106  			Name:     "should proxy image when a site URL with a subpath is set",
   107  			Input:    imageURL,
   108  			SiteURL:  proxyURL + "/subpath",
   109  			Expected: proxiedURL,
   110  		},
   111  		{
   112  			Name:     "should not proxy a relative image",
   113  			Input:    "/static/logo.png",
   114  			SiteURL:  defaultSiteURL,
   115  			Expected: "/static/logo.png",
   116  		},
   117  		{
   118  			Name:     "should not proxy an image on the Xenia server",
   119  			Input:    "https://xenia.example.com/static/logo.png",
   120  			SiteURL:  defaultSiteURL,
   121  			Expected: "https://xenia.example.com/static/logo.png",
   122  		},
   123  		{
   124  			Name:     "should not proxy an image on the Xenia server when a subpath is set",
   125  			Input:    "https://xenia.example.com/static/logo.png",
   126  			SiteURL:  defaultSiteURL + "/static",
   127  			Expected: "https://xenia.example.com/static/logo.png",
   128  		},
   129  		{
   130  			Name:     "should not proxy an image that has already been proxied",
   131  			Input:    proxiedURL,
   132  			SiteURL:  defaultSiteURL,
   133  			Expected: proxiedURL,
   134  		},
   135  	} {
   136  		t.Run(test.Name, func(t *testing.T) {
   137  			assert.Equal(t, test.Expected, getAtmosCamoImageURL(test.Input, test.SiteURL, proxyURL, options))
   138  		})
   139  	}
   140  
   141  }