github.com/yandex/pandora@v0.5.32/components/guns/http_scenario/ammo.go (about)

     1  package httpscenario
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"time"
     7  
     8  	"github.com/yandex/pandora/components/providers/scenario"
     9  )
    10  
    11  type SourceStorage interface {
    12  	Variables() map[string]any
    13  }
    14  
    15  type Scenario struct {
    16  	Requests        []Request
    17  	ID              uint64
    18  	Name            string
    19  	MinWaitingTime  time.Duration
    20  	VariableStorage SourceStorage
    21  }
    22  
    23  func (a *Scenario) SetID(id uint64) {
    24  	a.ID = id
    25  }
    26  
    27  func (a *Scenario) Clone() scenario.ProvAmmo {
    28  	return &Scenario{
    29  		Requests:        a.Requests,
    30  		Name:            a.Name,
    31  		MinWaitingTime:  a.MinWaitingTime,
    32  		VariableStorage: a.VariableStorage,
    33  	}
    34  }
    35  
    36  type Request struct {
    37  	Method         string
    38  	Headers        map[string]string
    39  	Tag            string
    40  	Body           *string
    41  	Name           string
    42  	URI            string
    43  	Preprocessor   Preprocessor
    44  	Postprocessors []Postprocessor
    45  	Templater      Templater
    46  	Sleep          time.Duration
    47  }
    48  
    49  func (r *Request) GetBody() []byte {
    50  	if r.Body == nil {
    51  		return nil
    52  	}
    53  	return []byte(*r.Body)
    54  }
    55  
    56  func (r *Request) GetHeaders() map[string]string {
    57  	result := make(map[string]string, len(r.Headers))
    58  	for k, v := range r.Headers {
    59  		result[k] = v
    60  	}
    61  	return result
    62  }
    63  
    64  type Preprocessor interface {
    65  	// Process is called before request is sent
    66  	// templateVars - variables from template. Can be modified
    67  	// sourceVars - variables from sources. Must NOT be modified
    68  	Process(templateVars map[string]any) (map[string]any, error)
    69  }
    70  
    71  type Postprocessor interface {
    72  	Process(resp *http.Response, body io.Reader) (map[string]any, error)
    73  }
    74  
    75  type RequestParts struct {
    76  	URL     string
    77  	Method  string
    78  	Body    []byte
    79  	Headers map[string]string
    80  }