github.com/vicanso/pike@v1.0.1-0.20210630235453-9099e041f6ec/server/server_test.go (about)

     1  // MIT License
     2  
     3  // Copyright (c) 2020 Tree Xie
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in all
    13  // copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    21  // SOFTWARE.
    22  
    23  package server
    24  
    25  import (
    26  	"regexp"
    27  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/vicanso/elton"
    31  	"github.com/vicanso/pike/cache"
    32  	"github.com/vicanso/pike/config"
    33  )
    34  
    35  func TestGetSetCacheStatus(t *testing.T) {
    36  	assert := assert.New(t)
    37  	c := elton.NewContext(nil, nil)
    38  	assert.Equal(cache.StatusUnknown, getCacheStatus(c))
    39  	setCacheStatus(c, cache.StatusHit)
    40  	assert.Equal(cache.StatusHit, getCacheStatus(c))
    41  }
    42  
    43  func TestGetSetHTTPResp(t *testing.T) {
    44  	assert := assert.New(t)
    45  	c := elton.NewContext(nil, nil)
    46  	assert.Nil(getHTTPResp(c))
    47  	httpResp := &cache.HTTPResponse{}
    48  	setHTTPResp(c, httpResp)
    49  	assert.Equal(httpResp, getHTTPResp(c))
    50  }
    51  
    52  func TestGetSetHTTPRespAge(t *testing.T) {
    53  	assert := assert.New(t)
    54  	c := elton.NewContext(nil, nil)
    55  	assert.Equal(0, getHTTPRespAge(c))
    56  	age := 10
    57  	setHTTPRespAge(c, age)
    58  	assert.Equal(age, getHTTPRespAge(c))
    59  }
    60  
    61  func TestGetSetHTTPCacheMaxAge(t *testing.T) {
    62  	assert := assert.New(t)
    63  	c := elton.NewContext(nil, nil)
    64  	assert.Equal(0, getHTTPCacheMaxAge(c))
    65  	age := 10
    66  	setHTTPCacheMaxAge(c, age)
    67  	assert.Equal(10, getHTTPCacheMaxAge(c))
    68  }
    69  
    70  func TestServer(t *testing.T) {
    71  	assert := assert.New(t)
    72  
    73  	locations := []string{
    74  		"location-test",
    75  	}
    76  	cache := "cache-test"
    77  	compress := "compress-test"
    78  	filter := regexp.MustCompile(`text`)
    79  	s := NewServer(ServerOption{
    80  		Locations:                 locations,
    81  		Cache:                     cache,
    82  		Compress:                  compress,
    83  		CompressContentTypeFilter: filter,
    84  	})
    85  	defer s.Close()
    86  
    87  	assert.Equal(cache, s.GetCache())
    88  	assert.Equal(locations, s.GetLocations())
    89  	compressSrv, compressMinLength, compressContentTypeFilter := s.GetCompress()
    90  	assert.Equal(compress, compressSrv)
    91  	assert.Equal(defaultCompressMinLength, compressMinLength)
    92  	assert.Equal(filter, compressContentTypeFilter)
    93  
    94  	err := s.Start(true)
    95  	assert.True(s.listening)
    96  	assert.Nil(err)
    97  	assert.NotEmpty(s.GetListenAddr())
    98  
    99  	minLength := 101
   100  	s.Update(ServerOption{
   101  		CompressMinLength: minLength,
   102  	})
   103  	_, compressMinLength, _ = s.GetCompress()
   104  
   105  	assert.Equal(minLength, compressMinLength)
   106  }
   107  
   108  func TestServers(t *testing.T) {
   109  	assert := assert.New(t)
   110  	locations := []string{
   111  		"location-test",
   112  	}
   113  	cache := "cache-test"
   114  	compress := "compress-test"
   115  	filter := regexp.MustCompile(`text`)
   116  	ss := NewServers([]ServerOption{
   117  		{
   118  			Locations:                 locations,
   119  			Cache:                     cache,
   120  			Compress:                  compress,
   121  			CompressContentTypeFilter: filter,
   122  		},
   123  	})
   124  	err := ss.Start()
   125  	assert.Nil(err)
   126  	defer ss.Close()
   127  	s := ss.Get("")
   128  	assert.NotNil(s)
   129  	compresName, _, _ := s.GetCompress()
   130  	assert.Equal(compress, compresName)
   131  
   132  	newCompress := "compress-new"
   133  	ss.Reset([]ServerOption{
   134  		{
   135  			Locations:                 locations,
   136  			Cache:                     cache,
   137  			Compress:                  newCompress,
   138  			CompressContentTypeFilter: filter,
   139  		},
   140  	})
   141  	compresName, _, _ = s.GetCompress()
   142  	assert.Equal(newCompress, compresName)
   143  }
   144  
   145  func TestConvertConfig(t *testing.T) {
   146  	assert := assert.New(t)
   147  	addr := ":3015"
   148  	locations := []string{
   149  		"location-test",
   150  	}
   151  	cache := "cache-test"
   152  	compress := "compress-test"
   153  	minLength := 1000
   154  	filter := `text|json`
   155  	configs := []config.ServerConfig{
   156  		{
   157  			Addr:                      addr,
   158  			Locations:                 locations,
   159  			Cache:                     cache,
   160  			Compress:                  compress,
   161  			CompressMinLength:         "1kb",
   162  			CompressContentTypeFilter: filter,
   163  		},
   164  	}
   165  	opts := convertConfig(configs)
   166  	assert.Equal(1, len(opts))
   167  	assert.Equal(addr, opts[0].Addr)
   168  	assert.Equal(locations, opts[0].Locations)
   169  	assert.Equal(cache, opts[0].Cache)
   170  	assert.Equal(compress, opts[0].Compress)
   171  	assert.Equal(minLength, opts[0].CompressMinLength)
   172  	assert.Equal(filter, opts[0].CompressContentTypeFilter.String())
   173  }
   174  
   175  func TestDefaultServers(t *testing.T) {
   176  	assert := assert.New(t)
   177  
   178  	defer func() {
   179  		_ = Close()
   180  	}()
   181  
   182  	assert.Nil(Get(""))
   183  
   184  	locations := []string{
   185  		"location-test",
   186  	}
   187  	cache := "cache-test"
   188  	compress := "compress-test"
   189  	Reset([]config.ServerConfig{
   190  		{
   191  			Locations:                 locations,
   192  			Cache:                     cache,
   193  			Compress:                  compress,
   194  			CompressContentTypeFilter: "text",
   195  		},
   196  	})
   197  	err := Start()
   198  	assert.Nil(err)
   199  	s := Get("")
   200  	assert.NotNil(s)
   201  }