summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammed Al Sahaf <msaa1990@gmail.com>2024-05-14 10:50:14 +0300
committerGitHub <noreply@github.com>2024-05-14 07:50:14 +0000
commit4c90f1427f22a85f767dbeabb3bedcc8f9355c84 (patch)
tree9ca714895726403cac5321d343aec112683242e6
parentfb63e2e40ca34122849e63da85952246bf6bc6f1 (diff)
caddytest: normalize the JSON config (#6316)
* caddytest: normalize the JSON config
-rw-r--r--caddytest/caddytest.go14
-rw-r--r--caddytest/caddytest_test.go96
2 files changed, 110 insertions, 0 deletions
diff --git a/caddytest/caddytest.go b/caddytest/caddytest.go
index 3c6f95da..05aa1e3f 100644
--- a/caddytest/caddytest.go
+++ b/caddytest/caddytest.go
@@ -136,6 +136,20 @@ func (tc *Tester) initServer(rawConfig string, configType string) error {
})
rawConfig = prependCaddyFilePath(rawConfig)
+ // normalize JSON config
+ if configType == "json" {
+ tc.t.Logf("Before: %s", rawConfig)
+ var conf any
+ if err := json.Unmarshal([]byte(rawConfig), &conf); err != nil {
+ return err
+ }
+ c, err := json.Marshal(conf)
+ if err != nil {
+ return err
+ }
+ rawConfig = string(c)
+ tc.t.Logf("After: %s", rawConfig)
+ }
client := &http.Client{
Timeout: Default.LoadRequestTimeout,
}
diff --git a/caddytest/caddytest_test.go b/caddytest/caddytest_test.go
index a46867ca..937537fa 100644
--- a/caddytest/caddytest_test.go
+++ b/caddytest/caddytest_test.go
@@ -1,6 +1,7 @@
package caddytest
import (
+ "net/http"
"strings"
"testing"
)
@@ -31,3 +32,98 @@ func TestReplaceCertificatePaths(t *testing.T) {
t.Error("expected redirect uri to be unchanged")
}
}
+
+func TestLoadUnorderedJSON(t *testing.T) {
+ tester := NewTester(t)
+ tester.InitServer(`
+ {
+ "logging": {
+ "logs": {
+ "default": {
+ "level": "DEBUG",
+ "writer": {
+ "output": "stdout"
+ }
+ },
+ "sStdOutLogs": {
+ "level": "DEBUG",
+ "writer": {
+ "output": "stdout"
+ },
+ "include": [
+ "http.*",
+ "admin.*"
+ ]
+ },
+ "sFileLogs": {
+ "level": "DEBUG",
+ "writer": {
+ "output": "stdout"
+ },
+ "include": [
+ "http.*",
+ "admin.*"
+ ]
+ }
+ }
+ },
+ "admin": {
+ "listen": "localhost:2999"
+ },
+ "apps": {
+ "pki": {
+ "certificate_authorities" : {
+ "local" : {
+ "install_trust": false
+ }
+ }
+ },
+ "http": {
+ "http_port": 9080,
+ "https_port": 9443,
+ "servers": {
+ "s_server": {
+ "listen": [
+ ":9443",
+ ":9080"
+ ],
+ "routes": [
+ {
+ "handle": [
+ {
+ "handler": "static_response",
+ "body": "Hello"
+ }
+ ]
+ },
+ {
+ "match": [
+ {
+ "host": [
+ "localhost",
+ "127.0.0.1"
+ ]
+ }
+ ]
+ }
+ ],
+ "logs": {
+ "default_logger_name": "sStdOutLogs",
+ "logger_names": {
+ "localhost": "sStdOutLogs",
+ "127.0.0.1": "sFileLogs"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ `, "json")
+ req, err := http.NewRequest(http.MethodGet, "http://localhost:9080/", nil)
+ if err != nil {
+ t.Fail()
+ return
+ }
+ tester.AssertResponseCode(req, 200)
+}