feat: support HTTP/2.0

This commit is contained in:
buyuxiang
2022-04-07 21:40:37 +08:00
parent 8aabf5a422
commit e6ca2ac5cd
9 changed files with 165 additions and 75 deletions

View File

@@ -39,6 +39,7 @@ const (
type Request struct {
Method HTTPMethod `json:"method" yaml:"method"` // required
URL string `json:"url" yaml:"url"` // required
EnableHTTP2 bool `json:"enable_HTTP2,omitempty" yaml:"enableHTTP2,omitempty"`
Params map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"`
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"`
Cookies map[string]string `json:"cookies,omitempty" yaml:"cookies,omitempty"`
@@ -56,17 +57,23 @@ func newRequestBuilder(parser *Parser, config *TConfig, stepRequest *Request) *r
var requestMap map[string]interface{}
_ = json.Unmarshal(jsonRequest, &requestMap)
request := &http.Request{
Header: make(http.Header),
}
if stepRequest.EnableHTTP2 {
request.ProtoMajor = 2
request.ProtoMinor = 0
} else {
request.ProtoMajor = 1
request.ProtoMinor = 1
}
return &requestBuilder{
stepRequest: stepRequest,
req: &http.Request{
Header: make(http.Header),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
},
config: config,
parser: parser,
requestMap: requestMap,
req: request,
config: config,
parser: parser,
requestMap: requestMap,
}
}
@@ -318,7 +325,13 @@ func runStepRequest(r *SessionRunner, step *TStep) (stepResult *StepResult, err
// do request action
start := time.Now()
resp, err := r.hrpRunner.client.Do(rb.req)
var resp *http.Response
if step.Request.EnableHTTP2 {
resp, err = r.hrpRunner.http2Client.Do(rb.req)
} else {
resp, err = r.hrpRunner.httpClient.Do(rb.req)
}
stepResult.Elapsed = time.Since(start).Milliseconds()
if err != nil {
return stepResult, errors.Wrap(err, "do request failed")
@@ -612,6 +625,16 @@ func (s *StepRequest) SetThinkTime(time float64) *StepThinkTime {
}
}
// SetRendezvous creates a new rendezvous
func (s *StepRequest) SetRendezvous(name string) *StepRendezvous {
s.step.Rendezvous = &Rendezvous{
Name: name,
}
return &StepRendezvous{
step: s.step,
}
}
// StepRequestWithOptionalArgs implements IStep interface.
type StepRequestWithOptionalArgs struct {
step *TStep
@@ -647,6 +670,12 @@ func (s *StepRequestWithOptionalArgs) SetAuth(auth map[string]string) *StepReque
return s
}
// EnableHTTP2 enables HTTP/2.0 protocol
func (s *StepRequestWithOptionalArgs) EnableHTTP2() *StepRequestWithOptionalArgs {
s.step.Request.EnableHTTP2 = true
return s
}
// WithParams sets HTTP request params for current step.
func (s *StepRequestWithOptionalArgs) WithParams(params map[string]interface{}) *StepRequestWithOptionalArgs {
s.step.Request.Params = params