Skip to content

Commit

Permalink
Convert headers to lowercase. fixes #760
Browse files Browse the repository at this point in the history
  • Loading branch information
aelsabbahy committed Sep 15, 2022
1 parent 12cef65 commit 1fe5571
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
9 changes: 7 additions & 2 deletions matchers/have_patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func (m *HavePatternsMatcher) Match(actual interface{}) (success bool, err error
fh = av
case string:
fh = strings.NewReader(av)
case []string:
fh = strings.NewReader(strings.Join(av, "\n"))
default:
err = fmt.Errorf("Incorrect type %T", actual)

Expand Down Expand Up @@ -114,8 +116,11 @@ func (m *HavePatternsMatcher) Match(actual interface{}) (success bool, err error
}

func (m *HavePatternsMatcher) FailureResult(actual interface{}) MatcherResult {
a, ok := actual.(string)
if !ok {
var a interface{}
switch actual.(type) {
case string, []string:
a = actual
default:
a = fmt.Sprintf("object: %T", actual)
}
return MatcherResult{
Expand Down
6 changes: 3 additions & 3 deletions resource/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type HTTP struct {
Status matcher `json:"status" yaml:"status"`
AllowInsecure bool `json:"allow-insecure" yaml:"allow-insecure"`
NoFollowRedirects bool `json:"no-follow-redirects" yaml:"no-follow-redirects"`
Timeout int `json:"timeout" yaml:"timeout"`
Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"`
RequestHeader []string `json:"request-headers,omitempty" yaml:"request-headers,omitempty"`
RequestBody string `json:"request-bod,omitemptyy" yaml:"request-body,omitempty"`
Headers matcher `json:"headers,omitempty" yaml:"headers,omitempty"`
Body matcher `json:"body" yaml:"body"`
Body matcher `json:"body,omitempty" yaml:"body,omitempty"`
Username string `json:"username,omitempty" yaml:"username,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
Skip bool `json:"skip,omitempty" yaml:"skip,omitempty"`
Expand Down Expand Up @@ -64,7 +64,7 @@ func (u *HTTP) Validate(sys *system.System) []TestResult {
skip = true
}
if isSet(u.Headers) {
results = append(results, ValidateGomegaValue(u, "Headers", u.Headers, sysHTTP.Headers, skip))
results = append(results, ValidateValue(u, "Headers", u.Headers, sysHTTP.Headers, skip))
}
if isSet(u.Body) {
results = append(results, ValidateValue(u, "Body", u.Body, sysHTTP.Body, skip))
Expand Down
11 changes: 6 additions & 5 deletions system/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"net/url"
"sort"
"strings"
"time"

Expand All @@ -15,7 +16,7 @@ import (
type HTTP interface {
HTTP() string
Status() (int, error)
Headers() (io.Reader, error)
Headers() ([]string, error)
Body() (io.Reader, error)
Exists() (bool, error)
SetAllowInsecure(bool)
Expand Down Expand Up @@ -61,9 +62,10 @@ func NewDefHTTP(httpStr string, system *System, config util.Config) HTTP {
func HeaderToArray(header http.Header) (res []string) {
for name, values := range header {
for _, value := range values {
res = append(res, fmt.Sprintf("%s: %s", name, value))
res = append(res, strings.ToLower(fmt.Sprintf("%s: %s", name, value)))
}
}
sort.Strings(res)
return
}

Expand Down Expand Up @@ -149,13 +151,12 @@ func (u *DefHTTP) Status() (int, error) {
return u.resp.StatusCode, nil
}

func (u *DefHTTP) Headers() (io.Reader, error) {
func (u *DefHTTP) Headers() ([]string, error) {
if err := u.setup(); err != nil {
return nil, err
}

var headerString = strings.Join(HeaderToArray(u.resp.Header), "\n")
return strings.NewReader(headerString), nil
return HeaderToArray(u.resp.Header), nil
}

func (u *DefHTTP) Body() (io.Reader, error) {
Expand Down

0 comments on commit 1fe5571

Please sign in to comment.