diff --git a/README.md b/README.md index 42047a289..c5fc0902e 100644 --- a/README.md +++ b/README.md @@ -109,15 +109,19 @@ only the phase the rule is defined for. ## E2E Testing -[`Http/e2e/`](./http/e2e) provides an utility to run e2e tests. +[`http/e2e/`](./http/e2e) provides an utility to run e2e tests. It can be used standalone against your own waf deployment: + ```shell -go run github.com/corazawaf/coraza/http/e2e@main --proxy-hostport localhost:8080 --httpbin-hostport localhost:8081 +go run github.com/corazawaf/coraza/http/e2e/cmd/httpe2e@main --proxy-hostport localhost:8080 --httpbin-hostport localhost:8081 ``` + or as a library by importing: + ```go -"github.com/corazawaf/coraza/v3/http/e2e/pkg" +"github.com/corazawaf/coraza/v3/http/e2e" ``` + As a reference for library usage, see [`testing/e2e/e2e_test.go`](.testing/e2e/e2e_test.go). Expected directives that have to be loaded and available flags can be found in [`http/e2e/main.go`](./examples/http/e2e/main.go). diff --git a/http/e2e/main.go b/http/e2e/cmd/httpe2e/main.go similarity index 96% rename from http/e2e/main.go rename to http/e2e/cmd/httpe2e/main.go index c3f84c069..e60899bf4 100644 --- a/http/e2e/main.go +++ b/http/e2e/cmd/httpe2e/main.go @@ -1,6 +1,9 @@ // Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors // SPDX-License-Identifier: Apache-2.0 +//go:build !tinygo +// +build !tinygo + package main import ( @@ -8,7 +11,7 @@ import ( "fmt" "os" - e2e "github.com/corazawaf/coraza/v3/http/e2e/pkg" + "github.com/corazawaf/coraza/v3/http/e2e" ) // Flags: diff --git a/http/e2e/pkg/runner.go b/http/e2e/e2e.go similarity index 98% rename from http/e2e/pkg/runner.go rename to http/e2e/e2e.go index 9532b7efb..b5ec0bb90 100644 --- a/http/e2e/pkg/runner.go +++ b/http/e2e/e2e.go @@ -1,13 +1,15 @@ // Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors // SPDX-License-Identifier: Apache-2.0 +//go:build !tinygo +// +build !tinygo + package e2e import ( "fmt" "io" "net/http" - "net/url" "strconv" "strings" "time" @@ -303,9 +305,9 @@ func Run(cfg Config) error { } func setHTTPSchemeIfMissing(rawURL string) string { - parsedURL, _ := url.Parse(rawURL) - if parsedURL.Scheme == "" { - parsedURL.Scheme = "http" + if rawURL == "" || strings.HasPrefix(rawURL, "http") || strings.HasPrefix(rawURL, "://") { + return rawURL } - return parsedURL.String() + + return "http://" + rawURL } diff --git a/http/e2e/e2e_test.go b/http/e2e/e2e_test.go new file mode 100644 index 000000000..35e972759 --- /dev/null +++ b/http/e2e/e2e_test.go @@ -0,0 +1,31 @@ +// Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors +// SPDX-License-Identifier: Apache-2.0 + +//go:build !tinygo +// +build !tinygo + +package e2e + +import "testing" + +func TestSetHTTPSchemeIfMissing(t *testing.T) { + tests := map[string]struct { + rawURL string + expectedURL string + }{ + "empty": {rawURL: "", expectedURL: ""}, + "path": {rawURL: "abc", expectedURL: "http://abc"}, + "path and port": {rawURL: "abc:123", expectedURL: "http://abc:123"}, + "no schema": {rawURL: "://localhost:123/", expectedURL: "://localhost:123/"}, + "with schema": {rawURL: "http://1.2.3.4:8080/abc", expectedURL: "http://1.2.3.4:8080/abc"}, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + url := setHTTPSchemeIfMissing(test.rawURL) + if want, have := test.expectedURL, url; want != have { + t.Errorf("unexpected URL, want %q, have %q", want, have) + } + }) + } +} diff --git a/testing/e2e/e2e_test.go b/testing/e2e/e2e_test.go index 08fbd0999..45afc2ac4 100644 --- a/testing/e2e/e2e_test.go +++ b/testing/e2e/e2e_test.go @@ -1,4 +1,4 @@ -// Copyright 2022 Juan Pablo Tosso and the OWASP Coraza contributors +// Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors // SPDX-License-Identifier: Apache-2.0 // These benchmarks don't currently compile with TinyGo @@ -15,7 +15,7 @@ import ( "github.com/corazawaf/coraza/v3" txhttp "github.com/corazawaf/coraza/v3/http" - e2e "github.com/corazawaf/coraza/v3/http/e2e/pkg" + "github.com/corazawaf/coraza/v3/http/e2e" "github.com/mccutchen/go-httpbin/v2/httpbin" )