Skip to content

Commit

Permalink
test: fix tests for header and client
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Zipitria <felipe.zipitria@owasp.org>
  • Loading branch information
fzipi committed Jun 10, 2023
1 parent a40334c commit c864424
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
27 changes: 11 additions & 16 deletions ftwhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ func (s *clientTestSuite) TearDownTest() {

func (s *clientTestSuite) httpHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.URL.Path == "/not-found" {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusOK)
}
resp := new(bytes.Buffer)
for key, value := range r.Header {
_, err := fmt.Fprintf(resp, "%s=%s,", key, value)
Expand All @@ -53,6 +57,9 @@ func (s *clientTestSuite) httpHandler() http.HandlerFunc {
}

func (s *clientTestSuite) httpTestServer(secure bool) {
s.HTTPStatusCode(s.httpHandler(), http.MethodGet, "/", nil, http.StatusOK)
s.HTTPStatusCode(s.httpHandler(), http.MethodGet, "/not-found", nil, http.StatusNotFound)

if secure {
s.ts = httptest.NewTLSServer(s.httpHandler())
} else {
Expand Down Expand Up @@ -80,24 +87,12 @@ func (s *clientTestSuite) TestDoRequest() {
s.NoError(err, "This should not error")
s.client.SetRootCAs(s.ts.Client().Transport.(*http.Transport).TLSClientConfig.RootCAs)
req := generateBaseRequestForTesting()
req.requestLine.URI = "/not-found"
err = s.client.NewConnection(*d)
s.NoError(err, "This should not error")

response, err := s.client.Do(*req)

// I'm getting consistently 400 Bad Request from the httpsTestServer's response,
// so I'm commenting this out for now.
// Example call used to test this:
// nc httpbin.org 80
// UNEXISTENT /bad/path HTTP/1.4
// Host: httpbin.org
// User-Agent: curl/7.88.1
// Accept: */*
//
// Will return HTTP/1.1 404 NOT FOUND from upstream.
// The same call to the httpsTestServer will return HTTP/1.1 400 Bad Request.
s.NoError(err, "This should not error")
s.Equal(response.Parsed.StatusCode, http.StatusBadRequest, "Error in calling website")
s.NoError(err, "This should error")
s.Equal(http.StatusNotFound, response.Parsed.StatusCode, "Error in calling website")
}

func (s *clientTestSuite) TestGetTrackedTime() {
Expand Down
3 changes: 3 additions & 0 deletions ftwhttp/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"io"
"sort"

"github.com/rs/zerolog/log"
)

const (
Expand Down Expand Up @@ -97,6 +99,7 @@ func (h Header) WriteBytes(b *bytes.Buffer) (int, error) {
for _, key := range sorted {
// we want all headers "as-is"
s := key + ": " + h[key] + "\r\n"
log.Info().Msgf("Writing header: %s", s)
n, err := b.Write([]byte(s))
count += n
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions ftwhttp/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ package ftwhttp
import (
"bytes"
"errors"
"github.com/rs/zerolog/log"
"io"
"testing"

Expand Down Expand Up @@ -70,11 +69,10 @@ func TestHeaderTestSuite(t *testing.T) {

func (s *headerTestSuite) TestHeaderWrite() {
for _, test := range headerWriteTests {
sorted := test.h.getSortedHeadersByName()
err := test.h.Write(io.Discard)
s.NoError(err)
err = test.h.Write(BadWriter{err: errors.New("fake error")})
if len(sorted) > 0 {
if len(test.h) > 0 {
s.EqualErrorf(err, "fake error", "Write: got %v, want %v", err, "fake error")
} else {
s.NoErrorf(err, "Write: got %v", err)
Expand All @@ -83,14 +81,14 @@ func (s *headerTestSuite) TestHeaderWrite() {
}

func (s *headerTestSuite) TestHeaderWriteBytes() {
var buf bytes.Buffer
for i, test := range headerWriteTests {
var buf bytes.Buffer

n, err := test.h.WriteBytes(&buf)
w := buf.Bytes()
log.Info().Msgf("buf: %s", w)
w := buf.String()
s.Lenf(w, n, "#%d: WriteBytes: got %d, want %d", i, n, len(w))
s.NoErrorf(err, "#%d: WriteBytes: got %v", i, err)
s.Equalf(test.expected, string(w), "#%d: WriteBytes: got %q, want %q", i, w, test.expected)
s.Equalf(test.expected, w, "#%d: WriteBytes: got %q, want %q", i, w, test.expected)
buf.Reset()
}
}
Expand All @@ -117,10 +115,12 @@ func (s *headerTestSuite) TestHeaderSetGet() {

func (s *headerTestSuite) TestHeaderDel() {
for i, test := range headerWriteTests {
expected := test.h.Get("Content-Type")
// we clone it because we are modifying the original
headerCopy := test.h.Clone()
expected := headerCopy.Get("Content-Type")
if expected != "" {
test.h.Del("Content-Type")
value := test.h.Get("Content-Type")
headerCopy.Del("Content-Type")
value := headerCopy.Get("Content-Type")
s.Equalf("", value, "#%d: got: %s, want: %s\n", i, value, "")
}
}
Expand Down
4 changes: 2 additions & 2 deletions ftwhttp/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func generateBaseRequestForTesting() *Request {
rl := &RequestLine{
Method: "UNEXISTENT",
URI: "/this/path",
Version: "1.4",
Version: "HTTP/1.4",
}

h := Header{"This": "Header", "Connection": "Not-Closed"}
h := Header{"Host": "localhost", "This": "Header", "Connection": "Not-Closed"}

req = NewRequest(rl, h, []byte("Data"), true)

Expand Down

0 comments on commit c864424

Please sign in to comment.