Skip to content

Commit

Permalink
adding --body-regex option (#42)
Browse files Browse the repository at this point in the history
* added option to generate body from regex

Co-authored-by: Hani Zakher <zakher@oculeus.de>
  • Loading branch information
hzakher and zakher1980 authored Jun 15, 2020
1 parent 312f138 commit 57c99d7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,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 @@ -32,6 +32,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

0 comments on commit 57c99d7

Please # to comment.