Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fixes e2e pkg. #841

Merged
merged 9 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
5 changes: 4 additions & 1 deletion http/e2e/main.go → http/e2e/cmd/httpe2e/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Copyright 2023 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

//go:build !tinygo
// +build !tinygo

package main

import (
"flag"
"fmt"
"os"

e2e "github.com/corazawaf/coraza/v3/http/e2e/pkg"
"github.com/corazawaf/coraza/v3/http/e2e"
)

// Flags:
Expand Down
12 changes: 7 additions & 5 deletions http/e2e/pkg/runner.go → http/e2e/e2e.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}
31 changes: 31 additions & 0 deletions http/e2e/e2e_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
4 changes: 2 additions & 2 deletions testing/e2e/e2e_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
)

Expand Down
Loading