Skip to content

Commit

Permalink
feat: Add config validation tests
Browse files Browse the repository at this point in the history
Adds a test to validate that the Defender module returns an
error when an invalid responder type is specified in the
Caddyfile.  The test uses the caddytest package to assert
that the expected error is returned.
  • Loading branch information
JasonLovesDoggo committed Jan 23, 2025
1 parent 09685b9 commit 502e9af
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package caddydefender

import (
"encoding/json"
"github.com/caddyserver/caddy/v2/caddytest"
"github.com/jasonlovesdoggo/caddy-defender/responders"
"testing"

Expand Down Expand Up @@ -186,3 +187,64 @@ func TestValidation(t *testing.T) {
require.ErrorContains(t, def.Validate(), "invalid IP range")
})
}

func TestDefenderValidation(t *testing.T) {
t.Run("Invalid responder type", func(t *testing.T) {
caddytest.AssertLoadError(t, `{
"admin": {
"disabled": true
},
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
"127.0.0.1:80",
"[::1]:80"
],
"routes": [
{
"handle": [
{
"handler": "defender",
"ranges": [
"localhost"
],
"raw_responder": "pineapple"
},
{
"body": "This is what a human sees",
"handler": "static_response"
}
]
}
],
"automatic_https": {
"disable": true
}
},
"srv1": {
"listen": [
"127.0.0.1:83",
"[::1]:83"
],
"routes": [
{
"handle": [
{
"body": "Clear text HTTP",
"handler": "static_response"
}
]
}
],
"automatic_https": {
"disable": true
}
}
}
}
}
}`, "json", "unknown responder type")
})
}

0 comments on commit 502e9af

Please sign in to comment.