Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

adding --body-regex option #42

Merged
merged 3 commits into from
Jun 15, 2020
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Available individual target settings:
- Timeout (default 10s)
- Method (default GET)
- Body (default empty)
- RegexBody (default false)
- BodyFilename (default none)
- Headers (default none)
- Cookies (default none)
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func init() {
RootCmd.PersistentFlags().StringP("timeout", "t", "10s", "Maximum seconds to wait for response")
RootCmd.PersistentFlags().StringP("request-method", "X", "GET", "Request type. GET, HEAD, POST, PUT, etc.")
RootCmd.PersistentFlags().String("body", "", "String to use as request body e.g. POST body.")
RootCmd.PersistentFlags().BoolP("body-regex", "", false, "Interpret Body as regular expressions.")
RootCmd.PersistentFlags().String("body-file", "", "Path to file to use as request body. Will overwrite --body if both are present.")
RootCmd.PersistentFlags().StringP("headers", "H", "", "Add arbitrary header line, eg. 'Accept-Encoding:gzip, Content-Type:application/json'")
RootCmd.PersistentFlags().String("cookies", "", "Add request cookies, eg. 'data=123; session=456'")
Expand Down
4 changes: 4 additions & 0 deletions cmd/stress.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var stressCmd = &cobra.Command{
stressCfg.Targets[i].Timeout = viper.GetString("timeout")
stressCfg.Targets[i].Method = viper.GetString("request-method")
stressCfg.Targets[i].Body = viper.GetString("body")
stressCfg.Targets[i].RegexBody = viper.GetBool("body-regex")
stressCfg.Targets[i].BodyFilename = viper.GetString("body-file")
stressCfg.Targets[i].Headers = viper.GetString("headers")
stressCfg.Targets[i].Cookies = viper.GetString("cookies")
Expand Down Expand Up @@ -85,6 +86,9 @@ var stressCmd = &cobra.Command{
if _, set := targetMapVals["Body"]; !set {
stressCfg.Targets[i].Body = viper.GetString("body")
}
if _, set := targetMapVals["RegexBody"]; !set {
stressCfg.Targets[i].RegexBody = viper.GetBool("body-regex")
}
if _, set := targetMapVals["BodyFilename"]; !set {
stressCfg.Targets[i].BodyFilename = viper.GetString("bodyFile")
}
Expand Down
3 changes: 3 additions & 0 deletions lib/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type (
Method string
//String that is the content of the HTTP body. Empty string is no body.
Body string
// Whether or not to interpret the Body as regular expression string
// and generate actual body from that
RegexBody bool
//A location on disk to read the HTTP body from. Empty string means it will not be read.
BodyFilename string
Headers string
Expand Down
9 changes: 8 additions & 1 deletion lib/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ func buildRequest(t Target) (http.Request, error) {
}
req, err = http.NewRequest(t.Method, URL.String(), bytes.NewBuffer(fileContents))
} else if t.Body != "" {
req, err = http.NewRequest(t.Method, URL.String(), bytes.NewBuffer([]byte(t.Body)))
bodyStr := t.Body
if t.RegexBody {
bodyStr, err = reggen.Generate(t.Body, 10)
if err != nil {
return http.Request{}, errors.New("failed to parse regex: " + err.Error())
}
}
req, err = http.NewRequest(t.Method, URL.String(), bytes.NewBuffer([]byte(bodyStr)))
} else {
req, err = http.NewRequest(t.Method, URL.String(), nil)
}
Expand Down