Skip to content

Commit

Permalink
feat: implement multi header support
Browse files Browse the repository at this point in the history
- support multiple headers with the same name
- support multiple headers with identical name and value
- make list of headers ordered; in the future, tests should be able to
  enforce the order of headers in a request
- improve API, tests, and documentation of ftwhttp.Header

- disable logging in tests where possible
- enable self-updater test (go-critic was complaining because the test
  file was touched)

Fixes coreruleset#332
  • Loading branch information
theseion committed Jan 12, 2025
1 parent f1f4972 commit 28b9081
Show file tree
Hide file tree
Showing 41 changed files with 820 additions and 610 deletions.
5 changes: 5 additions & 0 deletions check/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

schema "github.com/coreruleset/ftw-tests-schema/v2/types"
"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"

"github.com/coreruleset/go-ftw/config"
Expand Down Expand Up @@ -37,6 +38,10 @@ type checkBaseTestSuite struct {
cfg *config.FTWConfiguration
}

func (s *checkBaseTestSuite) SetupSuite() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

func (s *checkBaseTestSuite) BeforeTest(_, name string) {
var err error
var logName string
Expand Down
5 changes: 5 additions & 0 deletions check/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"

"github.com/coreruleset/go-ftw/config"
Expand Down Expand Up @@ -34,6 +35,10 @@ type checkErrorTestSuite struct {
cfg *config.FTWConfiguration
}

func (s *checkErrorTestSuite) SetupSuite() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

func TestCheckErrorTestSuite(t *testing.T) {
suite.Run(t, new(checkErrorTestSuite))
}
Expand Down
5 changes: 5 additions & 0 deletions check/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package check
import (
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"

"github.com/coreruleset/go-ftw/config"
Expand Down Expand Up @@ -36,6 +37,10 @@ func TestCheckLogsTestSuite(t *testing.T) {
suite.Run(t, new(checkLogsTestSuite))
}

func (s *checkLogsTestSuite) SetupSuite() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

func (s *checkLogsTestSuite) SetupTest() {
var err error
s.cfg = config.NewDefaultConfig()
Expand Down
5 changes: 5 additions & 0 deletions check/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package check
import (
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"

"github.com/coreruleset/go-ftw/config"
Expand Down Expand Up @@ -36,6 +37,10 @@ func TestCheckResponseTestSuite(t *testing.T) {
suite.Run(t, new(checkResponseTestSuite))
}

func (s *checkResponseTestSuite) SetupSuite() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

func (s *checkResponseTestSuite) SetupTest() {
var err error
s.cfg = config.NewDefaultConfig()
Expand Down
5 changes: 5 additions & 0 deletions check/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"slices"

"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"

"github.com/coreruleset/go-ftw/config"
Expand All @@ -19,6 +20,10 @@ type checkStatusTestSuite struct {
cfg *config.FTWConfiguration
}

func (s *checkStatusTestSuite) SetupSuite() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

func (s *checkStatusTestSuite) SetupTest() {
var err error
s.cfg = config.NewDefaultConfig()
Expand Down
2 changes: 1 addition & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type rootCmdTestSuite struct {
suite.Suite
}

func TestRootChoreTestSuite(t *testing.T) {
func TestRootTestSuite(t *testing.T) {
suite.Run(t, new(rootCmdTestSuite))
}

Expand Down
83 changes: 46 additions & 37 deletions cmd/self_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
package cmd

import (
"bytes"
"fmt"
"io/fs"
"os"
"os/exec"
"path"
"testing"

"github.com/coreruleset/go-ftw/internal/updater"
"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"
)

Expand All @@ -18,6 +23,10 @@ type selfUpdateTestSuite struct {
executablePath string
}

func (s *selfUpdateTestSuite) SetupSuite() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

func (s *selfUpdateTestSuite) SetupTest() {
var err error
s.tempDir, err = os.MkdirTemp("", "self-update-tests")
Expand All @@ -38,40 +47,40 @@ func TestRunSelfUpdateTestSuite(t *testing.T) {
suite.Run(t, new(selfUpdateTestSuite))
}

// func (s *selfUpdateTestSuite) TestSelfUpdateDev() {
// _, err := updater.Updater("v0.0.0-dev", s.executablePath)
// s.Require().NoError(err)
//}
//
//func (s *selfUpdateTestSuite) TestSelfUpdateBigVersion() {
// newVersion, err := updater.Updater("v10000.1.1", s.executablePath)
// s.Require().NoError(err)
// s.Equal("v10000.1.1", newVersion)
//}
//
//func (s *selfUpdateTestSuite) TestSelfUpdateWithExecutablePath() {
// newVersion, err := updater.Updater("v1.3.7", s.executablePath)
// s.Require().NoError(err)
// s.NotEmpty(newVersion)
//
// s.FileExists(s.executablePath, "The executable should exist")
// contents, err := os.ReadFile(s.executablePath)
// s.Require().NoError(err)
// s.NotContains(string(contents), "Fake Binary", "The executable should be replaced")
//
// var out, stderr bytes.Buffer
//
// cmd := exec.Command(s.executablePath, "version")
// cmd.Stdout = &out
// cmd.Stderr = &stderr
//
// err = cmd.Run()
// if err == nil {
// versionString := fmt.Sprintf("ftw %s", newVersion)
// s.Contains(out.String(), versionString)
// } else {
// s.Equal("exit status 1", err.Error())
// oldBinaryWithUnsupportedVersionFlagError := "Error: unknown command \"version\" for \"go-ftw\"\nRun 'go-ftw --help' for usage.\n"
// s.Equal(oldBinaryWithUnsupportedVersionFlagError, stderr.String())
// }
//}
func (s *selfUpdateTestSuite) TestSelfUpdateDev() {
_, err := updater.Updater("v0.0.0-dev", s.executablePath)
s.Require().NoError(err)
}

func (s *selfUpdateTestSuite) TestSelfUpdateBigVersion() {
newVersion, err := updater.Updater("v10000.1.1", s.executablePath)
s.Require().NoError(err)
s.Equal("v10000.1.1", newVersion)
}

func (s *selfUpdateTestSuite) TestSelfUpdateWithExecutablePath() {
newVersion, err := updater.Updater("v1.1.2", s.executablePath)
s.Require().NoError(err)
s.NotEmpty(newVersion)

s.FileExists(s.executablePath, "The executable should exist")
contents, err := os.ReadFile(s.executablePath)
s.Require().NoError(err)
s.NotContains(string(contents), "Fake Binary", "The executable should be replaced")

var out, stderr bytes.Buffer

cmd := exec.Command(s.executablePath, "version")
cmd.Stdout = &out
cmd.Stderr = &stderr

err = cmd.Run()
if err == nil {
versionString := fmt.Sprintf("ftw %s", newVersion)
s.Contains(out.String(), versionString)
} else {
s.Equal("exit status 1", err.Error())
oldBinaryWithUnsupportedVersionFlagError := "Error: unknown command \"version\" for \"go-ftw\"\nRun 'go-ftw --help' for usage.\n"
s.Equal(oldBinaryWithUnsupportedVersionFlagError, stderr.String())
}
}
12 changes: 5 additions & 7 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"

"github.com/coreruleset/go-ftw/utils"
Expand Down Expand Up @@ -45,24 +46,21 @@ type fileTestSuite struct {
cfg *FTWConfiguration
}

type envTestSuite struct {
suite.Suite
}

type baseTestSuite struct {
suite.Suite
}

func TestConfigTestSuite(t *testing.T) {
suite.Run(t, new(baseTestSuite))
suite.Run(t, new(fileTestSuite))
suite.Run(t, new(envTestSuite))
}

func (s *fileTestSuite) SetupTest() {
func (s *baseTestSuite) SetupSuite() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

func (s *envTestSuite) SetupTest() {
func (s *fileTestSuite) SetupSuite() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

func (s *fileTestSuite) BeforeTest(_, name string) {
Expand Down
20 changes: 9 additions & 11 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"regexp"

schema "github.com/coreruleset/ftw-tests-schema/v2/types/overrides"

"github.com/coreruleset/go-ftw/ftwhttp"
)

// RunMode represents the mode of the test run
Expand Down Expand Up @@ -72,15 +70,15 @@ type FTWTestOverride struct {

// Overrides represents the overridden inputs that have to be applied to tests
type Overrides struct {
DestAddr *string `yaml:"dest_addr,omitempty" koanf:"dest_addr,omitempty"`
Port *int `yaml:"port,omitempty" koanf:"port,omitempty"`
Protocol *string `yaml:"protocol,omitempty" koanf:"protocol,omitempty"`
URI *string `yaml:"uri,omitempty" koanf:"uri,omitempty"`
Version *string `yaml:"version,omitempty" koanf:"version,omitempty"`
Headers ftwhttp.Header `yaml:"headers,omitempty" koanf:"headers,omitempty"`
Method *string `yaml:"method,omitempty" koanf:"method,omitempty"`
Data *string `yaml:"data,omitempty" koanf:"data,omitempty"`
SaveCookie *bool `yaml:"save_cookie,omitempty" koanf:"save_cookie,omitempty"`
DestAddr *string `yaml:"dest_addr,omitempty" koanf:"dest_addr,omitempty"`
Port *int `yaml:"port,omitempty" koanf:"port,omitempty"`
Protocol *string `yaml:"protocol,omitempty" koanf:"protocol,omitempty"`
URI *string `yaml:"uri,omitempty" koanf:"uri,omitempty"`
Version *string `yaml:"version,omitempty" koanf:"version,omitempty"`
Headers map[string]string `yaml:"headers,omitempty" koanf:"headers,omitempty"`
Method *string `yaml:"method,omitempty" koanf:"method,omitempty"`
Data *string `yaml:"data,omitempty" koanf:"data,omitempty"`
SaveCookie *bool `yaml:"save_cookie,omitempty" koanf:"save_cookie,omitempty"`
// Deprecated: replaced with AutocompleteHeaders
StopMagic *bool `yaml:"stop_magic" koanf:"stop_magic,omitempty"`
AutocompleteHeaders *bool `yaml:"autocomplete_headers" koanf:"autocomplete_headers,omitempty"`
Expand Down
22 changes: 16 additions & 6 deletions ftwhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"testing"
"time"

header_names "github.com/coreruleset/go-ftw/ftwhttp/header_names"
"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"
"golang.org/x/time/rate"
)
Expand All @@ -30,6 +32,10 @@ func TestClientTestSuite(t *testing.T) {
suite.Run(t, new(clientTestSuite))
}

func (s *clientTestSuite) SetupSuite() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

func (s *clientTestSuite) SetupTest() {
var err error
s.client, err = NewClient(NewClientConfig())
Expand Down Expand Up @@ -131,7 +137,7 @@ func (s *clientTestSuite) TestGetTrackedTime() {
Version: "HTTP/1.1",
}

h := Header{"Accept": "*/*", "User-Agent": "go-ftw test agent", "Host": "localhost"}
h := NewHeaderFromMap(map[string]string{"Accept": "*/*", "User-Agent": "go-ftw test agent", "Host": "localhost"})

data := []byte(`test=me&one=two&one=twice`)
req := NewRequest(rl, h, data, true)
Expand Down Expand Up @@ -165,10 +171,11 @@ func (s *clientTestSuite) TestClientMultipartFormDataRequest() {
Version: "HTTP/1.1",
}

h := Header{
"Accept": "*/*", "User-Agent": "go-ftw test agent", "Host": "localhost",
"Content-Type": "multipart/form-data; boundary=--------397236876",
}
h := NewHeader()
h.Add("Accept", "*/*")
h.Add("User-Agent", "go-ftw test agent")
h.Add("Host", "localhost")
h.Add(header_names.ContentType, "multipart/form-data; boundary=--------397236876")

data := []byte(`----------397236876
Content-Disposition: form-data; name="fileRap"; filename="test.txt"
Expand Down Expand Up @@ -250,7 +257,10 @@ func (s *clientTestSuite) TestClientRateLimits() {
Version: "HTTP/1.1",
}

h := Header{"Accept": "*/*", "User-Agent": "go-ftw test agent", "Host": "localhost"}
h := NewHeader()
h.Add("Accept", "*/*")
h.Add("User-Agent", "go-ftw test agent")
h.Add("Host", "localhost")
req := NewRequest(rl, h, nil, true)

// We need to do at least 2 calls so there is a wait between both.
Expand Down
10 changes: 9 additions & 1 deletion ftwhttp/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ package ftwhttp
import (
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/suite"
)

type connectionTestSuite struct {
suite.Suite
}

func (s *connectionTestSuite) SetupSuite() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

func TestConnectionTestSuite(t *testing.T) {
suite.Run(t, new(connectionTestSuite))
}
Expand All @@ -34,7 +39,10 @@ func (s *connectionTestSuite) TestMultipleRequestTypes() {
Version: "HTTP/1.1",
}

h := Header{"Accept": "*/*", "User-Agent": "go-ftw test agent", "Host": "localhost"}
h := NewHeader()
h.Add("Accept", "*/*")
h.Add("User-Agent", "go-ftw test agent")
h.Add("Host", "localhost")

data := []byte(`test=me&one=two`)
req = NewRequest(rl, h, data, true)
Expand Down
Loading

0 comments on commit 28b9081

Please sign in to comment.