Skip to content

Commit

Permalink
tests run everything together now
Browse files Browse the repository at this point in the history
  • Loading branch information
tednaleid committed May 4, 2024
1 parent 11b71dc commit ec28837
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
21 changes: 21 additions & 0 deletions cli/cli_echoserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net"
"strconv"
"strings"
"testing"
)

Expand Down Expand Up @@ -42,6 +43,26 @@ func TestEchoserverOverridePort(t *testing.T) {
assert.Equal(t, subcommand.Int("port"), int64(port))
}

// Runs the Echoserver and then runs ganda against it
func TestAllTogetherNow(t *testing.T) {
port := 9090
shutdownFunc := RunGandaAsync([]string{"ganda", "echoserver", "--port", strconv.Itoa(port)}, nil)

waitForPort(port)

url := fmt.Sprintf("http://localhost:%d/hello/world", port)

runResults, _ := RunGanda([]string{"ganda"}, strings.NewReader(url+"\n"))

runResults.assert(
t,
"hello/world\n",
"Response: 200 "+url+"\n",
)

shutdownFunc()
}

// RunGandaAsync will run ganda in a separate goroutine and return a function that can
// be called to cancel the ganda run and return the results
func RunGandaAsync(args []string, in io.Reader) func() GandaResults {
Expand Down
2 changes: 1 addition & 1 deletion cli/cli_response_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestResponseBodyWithJsonEnvelope(t *testing.T) {
}{
{"raw", config.Raw, "{ \"url\": \"" + server.urlFor("bar") + "\", \"code\": 200, \"body\": { \"foo\": \"/bar\" } }\n"},
{"discard", config.Discard, "{ \"url\": \"" + server.urlFor("bar") + "\", \"code\": 200, \"body\": null }\n"},
{"escaped", config.Escaped, "{ \"url\": \"" + server.urlFor("bar") + "\", \"code\": 200, \"body\": \"\"{ \\\"foo\\\": \\\"/bar\\\" }\"\" }\n"},
{"escaped", config.Escaped, "{ \"url\": \"" + server.urlFor("bar") + "\", \"code\": 200, \"body\": \"{ \\\"foo\\\": \\\"/bar\\\" }\" }\n"},
{"base64", config.Base64, "{ \"url\": \"" + server.urlFor("bar") + "\", \"code\": 200, \"body\": \"eyAiZm9vIjogIi9iYXIiIH0=\" }\n"},
{"sha256", config.Sha256, "{ \"url\": \"" + server.urlFor("bar") + "\", \"code\": 200, \"body\": \"f660cd1420c6acd9408932b9983909c26ab6cb21ffb40525670a7b7aa67092ec\" }\n"},
}
Expand Down
2 changes: 1 addition & 1 deletion responses/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func jsonEnvelopeResponseFn(bodyResponseFn emitResponseFn, responseBody config.R
}

// emit the body response
if responseBody == config.Discard || responseBody == config.Raw {
if responseBody == config.Discard || responseBody == config.Raw || responseBody == config.Escaped {
// no need to wrap either of these in quotes, Raw is assumed to be JSON
bodyBytesWritten, err = bodyResponseFn(response, out)
} else {
Expand Down
26 changes: 26 additions & 0 deletions responses/responses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,32 @@ func TestRawOutputJSON(t *testing.T) {
assert.Equal(t, "{ \"url\": \"http://example.com\", \"code\": 200, \"body\": \"hello world\" }", writeCloser.ToString())
}

func TestEscapedOutput(t *testing.T) {
responseFn := determineEmitResponseFn(config.Escaped)
assert.NotNil(t, responseFn)

mockResponse := NewMockResponseBodyOnly("hello world")
writeCloser := NewMockWriteCloser()

responseFn(&ResponseWithContext{Response: mockResponse.Response}, writeCloser)

assert.True(t, mockResponse.BodyClosed())
assert.Equal(t, "\"hello world\"", writeCloser.ToString())
}

func TestEscapedOutputJSON(t *testing.T) {
responseFn := determineEmitJsonResponseWithContextFn(config.Escaped)
assert.NotNil(t, responseFn)

mockResponse := NewMockResponseBodyOnly("\"hello world\"")
writeCloser := NewMockWriteCloser()

responseFn(&ResponseWithContext{Response: mockResponse.Response, RequestContext: nil}, writeCloser)

assert.True(t, mockResponse.BodyClosed())
assert.Equal(t, "{ \"url\": \"http://example.com\", \"code\": 200, \"body\": \"\\\"hello world\\\"\" }", writeCloser.ToString())
}

func TestDiscardOutput(t *testing.T) {
responseFn := determineEmitResponseFn(config.Discard)
assert.NotNil(t, responseFn)
Expand Down

0 comments on commit ec28837

Please sign in to comment.