Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugins): Caddy config cache_keys json parse error #460

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions configurationtypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ func (c *CacheKeys) parseJSON(rootDecoder *json.Decoder) {
var token json.Token
var err error

_, _ = rootDecoder.Token()
_, _ = rootDecoder.Token()
_, _ = rootDecoder.Token()

for err == nil {
Expand Down
33 changes: 33 additions & 0 deletions plugins/caddy/httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,39 @@ func TestQueryString(t *testing.T) {
}
}

func TestCacheKeys(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
admin localhost:2999
order cache before rewrite
http_port 9080
https_port 9443
cache
}
localhost:9080 {
route /query-string {
cache {
cache_keys {
query= {
disable_query
}
}
}
respond "Hello, query string!"
}
}`, "caddyfile")

resp1, _ := tester.AssertGetResponse(`http://localhost:9080/query-string?query=string`, 200, "Hello, query string!")
if resp1.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/query-string" {
t.Errorf("unexpected Cache-Status header %v", resp1.Header)
}
resp2, _ := tester.AssertGetResponse(`http://localhost:9080/query-string?foo=bar`, 200, "Hello, query string!")
if resp2.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/query-string?foo=bar" {
t.Errorf("unexpected Cache-Status header %v", resp2.Header)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you disable query within the cache_key then you can expect two stored responses because it should generate a key like that GET-http-localhost:9080-/query-string for both so the first one will receive a response that is stored and the second one will get a cached response.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick reply.
If I run the new test case alone with go test -run TestCacheKeys, then it passes, but without the run filter the same test fails. Could be a persistent state between tests?

I expected the new test case to be valid because the query= regexp matches the first request and modifies the key generation to exclude the query string, and the key becomes GET-http-localhost:9080-/query-string. The second request does not match the regexp so the key generation should include the query string and the key becomes GET-http-localhost:9080-/query-string?foo=bar. Since it is a different key its a miss and store.
At least this was my understanding of the docs. What is the expected behavior in this case?

}

func TestMaxAge(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
Expand Down
Loading