Fixed bugs with progress and http path / handling

dev
Ice3man543 2021-01-12 02:00:11 +05:30
parent d4191814c7
commit 4d800d8c0c
6 changed files with 31 additions and 23 deletions

View File

@ -33,7 +33,7 @@ func (e *Executer) Compile() error {
func (e *Executer) Requests() int {
var count int
for _, request := range e.requests {
count += int(request.Requests())
count += request.Requests()
}
return count
}

View File

@ -111,9 +111,6 @@ func (r *requestGenerator) Make(baseURL string, dynamicValues map[string]interfa
}
ctx := context.Background()
if strings.HasSuffix(baseURL, "/") {
baseURL = strings.TrimSuffix(baseURL, "/")
}
parsed, err := url.Parse(baseURL)
if err != nil {
return nil, err
@ -158,6 +155,9 @@ func baseURLWithTemplatePrefs(data string, parsedURL *url.URL) string {
// MakeHTTPRequestFromModel creates a *http.Request from a request template
func (r *requestGenerator) makeHTTPRequestFromModel(ctx context.Context, data string, values map[string]interface{}) (*generatedRequest, error) {
if strings.HasSuffix(values["BaseURL"].(string), "/") {
data = strings.TrimPrefix(data, "/")
}
URL := replacer.New(values).Replace(data)
// Build a request on the specified URL
@ -190,24 +190,22 @@ func (r *requestGenerator) makeHTTPRequestFromRaw(ctx context.Context, baseURL,
}
// handleRawWithPaylods handles raw requests along with paylaods
func (r *requestGenerator) handleRawWithPaylods(ctx context.Context, rawRequest, baseURL string, values, genValues map[string]interface{}) (*generatedRequest, error) {
func (r *requestGenerator) handleRawWithPaylods(ctx context.Context, rawRequest, baseURL string, values, generatorValues map[string]interface{}) (*generatedRequest, error) {
baseValues := generators.CopyMap(values)
finValues := generators.MergeMaps(baseValues, genValues)
finalValues := generators.MergeMaps(baseValues, generatorValues)
// Replace the dynamic variables in the URL if any
rawRequest = replacer.New(finValues).Replace(rawRequest)
rawRequest = replacer.New(finalValues).Replace(rawRequest)
dynamicValues := make(map[string]interface{})
for _, match := range templateExpressionRegex.FindAllString(rawRequest, -1) {
// check if the match contains a dynamic variable
expr := generators.TrimDelimiters(match)
compiled, err := govaluate.NewEvaluableExpressionWithFunctions(expr, dsl.HelperFunctions())
if err != nil {
return nil, err
}
result, err := compiled.Evaluate(finValues)
result, err := compiled.Evaluate(finalValues)
if err != nil {
return nil, err
}
@ -223,7 +221,7 @@ func (r *requestGenerator) handleRawWithPaylods(ctx context.Context, rawRequest,
// rawhttp
if r.request.Unsafe {
unsafeReq := &generatedRequest{rawRequest: rawRequestData, meta: genValues, original: r.request}
unsafeReq := &generatedRequest{rawRequest: rawRequestData, meta: generatorValues, original: r.request}
return unsafeReq, nil
}
@ -250,7 +248,7 @@ func (r *requestGenerator) handleRawWithPaylods(ctx context.Context, rawRequest,
if err != nil {
return nil, err
}
return &generatedRequest{request: request, meta: genValues, original: r.request}, nil
return &generatedRequest{request: request, meta: generatorValues, original: r.request}, nil
}
// fillRequest fills various headers in the request with values

View File

@ -114,11 +114,22 @@ func (r *Request) Compile(options *protocols.ExecuterOptions) error {
// Requests returns the total number of requests the YAML rule will perform
func (r *Request) Requests() int {
if r.generator != nil {
payloadRequests := r.generator.NewIterator().Total()
return len(r.Raw) * payloadRequests
payloadRequests := r.generator.NewIterator().Total() * len(r.Raw)
if r.Threads != 0 {
payloadRequests = payloadRequests * r.Threads
}
return payloadRequests
}
if len(r.Raw) > 0 {
return len(r.Raw)
requests := len(r.Raw)
if r.Threads != 0 {
requests = requests * r.Threads
}
return requests
}
return len(r.Path)
requests := len(r.Path)
if r.Threads != 0 {
requests = requests * r.Threads
}
return requests
}

View File

@ -94,12 +94,11 @@ func Parse(request, baseURL string, unsafe bool) (*Request, error) {
if rawRequest.Path == "" {
rawRequest.Path = parsedURL.Path
} else if strings.HasPrefix(rawRequest.Path, "?") {
// requests generated from http.ReadRequest have incorrect RequestURI, so they
// cannot be used to perform another request directly, we need to generate a new one
// with the new target url
rawRequest.Path = fmt.Sprintf("%s%s", parsedURL.Path, rawRequest.Path)
}
if strings.HasSuffix(baseURL, "/") {
rawRequest.Path = strings.TrimPrefix(rawRequest.Path, "/")
}
rawRequest.FullURL = fmt.Sprintf("%s://%s%s", parsedURL.Scheme, strings.TrimSpace(hostURL), rawRequest.Path)
// Set the request body

View File

@ -264,8 +264,8 @@ func (r *Request) executeRequest(reqURL string, request *generatedRequest, dynam
}
}
if err != nil {
if resp != nil {
_, _ = io.Copy(ioutil.Discard, resp.Body)
if resp != nil && resp.Body != nil {
// _, _ = io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
r.options.Output.Request(r.options.TemplateID, reqURL, "http", err)

View File

@ -74,11 +74,11 @@ func Parse(filePath string, options *protocols.ExecuterOptions) (*Template, erro
}
if template.Executer != nil {
template.TotalRequests += template.Executer.Requests()
err := template.Executer.Compile()
if err != nil {
return nil, errors.Wrap(err, "could not compile request")
}
template.TotalRequests += template.Executer.Requests()
}
return template, nil
}