github.com/vicanso/pike@v1.0.1-0.20210630235453-9099e041f6ec/server/cache_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 "net/http/httptest" 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 TestRequestIsPass(t *testing.T) { 36 assert := assert.New(t) 37 assert.True(requestIsPass(httptest.NewRequest("POST", "/", nil))) 38 assert.True(requestIsPass(httptest.NewRequest("PATCH", "/", nil))) 39 assert.True(requestIsPass(httptest.NewRequest("PUT", "/", nil))) 40 assert.False(requestIsPass(httptest.NewRequest("GET", "/", nil))) 41 assert.False(requestIsPass(httptest.NewRequest("HEAD", "/", nil))) 42 } 43 44 func TestGetKey(t *testing.T) { 45 assert := assert.New(t) 46 47 req := httptest.NewRequest("GET", "http://test.com/users/me?type=1", nil) 48 assert.Equal("GET test.com http://test.com/users/me?type=1", string(getKey(req))) 49 } 50 51 func TestCacheMiddleware(t *testing.T) { 52 assert := assert.New(t) 53 54 cacheableContext := elton.NewContext( 55 httptest.NewRecorder(), 56 httptest.NewRequest("GET", "/", nil), 57 ) 58 // 设置可缓存有效期为10 59 setHTTPCacheMaxAge(cacheableContext, 10) 60 setHTTPResp(cacheableContext, &cache.HTTPResponse{}) 61 62 tests := []struct { 63 c *elton.Context 64 status cache.Status 65 }{ 66 // 直接pass的请求 67 { 68 c: elton.NewContext( 69 httptest.NewRecorder(), 70 httptest.NewRequest("POST", "/users/login", nil), 71 ), 72 status: cache.StatusPassed, 73 }, 74 // 首次fetching,返回不可缓存 75 { 76 c: elton.NewContext( 77 httptest.NewRecorder(), 78 httptest.NewRequest("GET", "/users/me", nil), 79 ), 80 status: cache.StatusFetching, 81 }, 82 // 第二次hit for pass 83 { 84 c: elton.NewContext( 85 httptest.NewRecorder(), 86 httptest.NewRequest("GET", "/users/me", nil), 87 ), 88 status: cache.StatusHitForPass, 89 }, 90 // 首次fetching,返回可缓存 91 { 92 c: cacheableContext, 93 status: cache.StatusFetching, 94 }, 95 // 第二次则从缓存获取 96 { 97 c: elton.NewContext( 98 httptest.NewRecorder(), 99 httptest.NewRequest("GET", "/", nil), 100 ), 101 status: cache.StatusHit, 102 }, 103 } 104 105 cacheName := "test" 106 cache.ResetDispatchers([]config.CacheConfig{ 107 { 108 Name: cacheName, 109 Size: 100, 110 }, 111 }) 112 s := NewServer(ServerOption{ 113 Cache: cacheName, 114 }) 115 116 fn := NewCache(s) 117 for _, tt := range tests { 118 tt.c.Next = func() error { 119 return nil 120 } 121 err := fn(tt.c) 122 assert.Nil(err) 123 assert.Equal(tt.status, getCacheStatus(tt.c)) 124 } 125 126 }