Skip to content

Commit

Permalink
add response body flag to replace individual flags
Browse files Browse the repository at this point in the history
  • Loading branch information
tednaleid committed Dec 30, 2023
1 parent ebba5c4 commit 04fd50f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
26 changes: 26 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
ctx "context"
"fmt"
"github.com/tednaleid/ganda/config"
"github.com/tednaleid/ganda/execcontext"
"github.com/tednaleid/ganda/parser"
Expand Down Expand Up @@ -50,6 +51,31 @@ func setupCommand(
Value: conf.BaseRetryDelayMillis,
Destination: &conf.BaseRetryDelayMillis,
},
&cli.StringFlag{
Name: "response-body",
Aliases: []string{"B"},
DefaultText: "raw",
Usage: "transforms the body of the response. Values: 'raw' (unchanged), 'base64', 'discard' (don't emit body), 'escaped' (JSON escaped string), 'sha256'",
// we are slightly abusing the validator as a setter because v3 of urfave/cli doesn't currently support generic flags
Validator: func(s string) error {
switch s {
case "", string(config.Raw):
conf.ResponseBody = config.Raw
case string(config.Base64):
conf.ResponseBody = config.Base64
case string(config.Discard):
conf.ResponseBody = config.Discard
case string(config.Escaped):
conf.ResponseBody = config.Escaped
case string(config.Sha256):
conf.ResponseBody = config.Sha256
return nil
default:
return fmt.Errorf("invalid response-body value: %s", s)
}
return nil
},
},
&cli.IntFlag{
Name: "connect-timeout-ms",
Usage: "number of milliseconds to wait for a connection to be established before timeout",
Expand Down
35 changes: 33 additions & 2 deletions cli/cli_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"github.com/stretchr/testify/assert"
"github.com/tednaleid/ganda/config"
"math"
"strconv"
"testing"
Expand Down Expand Up @@ -30,7 +31,7 @@ func TestWorkers(t *testing.T) {
assert.Equal(t, 10, results.context.ResponseWorkers)

separateResults, _ := ParseArgs([]string{"ganda", "-W", "10", "--response-workers", "5"})
assert.NotNil(t, results)
assert.NotNil(t, separateResults)
assert.Equal(t, 10, separateResults.context.RequestWorkers)
assert.Equal(t, 5, separateResults.context.ResponseWorkers)
}
Expand All @@ -41,7 +42,7 @@ func TestRetries(t *testing.T) {
assert.Equal(t, int64(0), results.context.Retries)

separateResults, _ := ParseArgs([]string{"ganda", "--retry", "5"})
assert.NotNil(t, results)
assert.NotNil(t, separateResults)
assert.Equal(t, int64(5), separateResults.context.Retries)
}

Expand All @@ -61,3 +62,33 @@ func TestInvalidWorkers(t *testing.T) {
assert.Contains(t, results.stderr, tc.error)
}
}

func TestResponseBody(t *testing.T) {
results, _ := ParseArgs([]string{"ganda"})
assert.NotNil(t, results)
assert.NotNil(t, results.context)
assert.Equal(t, config.Raw, results.context.ResponseBody)

testCases := []struct {
input string
expected config.ResponseBodyType
}{
{"base64", config.Base64},
{"discard", config.Discard},
{"escaped", config.Escaped},
{"raw", config.Raw},
{"sha256", config.Sha256},
}

for _, tc := range testCases {
shortResults, _ := ParseArgs([]string{"ganda", "-B", tc.input})
assert.NotNil(t, shortResults)
assert.NotNil(t, shortResults.context)
assert.Equal(t, tc.expected, shortResults.context.ResponseBody)

longResults, _ := ParseArgs([]string{"ganda", "--response-body", tc.input})
assert.NotNil(t, longResults)
assert.NotNil(t, longResults.context)
assert.Equal(t, tc.expected, longResults.context.ResponseBody)
}
}
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Config struct {
RequestMethod string
RequestWorkers int
ResponseWorkers int
ResponseBody ResponseBodyType
Retries int64
Silent bool
SubdirLength int64
Expand All @@ -39,6 +40,7 @@ func New() *Config {
JsonEnvelope: false,
RequestMethod: "GET",
RequestWorkers: 1,
ResponseBody: Raw,
Retries: 0,
Silent: false,
SubdirLength: 0,
Expand Down Expand Up @@ -76,3 +78,13 @@ func ConvertRequestHeaders(stringHeaders []string) ([]RequestHeader, error) {

return requestHeaders, nil
}

type ResponseBodyType string

const (
Base64 ResponseBodyType = "base64"
Discard ResponseBodyType = "discard"
Escaped ResponseBodyType = "escaped" // escaped to a valid JSON string
Sha256 ResponseBodyType = "sha256"
Raw ResponseBodyType = "raw"
)
2 changes: 2 additions & 0 deletions execcontext/execcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Context struct {
RequestHeaders []config.RequestHeader
RequestMethod string
RequestWorkers int
ResponseBody config.ResponseBodyType
ResponseWorkers int
Retries int64
SubdirLength int64
Expand All @@ -51,6 +52,7 @@ func New(conf *config.Config, in io.Reader, stderr io.Writer, stdout io.Writer)
RequestMethod: conf.RequestMethod,
RequestWorkers: conf.RequestWorkers,
RequestHeaders: conf.RequestHeaders,
ResponseBody: conf.ResponseBody,
ResponseWorkers: conf.ResponseWorkers,
Retries: conf.Retries,
SubdirLength: conf.SubdirLength,
Expand Down

0 comments on commit 04fd50f

Please sign in to comment.