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 f76740f
Show file tree
Hide file tree
Showing 24 changed files with 637 additions and 581 deletions.
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
11 changes: 0 additions & 11 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,13 @@ 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 *envTestSuite) SetupTest() {
}

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
17 changes: 11 additions & 6 deletions ftwhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"
"time"

header_names "github.com/coreruleset/go-ftw/ftwhttp/header_names"
"github.com/stretchr/testify/suite"
"golang.org/x/time/rate"
)
Expand Down Expand Up @@ -131,7 +132,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 +166,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 +252,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
5 changes: 4 additions & 1 deletion ftwhttp/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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 f76740f

Please sign in to comment.