Skip to content

Commit

Permalink
feat(tests): add more caddy test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed May 18, 2024
1 parent 4e350cc commit 55134e8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ default_cache:
headers: # Add headers to the key
- Authorization # Add the header value in the key
- Content-Type # Add the header value in the key
hide: true # Prevent the key from being exposed in the `Cache-Status` HTTP response header
template: "{http.request.method}-{http.request.host}-{http.request.path}" # Use caddy placeholders to create the key (when this option is enabled, disable_* directives are skipped)
etcd: # If distributed is set to true, you'll have to define either the etcd or olric section
configuration: # Configure directly the Etcd client
endpoints: # Define multiple endpoints
Expand Down Expand Up @@ -209,6 +209,7 @@ surrogate_keys:
| `default_cache.key.hash` | Hash the key name in the storage | `true`<br/><br/>`(default: false)` |
| `default_cache.key.headers` | Add headers to the key matching the regexp | `- Authorization`<br/><br/>`- Content-Type`<br/><br/>`- X-Additional-Header` |
| `default_cache.key.hide` | Prevent the key from being exposed in the `Cache-Status` HTTP response header | `true`<br/><br/>`(default: false)` |
| `default_cache.key.template` | Use caddy placeholders to create the key (when this option is enabled, disable_* directives are skipped) | [Placeholders documentation](https://caddyserver.com/docs/caddyfile/concepts#placeholders) |
| `default_cache.mode` | RFC respect tweaking | One of `bypass` `bypass_request` `bypass_response` `strict` (default `strict`) |
| `default_cache.nuts` | Configure the Nuts cache storage | |
| `default_cache.nuts.path` | Set the Nuts file path storage | `/anywhere/nuts/storage` |
Expand Down
5 changes: 4 additions & 1 deletion plugins/caddy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,17 @@ What does these directives mean?
| `cdn.service_id` | The service id if required, depending the provider | `123456_id` |
| `cdn.zone_id` | The zone id if required, depending the provider | `anywhere_zone` |
| `default_cache_control` | Set the default value of `Cache-Control` response header if not set by upstream (Souin treats empty `Cache-Control` as `public` if omitted) | `no-store` |
| `max_cachable_body_bytes` | Set the maximum size (in bytes) for a response body to be cached (unlimited if omited) | `1048576` (1MB) |
| `max_cachable_body_bytes` | Set the maximum size (in bytes) for a response body to be cached (unlimited if omited) | `1048576` (1MB) |
| `key` | Override the key generation with the ability to disable unecessary parts | |
| `key.disable_body` | Disable the body part in the key (GraphQL context) | `true`<br/><br/>`(default: false)` |
| `key.disable_host` | Disable the host part in the key | `true`<br/><br/>`(default: false)` |
| `key.disable_method` | Disable the method part in the key | `true`<br/><br/>`(default: false)` |
| `key.disable_query` | Disable the query string part in the key | `true`<br/><br/>`(default: false)` |
| `key.disable_scheme` | Disable the scheme string part in the key | `true`<br/><br/>`(default: false)` |
| `key.hash` | Hash the key before store it in the storage to get smaller keys | `true`<br/><br/>`(default: false)` |
| `key.headers` | Add headers to the key matching the regexp | `Authorization Content-Type X-Additional-Header` |
| `key.hide` | Prevent the key from being exposed in the `Cache-Status` HTTP response header | `true`<br/><br/>`(default: false)` |
| `key.template` | Use caddy templates to create the key (when this option is enabled, disable_* directives are skipped) | `KEY-{http.request.uri.path}-{http.request.uri.query}` |
| `mode` | Bypass the RFC respect | One of `bypass` `bypass_request` `bypass_response` `strict` (default `strict`) |
| `nuts` | Configure the Nuts cache storage | |
| `nuts.path` | Set the Nuts file path storage | `/anywhere/nuts/storage` |
Expand Down
82 changes: 82 additions & 0 deletions plugins/caddy/httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,88 @@ func TestAgeHeader(t *testing.T) {
}
}

func TestKeyGeneration(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
admin localhost:2999
http_port 9080
https_port 9443
cache {
ttl 1000s
}
}
localhost:9080 {
route /key-template-route {
cache {
key {
template {method}-{host}-{path}-WITH_SUFFIX
}
}
respond "Hello, template route!"
}
route /key-headers-route {
cache {
key {
headers X-Header X-Internal
}
}
respond "Hello, headers route!"
}
route /key-hash-route {
cache {
key {
hash
}
}
respond "Hello, hash route!"
}
}`, "caddyfile")

resp1, _ := tester.AssertGetResponse(`http://localhost:9080/key-template-route`, 200, "Hello, template route!")
if resp1.Header.Get("Age") != "" {
t.Errorf("unexpected Age header %v", resp1.Header.Get("Age"))
}
if !strings.Contains(resp1.Header.Get("Cache-Status"), "key=GET-localhost-/key-template-route-WITH_SUFFIX") {
t.Errorf("unexpected Cache-Status header %v", resp1.Header.Get("Cache-Status"))
}

resp2, _ := tester.AssertGetResponse(`http://localhost:9080/key-template-route`, 200, "Hello, template route!")
if resp2.Header.Get("Age") == "" {
t.Error("Age header should be present")
}
if resp2.Header.Get("Age") != "1" {
t.Error("Age header should be present")
}
if !strings.Contains(resp2.Header.Get("Cache-Status"), "key=GET-localhost-/key-template-route-WITH_SUFFIX") {
t.Errorf("unexpected Cache-Status header %v", resp2.Header.Get("Cache-Status"))
}

rq, _ := http.NewRequest(http.MethodGet, "http://localhost:9080/key-headers-route", nil)
rq.Header = http.Header{
"X-Internal": []string{"my-value"},
}
resp1, _ = tester.AssertResponse(rq, 200, "Hello, headers route!")
if resp1.Header.Get("Age") != "" {
t.Errorf("unexpected Age header %v", resp1.Header.Get("Age"))
}
if !strings.Contains(resp1.Header.Get("Cache-Status"), "key=GET-http-localhost:9080-/key-headers-route--my-value") {
t.Errorf("unexpected Cache-Status header %v", resp1.Header.Get("Cache-Status"))
}

rq.Header = http.Header{
"X-Header": []string{"first"},
"X-Internal": []string{"my-value"},
}
resp1, _ = tester.AssertResponse(rq, 200, "Hello, headers route!")
if resp1.Header.Get("Age") != "" {
t.Errorf("unexpected Age header %v", resp1.Header.Get("Age"))
}
if !strings.Contains(resp1.Header.Get("Cache-Status"), "key=GET-http-localhost:9080-/key-headers-route-first-my-value") {
t.Errorf("unexpected Cache-Status header %v", resp1.Header.Get("Cache-Status"))
}
}

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

0 comments on commit 55134e8

Please sign in to comment.