Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions check/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ func TestAssertResponseTextFailOK(t *testing.T) {
assert.Falsef(t, c.AssertResponseContains(e.response), "response shouldn't contain text %v", e.response)
}
}

func TestAssertResponseTextChecksFullResponseOK(t *testing.T) {
cfg, err := config.NewConfigFromString(yamlApacheConfig)
assert.NoError(t, err)

c := NewCheck(cfg)
for _, e := range expectedResponseOKTests {
c.SetExpectResponse(e.expected)
assert.Truef(t, c.AssertResponseContains(e.response), "unexpected response: %v", e.response)
}
}
4 changes: 2 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var yamlConfig = `---
logfile: 'tests/logs/modsec2-apache/apache2/error.log'
testoverride:
input:
dest_addr: 'httpbin.org'
dest_addr: 'httpbingo.org'
port: '1234'
ignore:
'920400-1$': 'This test must be ignored'
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestNewConfigConfig(t *testing.T) {

overrides := cfg.TestOverride.Overrides
assert.NotNil(t, overrides.DestAddr, "Looks like we are not overriding destination address")
assert.Equal(t, "httpbin.org", *overrides.DestAddr, "Looks like we are not overriding destination address")
assert.Equal(t, "httpbingo.org", *overrides.DestAddr, "Looks like we are not overriding destination address")
}

func TestNewConfigBadConfig(t *testing.T) {
Expand Down
14 changes: 3 additions & 11 deletions ftwhttp/response.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package ftwhttp

import (
"io"
)

// GetBodyAsString gives the response body as string, or nil if there was some error
func (r *Response) GetBodyAsString() string {
body, err := io.ReadAll(r.Parsed.Body)
if err != nil {
return ""
}
return string(body)
// GetFullResponse gives the full response as string, or nil if there was some error
func (r *Response) GetFullResponse() string {
return string(r.RAW)
}
47 changes: 45 additions & 2 deletions ftwhttp/response_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ftwhttp

import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -67,6 +68,25 @@ func testServer() (server *httptest.Server) {
return ts
}

// Error checking omitted for brevity
func testEchoServer(t *testing.T) (server *httptest.Server) {

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Powered-By", "go-ftw")
w.WriteHeader(http.StatusOK)
resp := new(bytes.Buffer)
for key, value := range r.Header {
_, err := fmt.Fprintf(resp, "%s=%s,", key, value)
assert.NoError(t, err)
}

_, err := w.Write(resp.Bytes())
assert.NoError(t, err)
}))

return ts
}

func testServerWithCookies() (server *httptest.Server) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expiration := time.Now().Add(365 * 24 * time.Hour)
Expand Down Expand Up @@ -96,7 +116,7 @@ func TestResponse(t *testing.T) {
response, err := client.Do(*req)
assert.NoError(t, err)

assert.Equal(t, "Hello, client\n", response.GetBodyAsString())
assert.Contains(t, response.GetFullResponse(), "Hello, client\n")
}

func TestResponseWithCookies(t *testing.T) {
Expand All @@ -118,11 +138,34 @@ func TestResponseWithCookies(t *testing.T) {

assert.NoError(t, err)

assert.Equal(t, "Setting Cookies!\n", response.GetBodyAsString())
assert.Contains(t, response.GetFullResponse(), "Setting Cookies!\n")

cookiereq := generateRequestWithCookiesForTesting()

_, err = client.Do(*cookiereq)

assert.NoError(t, err)
}

func TestResponseChecksFullResponse(t *testing.T) {
server := testEchoServer(t)

defer server.Close()

d, err := DestinationFromString(server.URL)
assert.NoError(t, err)
req := generateRequestForTesting(true)

client, err := NewClient(NewClientConfig())
assert.NoError(t, err)
err = client.NewConnection(*d)

assert.NoError(t, err)

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

assert.NoError(t, err)

assert.Contains(t, response.GetFullResponse(), "X-Powered-By: go-ftw")
assert.Contains(t, response.GetFullResponse(), "User-Agent=[Go Tests]")
}
6 changes: 3 additions & 3 deletions runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func RunStage(runContext *TestRunContext, ftwCheck *check.FTWCheck, testCase tes
func markAndFlush(runContext *TestRunContext, dest *ftwhttp.Destination, stageID string) ([]byte, error) {
rline := &ftwhttp.RequestLine{
Method: "GET",
// Use the `/status` endpoint of `httpbin` (http://httpbin.org), if possible,
// Use the `/status` endpoint of `httpbin` (http://httpbingo.org), if possible,
// to minimize the amount of data transferred and in the log.
// `httpbin` is used by the CRS test setup.
URI: "/status/200",
Expand Down Expand Up @@ -333,8 +333,8 @@ func checkResult(c *check.FTWCheck, response *ftwhttp.Response, responseError er
if c.StatusCodeRequired() && !c.AssertStatus(response.Parsed.StatusCode) {
return Failed
}
// Check response
if c.ResponseContainsRequired() && !c.AssertResponseContains(response.GetBodyAsString()) {
// Check if text is contained in the full raw response
if c.ResponseContainsRequired() && !c.AssertResponseContains(response.GetFullResponse()) {
return Failed
}
}
Expand Down