Skip to content

Commit

Permalink
chore: fixes e2e pkg. (#841)
Browse files Browse the repository at this point in the history
* chore: fixes e2e pkg.

* fix: setHTTPSchemeIfMissing

Co-Authored-By: Matteo Pace <matteo@tetrate.io>

* tests: adds test for setHTTPSchemeIfMissing.

* chore: fixes license.

* chore: attempt to fix it.

* tests: ignores tinygo for http e2e.

* tests: adds test for setHTTPSchemeIfMissing.

* chore: fixes license.

* tests: ignores tinygo for cmd http e2e.

---------

Co-authored-by: Matteo Pace <pace.matteo96@gmail.com>
Co-authored-by: Matteo Pace <matteo@tetrate.io>
  • Loading branch information
3 people authored Jul 11, 2023
1 parent a838547 commit e20a5b4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
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

0 comments on commit e20a5b4

Please sign in to comment.