Skip to content

Commit

Permalink
fix(rfc): Ensure supported Vary header (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak authored Mar 17, 2022
1 parent 7210ad0 commit ce6ab9e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 9 deletions.
2 changes: 0 additions & 2 deletions cache/providers/abstractProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ func varyVoter(baseKey string, req *http.Request, currentKey string) bool {
return true
}
}

return true
}

return false
Expand Down
88 changes: 86 additions & 2 deletions docs/e2e/Souin E2E.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,45 @@
},
"response": []
},
{
"name": "Vary",
"event": [
{
"listen": "test",
"script": {
"exec": [
"utils.validateVary(pm, `${utils.getVar(pm, 'caddy_url')}/vary`)"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [
{
"key": "X-something",
"value": "should-vary-1",
"type": "text"
},
{
"key": "Cache-Control",
"value": "",
"type": "text"
}
],
"url": {
"raw": "{{caddy_url}}/vary",
"host": [
"{{caddy_url}}"
],
"path": [
"vary"
]
}
},
"response": []
},
{
"name": "Default no store",
"event": [
Expand Down Expand Up @@ -1254,9 +1293,10 @@
"type": "text/javascript",
"exec": [
"utils = {",
" request: (url, cacheControl = '', body = null) => ({",
" request: (url, cacheControl = '', body = null, headers = {}) => ({",
" header: {",
" 'Cache-Control': cacheControl",
" 'Cache-Control': cacheControl,",
" ...headers,",
" },",
" method: 'GET',",
" body,",
Expand Down Expand Up @@ -1319,6 +1359,50 @@
" });",
" });",
" },",
" validateVary: (pm, baseUrl = '') => {",
" pm.test(\"Status code is 200 with Cache-Status header\", function () {",
" pm.response.to.have.status(200);",
" pm.response.to.have.header(\"Cache-Status\");",
" pm.expect(pm.response.headers.get(\"Cache-Status\")).to.eql(\"Souin; fwd=uri-miss; stored\");",
" pm.response.to.not.have.header(\"Age\");",
" pm.expect(pm.response.text()).to.eql(\"Hello should-vary-1\");",
" });",
" const vary1 = utils.request(baseUrl, '', null, {'X-Something': 'should-vary-1'});",
" const vary2 = utils.request(baseUrl, '', null, {'X-Something': 'should-vary-2'});",
" pm.sendRequest(vary1, function (_, resVary1) {",
" pm.test('Status code is 200, Cache-Status and Age are present', function () {",
" const expected = pm.expect(resVary1);",
" expected.to.have.status(200);",
" expected.to.have.header(\"Cache-Status\");",
" expected.to.have.header(\"Age\");",
" pm.expect(resVary1.headers.get(\"Cache-Status\")).to.include(\"Souin; hit; ttl=14\");",
" pm.expect(resVary1.text()).to.eql(\"Hello should-vary-1\");",
" });",
"",
"",
" pm.sendRequest(vary2, function (_, resVary2Stored) {",
" pm.test(\"Status code is 200 with Cache-Status header\", function () {",
" const expected = pm.expect(resVary2Stored);",
" expected.to.have.status(200);",
" expected.to.have.header(\"Cache-Status\");",
" pm.expect(resVary2Stored.headers.get(\"Cache-Status\")).to.eql(\"Souin; fwd=uri-miss; stored\");",
" expected.to.not.have.header(\"Age\");",
" pm.expect(resVary2Stored.text()).to.eql(\"Hello should-vary-2\");",
" });",
"",
" pm.sendRequest(vary2, function (_, resVary2) {",
" pm.test('Status code is 200, Cache-Status and Age are present', function () {",
" const expected = pm.expect(resVary2);",
" expected.to.have.status(200);",
" expected.to.have.header(\"Cache-Status\");",
" expected.to.have.header(\"Age\");",
" pm.expect(resVary2.headers.get(\"Cache-Status\")).to.include(\"Souin; hit; ttl=14\");",
" pm.expect(resVary2.text()).to.eql(\"Hello should-vary-2\");",
" });",
" });",
" });",
" });",
" },",
" souinAPI: {",
" listKeys: (pm, baseKey, baseUrl = '', additionalPath = '', cacheControl = '') => {",
" const isCached = cacheControl == ''",
Expand Down
8 changes: 8 additions & 0 deletions plugins/caddy/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ cache @matchdefault {
ttl 5s
}

route /vary {
cache {
ttl 15s
}
header Vary X-Something
respond "Hello {http.request.header.X-Something}"
}

route /cache-s-maxage {
cache
header Cache-Control "s-maxage=10"
Expand Down
10 changes: 5 additions & 5 deletions plugins/caddy/httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ type getterContext struct {
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (s *SouinCaddyPlugin) ServeHTTP(rw http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
req := s.Retriever.GetContext().Method.SetContext(r)
if !plugins.CanHandle(req, s.Retriever) {
rw.Header().Add("Cache-Status", "Souin; fwd=uri-miss")
return next.ServeHTTP(rw, r)
}

if b, handler := s.HandleInternally(req); b {
handler(rw, req)
return nil
}

if !plugins.CanHandle(req, s.Retriever) {
rw.Header().Add("Cache-Status", "Souin; fwd=uri-miss")
return next.ServeHTTP(rw, r)
}

req = s.Retriever.GetContext().SetContext(req)
customWriter := &plugins.CustomWriter{
Response: &http.Response{},
Expand Down

0 comments on commit ce6ab9e

Please sign in to comment.