diff options
| author | Shulhan <ms@kilabit.info> | 2024-01-25 18:59:23 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-01-25 18:59:23 +0700 |
| commit | 7d3ce2ca01f2d4954d89430e48ca23c89416668b (patch) | |
| tree | 4f6e5eaccf08816e8bd36bd03c37d2729e9978e3 /http_target.go | |
| parent | ae60ff83f53d14bb9427c02695414afb20147051 (diff) | |
| download | gorankusu-7d3ce2ca01f2d4954d89430e48ca23c89416668b.tar.xz | |
all: support parameter binding in HTTP Path
If HTTP Path contains key, for example "/:book", and the Params contains
the same key, the Path will be filled with value from Params.
The same key in Params will be deleted and not send on query parameter
or body.
Diffstat (limited to 'http_target.go')
| -rw-r--r-- | http_target.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/http_target.go b/http_target.go index 8e42309..0ce1265 100644 --- a/http_target.go +++ b/http_target.go @@ -5,6 +5,7 @@ package trunks import ( "fmt" + "log" "os" "path/filepath" "sort" @@ -13,6 +14,7 @@ import ( libhttp "github.com/shuLhan/share/lib/http" "github.com/shuLhan/share/lib/mlog" + libpath "github.com/shuLhan/share/lib/path" vegeta "github.com/tsenart/vegeta/v12/lib" ) @@ -149,6 +151,44 @@ func (ht *HttpTarget) getResultByName(name string) (result *AttackResult) { return nil } +// paramsToPath fill the key in path with value from parameter. +// The key is sub-path that start with ":", for example "/:name", the key is +// name. +// The key in Params will be deleted if its exists in Path. +func (ht *HttpTarget) paramsToPath() { + var ( + logp = `paramsToPath` + + rute *libpath.Route + err error + ) + + rute, err = libpath.NewRoute(ht.Path) + if err != nil { + log.Printf(`%s %q: %s`, logp, ht.ID, err) + return + } + + var ( + fin FormInput + key string + ok bool + ) + + for _, key = range rute.Keys() { + fin, ok = ht.Params[key] + if !ok { + continue + } + ok = rute.Set(key, fin.Value) + if ok { + delete(ht.Params, key) + } + } + + ht.Path = rute.String() +} + func (ht *HttpTarget) sortResults() { sort.Slice(ht.Results, func(x, y int) bool { return ht.Results[x].Name > ht.Results[y].Name |
