aboutsummaryrefslogtreecommitdiffhomepage
path: root/admin_test.go
diff options
context:
space:
mode:
authorjhwz <[email protected]>2022-07-07 07:50:07 +1200
committerGitHub <[email protected]>2022-07-06 13:50:07 -0600
commitf259ed52bb3764ce4fd5d88f1712cb43247c2639 (patch)
treea0f7e7a68117b81d757e1a6daddc3e257b97f29b /admin_test.go
parent8bac134f26874b25aa22f8fee325a05d39157a4c (diff)
downloadcaddy-f259ed52bb3764ce4fd5d88f1712cb43247c2639.tar.gz
caddy-f259ed52bb3764ce4fd5d88f1712cb43247c2639.zip
admin: support ETag on config endpoints (#4579)
* admin: support ETags * support etags Co-authored-by: Matt Holt <[email protected]>
Diffstat (limited to 'admin_test.go')
-rw-r--r--admin_test.go51
1 files changed, 50 insertions, 1 deletions
diff --git a/admin_test.go b/admin_test.go
index 608a32c67..32f20c627 100644
--- a/admin_test.go
+++ b/admin_test.go
@@ -15,7 +15,9 @@
package caddy
import (
+ "encoding/hex"
"encoding/json"
+ "net/http"
"reflect"
"sync"
"testing"
@@ -139,10 +141,57 @@ func TestLoadConcurrent(t *testing.T) {
wg.Done()
}()
}
-
wg.Wait()
}
+type fooModule struct {
+ IntField int
+ StrField string
+}
+
+func (fooModule) CaddyModule() ModuleInfo {
+ return ModuleInfo{
+ ID: "foo",
+ New: func() Module { return new(fooModule) },
+ }
+}
+func (fooModule) Start() error { return nil }
+func (fooModule) Stop() error { return nil }
+
+func TestETags(t *testing.T) {
+ RegisterModule(fooModule{})
+
+ if err := Load([]byte(`{"apps": {"foo": {"strField": "abc", "intField": 0}}}`), true); err != nil {
+ t.Fatalf("loading: %s", err)
+ }
+
+ const key = "/" + rawConfigKey + "/apps/foo"
+
+ // try update the config with the wrong etag
+ err := changeConfig(http.MethodPost, key, []byte(`{"strField": "abc", "intField": 1}}`), "/"+rawConfigKey+" not_an_etag", false)
+ if apiErr, ok := err.(APIError); !ok || apiErr.HTTPStatus != http.StatusPreconditionFailed {
+ t.Fatalf("expected precondition failed; got %v", err)
+ }
+
+ // get the etag
+ hash := etagHasher()
+ if err := readConfig(key, hash); err != nil {
+ t.Fatalf("reading: %s", err)
+ }
+
+ // do the same update with the correct key
+ err = changeConfig(http.MethodPost, key, []byte(`{"strField": "abc", "intField": 1}`), key+" "+hex.EncodeToString(hash.Sum(nil)), false)
+ if err != nil {
+ t.Fatalf("expected update to work; got %v", err)
+ }
+
+ // now try another update. The hash should no longer match and we should get precondition failed
+ err = changeConfig(http.MethodPost, key, []byte(`{"strField": "abc", "intField": 2}`), key+" "+hex.EncodeToString(hash.Sum(nil)), false)
+ if apiErr, ok := err.(APIError); !ok || apiErr.HTTPStatus != http.StatusPreconditionFailed {
+ t.Fatalf("expected precondition failed; got %v", err)
+ }
+}
+
func BenchmarkLoad(b *testing.B) {
for i := 0; i < b.N; i++ {
Load(testCfg, true)