Skip to content

Commit

Permalink
Increase test coverage for Caddyfile parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
hslatman committed Jan 13, 2024
1 parent a0805a0 commit 3d6402c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
94 changes: 94 additions & 0 deletions crowdsec/caddyfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ func TestUnmarshalCaddyfile(t *testing.T) {
args args
wantParseErr bool
}{
{
name: "fail/missing tokens",
expected: &CrowdSec{},
args: args{
d: caddyfile.NewTestDispenser(``),
},
wantParseErr: true,
},
{
name: "fail/not-crowdsec",
expected: &CrowdSec{},
Expand All @@ -42,6 +50,92 @@ func TestUnmarshalCaddyfile(t *testing.T) {
},
wantParseErr: true,
},
{
name: "fail/no-api-url",
expected: &CrowdSec{},
args: args{
d: caddyfile.NewTestDispenser(`crowdsec {
api_url
api_key some_random_key
ticker_interval 30x
}`),
},
wantParseErr: true,
},
{
name: "fail/invalid-api-url",
expected: &CrowdSec{},
args: args{
d: caddyfile.NewTestDispenser(`crowdsec {
api_url http://\x00/
api_key some_random_key
ticker_interval 30x
}`),
},
wantParseErr: true,
},
{
name: "fail/invalid-api-url-no-scheme",
expected: &CrowdSec{},
args: args{
d: caddyfile.NewTestDispenser(`crowdsec {
api_url example.com
api_key some_random_key
ticker_interval 30x
}`),
},
wantParseErr: true,
},
{
name: "fail/missing-api-key",
expected: &CrowdSec{},
args: args{
d: caddyfile.NewTestDispenser(`crowdsec {
api_url http://127.0.0.1:8080
api_key
}`),
},
wantParseErr: true,
},
{
name: "fail/missing-ticker-interval",
expected: &CrowdSec{},
args: args{
d: caddyfile.NewTestDispenser(`crowdsec {
api_url http://127.0.0.1:8080
api_key test-key
ticker_interval
}`),
},
wantParseErr: true,
},
{
name: "fail/invalid-streaming",
expected: &CrowdSec{},
args: args{
d: caddyfile.NewTestDispenser(`crowdsec {
api_url http://127.0.0.1:8080
api_key test-key
ticker_interval 30s
disable_streaming absolutely
}`),
},
wantParseErr: true,
},
{
name: "fail/invalid-streaming",
expected: &CrowdSec{},
args: args{
d: caddyfile.NewTestDispenser(`crowdsec {
api_url http://127.0.0.1:8080
api_key test-key
ticker_interval 30s
disable_streaming
enable_hard_fails yo
}`),
},
wantParseErr: true,
},
{
name: "fail/unknown-token",
expected: &CrowdSec{},
Expand Down
4 changes: 2 additions & 2 deletions crowdsec/crowdsec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestCrowdSec_streamingBouncerRuntime(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestCount += 1
w.WriteHeader(200) // just accept any request
w.Write(nil)
w.Write(nil) // nolint
}))
defer srv.Close()

Expand Down Expand Up @@ -201,7 +201,7 @@ func TestCrowdSec_liveBouncerRuntime(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestCount += 1
w.WriteHeader(200) // just accept any request
w.Write(nil)
w.Write(nil) // nolint
}))
defer srv.Close()

Expand Down

0 comments on commit 3d6402c

Please sign in to comment.