From 502e9af2151f7ef319c3c7212ef52fa399ab725d Mon Sep 17 00:00:00 2001 From: Jason Cameron Date: Thu, 23 Jan 2025 02:37:15 -0500 Subject: [PATCH] feat: Add config validation tests 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. --- config_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/config_test.go b/config_test.go index 4502ee3..a38e47d 100644 --- a/config_test.go +++ b/config_test.go @@ -2,6 +2,7 @@ package caddydefender import ( "encoding/json" + "github.com/caddyserver/caddy/v2/caddytest" "github.com/jasonlovesdoggo/caddy-defender/responders" "testing" @@ -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") + }) +}