summaryrefslogtreecommitdiff
path: root/caddytest
diff options
context:
space:
mode:
Diffstat (limited to 'caddytest')
-rw-r--r--caddytest/caddytest.go57
-rw-r--r--caddytest/integration/caddyfile_adapt_test.go285
2 files changed, 341 insertions, 1 deletions
diff --git a/caddytest/caddytest.go b/caddytest/caddytest.go
index 0f2799f..ce70f77 100644
--- a/caddytest/caddytest.go
+++ b/caddytest/caddytest.go
@@ -19,6 +19,8 @@ import (
"testing"
"time"
+ "github.com/aryann/difflib"
+ "github.com/caddyserver/caddy/v2/caddyconfig"
caddycmd "github.com/caddyserver/caddy/v2/cmd"
// plug in Caddy modules here
@@ -58,7 +60,8 @@ func timeElapsed(start time.Time, name string) {
// InitServer this will configure the server with a configurion of a specific
// type. The configType must be either "json" or the adapter type.
func InitServer(t *testing.T, rawConfig string, configType string) {
- if err := initServer(t, rawConfig, configType); errors.Is(err, &configLoadError{}) {
+
+ if err := initServer(t, rawConfig, configType); err != nil {
t.Logf("failed to load config: %s", err)
t.Fail()
}
@@ -316,3 +319,55 @@ func AssertRedirect(t *testing.T, requestURI string, expectedToLocation string,
return resp
}
+
+// AssertAdapt adapts a config and then tests it against an expected result
+func AssertAdapt(t *testing.T, rawConfig string, adapterName string, expectedResponse string) {
+
+ cfgAdapter := caddyconfig.GetAdapter(adapterName)
+ if cfgAdapter == nil {
+ t.Errorf("unrecognized config adapter '%s'", adapterName)
+ return
+ }
+
+ options := make(map[string]interface{})
+ options["pretty"] = "true"
+
+ result, warnings, err := cfgAdapter.Adapt([]byte(rawConfig), options)
+ if err != nil {
+ t.Errorf("adapting config using %s adapter: %v", adapterName, err)
+ return
+ }
+
+ if len(warnings) > 0 {
+ for _, w := range warnings {
+ t.Logf("warning: directive: %s : %s", w.Directive, w.Message)
+ }
+ }
+
+ diff := difflib.Diff(
+ strings.Split(expectedResponse, "\n"),
+ strings.Split(string(result), "\n"))
+
+ // scan for failure
+ failed := false
+ for _, d := range diff {
+ if d.Delta != difflib.Common {
+ failed = true
+ break
+ }
+ }
+
+ if failed {
+ for _, d := range diff {
+ switch d.Delta {
+ case difflib.Common:
+ fmt.Printf(" %s\n", d.Payload)
+ case difflib.LeftOnly:
+ fmt.Printf(" - %s\n", d.Payload)
+ case difflib.RightOnly:
+ fmt.Printf(" + %s\n", d.Payload)
+ }
+ }
+ t.Fail()
+ }
+}
diff --git a/caddytest/integration/caddyfile_adapt_test.go b/caddytest/integration/caddyfile_adapt_test.go
new file mode 100644
index 0000000..bff325b
--- /dev/null
+++ b/caddytest/integration/caddyfile_adapt_test.go
@@ -0,0 +1,285 @@
+package integration
+
+import (
+ "testing"
+
+ "github.com/caddyserver/caddy/v2/caddytest"
+)
+
+func TestHttpOnlyOnLocalhost(t *testing.T) {
+ caddytest.AssertAdapt(t, `
+ localhost:80 {
+ respond /version 200 {
+ body "hello from localhost"
+ }
+ }
+ `, "caddyfile", `{
+ "apps": {
+ "http": {
+ "servers": {
+ "srv0": {
+ "listen": [
+ ":80"
+ ],
+ "routes": [
+ {
+ "match": [
+ {
+ "host": [
+ "localhost"
+ ]
+ }
+ ],
+ "handle": [
+ {
+ "handler": "subroute",
+ "routes": [
+ {
+ "handle": [
+ {
+ "body": "hello from localhost",
+ "handler": "static_response",
+ "status_code": 200
+ }
+ ],
+ "match": [
+ {
+ "path": [
+ "/version"
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "terminal": true
+ }
+ ]
+ }
+ }
+ }
+ }
+}`)
+}
+
+func TestHttpOnlyOnAnyAddress(t *testing.T) {
+ caddytest.AssertAdapt(t, `
+ :80 {
+ respond /version 200 {
+ body "hello from localhost"
+ }
+ }
+ `, "caddyfile", `{
+ "apps": {
+ "http": {
+ "servers": {
+ "srv0": {
+ "listen": [
+ ":80"
+ ],
+ "routes": [
+ {
+ "match": [
+ {
+ "path": [
+ "/version"
+ ]
+ }
+ ],
+ "handle": [
+ {
+ "body": "hello from localhost",
+ "handler": "static_response",
+ "status_code": 200
+ }
+ ]
+ }
+ ]
+ }
+ }
+ }
+ }
+}`)
+}
+
+func TestHttpsOnDomain(t *testing.T) {
+ caddytest.AssertAdapt(t, `
+ a.caddy.localhost {
+ respond /version 200 {
+ body "hello from localhost"
+ }
+ }
+ `, "caddyfile", `{
+ "apps": {
+ "http": {
+ "servers": {
+ "srv0": {
+ "listen": [
+ ":443"
+ ],
+ "routes": [
+ {
+ "match": [
+ {
+ "host": [
+ "a.caddy.localhost"
+ ]
+ }
+ ],
+ "handle": [
+ {
+ "handler": "subroute",
+ "routes": [
+ {
+ "handle": [
+ {
+ "body": "hello from localhost",
+ "handler": "static_response",
+ "status_code": 200
+ }
+ ],
+ "match": [
+ {
+ "path": [
+ "/version"
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "terminal": true
+ }
+ ]
+ }
+ }
+ }
+ }
+}`)
+}
+
+func TestHttpOnlyOnDomain(t *testing.T) {
+ caddytest.AssertAdapt(t, `
+ http://a.caddy.localhost {
+ respond /version 200 {
+ body "hello from localhost"
+ }
+ }
+ `, "caddyfile", `{
+ "apps": {
+ "http": {
+ "servers": {
+ "srv0": {
+ "listen": [
+ ":80"
+ ],
+ "routes": [
+ {
+ "match": [
+ {
+ "host": [
+ "a.caddy.localhost"
+ ]
+ }
+ ],
+ "handle": [
+ {
+ "handler": "subroute",
+ "routes": [
+ {
+ "handle": [
+ {
+ "body": "hello from localhost",
+ "handler": "static_response",
+ "status_code": 200
+ }
+ ],
+ "match": [
+ {
+ "path": [
+ "/version"
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "terminal": true
+ }
+ ],
+ "automatic_https": {
+ "skip": [
+ "a.caddy.localhost"
+ ]
+ }
+ }
+ }
+ }
+ }
+}`)
+}
+
+func TestHttpOnlyOnNonStandardPort(t *testing.T) {
+ caddytest.AssertAdapt(t, `
+ http://a.caddy.localhost:81 {
+ respond /version 200 {
+ body "hello from localhost"
+ }
+ }
+ `, "caddyfile", `{
+ "apps": {
+ "http": {
+ "servers": {
+ "srv0": {
+ "listen": [
+ ":81"
+ ],
+ "routes": [
+ {
+ "match": [
+ {
+ "host": [
+ "a.caddy.localhost"
+ ]
+ }
+ ],
+ "handle": [
+ {
+ "handler": "subroute",
+ "routes": [
+ {
+ "handle": [
+ {
+ "body": "hello from localhost",
+ "handler": "static_response",
+ "status_code": 200
+ }
+ ],
+ "match": [
+ {
+ "path": [
+ "/version"
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "terminal": true
+ }
+ ],
+ "automatic_https": {
+ "skip": [
+ "a.caddy.localhost"
+ ]
+ }
+ }
+ }
+ }
+ }
+}`)
+}