fix: EnableHTTP2() -> HTTP2(); HTTP/2.0 -> HTTP/2

This commit is contained in:
buyuxiang
2022-04-08 09:27:02 +08:00
parent be7f7122d8
commit 62c2ac6939
3 changed files with 72 additions and 35 deletions

View File

@@ -12,7 +12,7 @@
- change: lock funplugin version when creating scaffold project - change: lock funplugin version when creating scaffold project
- fix: call referenced api/testcase with relative path - fix: call referenced api/testcase with relative path
- refactor: redesign `IStep` to make step extensible to support implementing new protocols and test types - refactor: redesign `IStep` to make step extensible to support implementing new protocols and test types
- feat: support HTTP/2.0 protocol - feat: support HTTP/2 protocol
**python version** **python version**

View File

@@ -39,7 +39,7 @@ const (
type Request struct { type Request struct {
Method HTTPMethod `json:"method" yaml:"method"` // required Method HTTPMethod `json:"method" yaml:"method"` // required
URL string `json:"url" yaml:"url"` // required URL string `json:"url" yaml:"url"` // required
EnableHTTP2 bool `json:"enable_HTTP2,omitempty" yaml:"enableHTTP2,omitempty"` HTTP2 bool `json:"http2,omitempty" yaml:"http2,omitempty"`
Params map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"` Params map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"`
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"` Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"`
Cookies map[string]string `json:"cookies,omitempty" yaml:"cookies,omitempty"` Cookies map[string]string `json:"cookies,omitempty" yaml:"cookies,omitempty"`
@@ -60,7 +60,7 @@ func newRequestBuilder(parser *Parser, config *TConfig, stepRequest *Request) *r
request := &http.Request{ request := &http.Request{
Header: make(http.Header), Header: make(http.Header),
} }
if stepRequest.EnableHTTP2 { if stepRequest.HTTP2 {
request.ProtoMajor = 2 request.ProtoMajor = 2
request.ProtoMinor = 0 request.ProtoMinor = 0
} else { } else {
@@ -326,7 +326,7 @@ func runStepRequest(r *SessionRunner, step *TStep) (stepResult *StepResult, err
// do request action // do request action
start := time.Now() start := time.Now()
var resp *http.Response var resp *http.Response
if step.Request.EnableHTTP2 { if step.Request.HTTP2 {
resp, err = r.hrpRunner.http2Client.Do(rb.req) resp, err = r.hrpRunner.http2Client.Do(rb.req)
} else { } else {
resp, err = r.hrpRunner.httpClient.Do(rb.req) resp, err = r.hrpRunner.httpClient.Do(rb.req)
@@ -490,11 +490,24 @@ func (s *StepRequest) SetupHook(hook string) *StepRequest {
return s return s
} }
// HTTP2 enables HTTP/2 protocol
func (s *StepRequest) HTTP2() *StepRequest {
s.step.Request = &Request{
HTTP2: true,
}
return s
}
// GET makes a HTTP GET request. // GET makes a HTTP GET request.
func (s *StepRequest) GET(url string) *StepRequestWithOptionalArgs { func (s *StepRequest) GET(url string) *StepRequestWithOptionalArgs {
s.step.Request = &Request{ if s.step.Request != nil {
Method: httpGET, s.step.Request.Method = httpGET
URL: url, s.step.Request.URL = url
} else {
s.step.Request = &Request{
Method: httpGET,
URL: url,
}
} }
return &StepRequestWithOptionalArgs{ return &StepRequestWithOptionalArgs{
step: s.step, step: s.step,
@@ -503,9 +516,14 @@ func (s *StepRequest) GET(url string) *StepRequestWithOptionalArgs {
// HEAD makes a HTTP HEAD request. // HEAD makes a HTTP HEAD request.
func (s *StepRequest) HEAD(url string) *StepRequestWithOptionalArgs { func (s *StepRequest) HEAD(url string) *StepRequestWithOptionalArgs {
s.step.Request = &Request{ if s.step.Request != nil {
Method: httpHEAD, s.step.Request.Method = httpHEAD
URL: url, s.step.Request.URL = url
} else {
s.step.Request = &Request{
Method: httpHEAD,
URL: url,
}
} }
return &StepRequestWithOptionalArgs{ return &StepRequestWithOptionalArgs{
step: s.step, step: s.step,
@@ -514,9 +532,14 @@ func (s *StepRequest) HEAD(url string) *StepRequestWithOptionalArgs {
// POST makes a HTTP POST request. // POST makes a HTTP POST request.
func (s *StepRequest) POST(url string) *StepRequestWithOptionalArgs { func (s *StepRequest) POST(url string) *StepRequestWithOptionalArgs {
s.step.Request = &Request{ if s.step.Request != nil {
Method: httpPOST, s.step.Request.Method = httpPOST
URL: url, s.step.Request.URL = url
} else {
s.step.Request = &Request{
Method: httpPOST,
URL: url,
}
} }
return &StepRequestWithOptionalArgs{ return &StepRequestWithOptionalArgs{
step: s.step, step: s.step,
@@ -525,9 +548,14 @@ func (s *StepRequest) POST(url string) *StepRequestWithOptionalArgs {
// PUT makes a HTTP PUT request. // PUT makes a HTTP PUT request.
func (s *StepRequest) PUT(url string) *StepRequestWithOptionalArgs { func (s *StepRequest) PUT(url string) *StepRequestWithOptionalArgs {
s.step.Request = &Request{ if s.step.Request != nil {
Method: httpPUT, s.step.Request.Method = httpPUT
URL: url, s.step.Request.URL = url
} else {
s.step.Request = &Request{
Method: httpPUT,
URL: url,
}
} }
return &StepRequestWithOptionalArgs{ return &StepRequestWithOptionalArgs{
step: s.step, step: s.step,
@@ -536,9 +564,14 @@ func (s *StepRequest) PUT(url string) *StepRequestWithOptionalArgs {
// DELETE makes a HTTP DELETE request. // DELETE makes a HTTP DELETE request.
func (s *StepRequest) DELETE(url string) *StepRequestWithOptionalArgs { func (s *StepRequest) DELETE(url string) *StepRequestWithOptionalArgs {
s.step.Request = &Request{ if s.step.Request != nil {
Method: httpDELETE, s.step.Request.Method = httpDELETE
URL: url, s.step.Request.URL = url
} else {
s.step.Request = &Request{
Method: httpDELETE,
URL: url,
}
} }
return &StepRequestWithOptionalArgs{ return &StepRequestWithOptionalArgs{
step: s.step, step: s.step,
@@ -547,9 +580,14 @@ func (s *StepRequest) DELETE(url string) *StepRequestWithOptionalArgs {
// OPTIONS makes a HTTP OPTIONS request. // OPTIONS makes a HTTP OPTIONS request.
func (s *StepRequest) OPTIONS(url string) *StepRequestWithOptionalArgs { func (s *StepRequest) OPTIONS(url string) *StepRequestWithOptionalArgs {
s.step.Request = &Request{ if s.step.Request != nil {
Method: httpOPTIONS, s.step.Request.Method = httpOPTIONS
URL: url, s.step.Request.URL = url
} else {
s.step.Request = &Request{
Method: httpOPTIONS,
URL: url,
}
} }
return &StepRequestWithOptionalArgs{ return &StepRequestWithOptionalArgs{
step: s.step, step: s.step,
@@ -558,9 +596,14 @@ func (s *StepRequest) OPTIONS(url string) *StepRequestWithOptionalArgs {
// PATCH makes a HTTP PATCH request. // PATCH makes a HTTP PATCH request.
func (s *StepRequest) PATCH(url string) *StepRequestWithOptionalArgs { func (s *StepRequest) PATCH(url string) *StepRequestWithOptionalArgs {
s.step.Request = &Request{ if s.step.Request != nil {
Method: httpPATCH, s.step.Request.Method = httpPATCH
URL: url, s.step.Request.URL = url
} else {
s.step.Request = &Request{
Method: httpPATCH,
URL: url,
}
} }
return &StepRequestWithOptionalArgs{ return &StepRequestWithOptionalArgs{
step: s.step, step: s.step,
@@ -670,12 +713,6 @@ func (s *StepRequestWithOptionalArgs) SetAuth(auth map[string]string) *StepReque
return s 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. // WithParams sets HTTP request params for current step.
func (s *StepRequestWithOptionalArgs) WithParams(params map[string]interface{}) *StepRequestWithOptionalArgs { func (s *StepRequestWithOptionalArgs) WithParams(params map[string]interface{}) *StepRequestWithOptionalArgs {
s.step.Request.Params = params s.step.Request.Params = params

View File

@@ -27,18 +27,18 @@ func TestProtocol(t *testing.T) {
AssertEqual("status_code", 200, "check status code"). AssertEqual("status_code", 200, "check status code").
AssertEqual("proto", "HTTP/1.1", "check protocol type"). AssertEqual("proto", "HTTP/1.1", "check protocol type").
AssertLengthEqual("body.json.foo1", 4, "check body foo1"), AssertLengthEqual("body.json.foo1", 4, "check body foo1"),
hrp.NewStep("HTTP2.0 get"). hrp.NewStep("HTTP/2 get").
HTTP2().
GET("/get"). GET("/get").
EnableHTTP2().
WithParams(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}). WithParams(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}).
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}). WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
Validate(). Validate().
AssertEqual("status_code", 200, "check status code"). AssertEqual("status_code", 200, "check status code").
AssertEqual("proto", "HTTP/2.0", "check protocol type"). AssertEqual("proto", "HTTP/2.0", "check protocol type").
AssertLengthEqual("body.args.foo1", 4, "check param foo1"), AssertLengthEqual("body.args.foo1", 4, "check param foo1"),
hrp.NewStep("HTTP/2.0 post"). hrp.NewStep("HTTP/2 post").
HTTP2().
POST("/post"). POST("/post").
EnableHTTP2().
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}). WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
WithBody(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}). WithBody(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}).
Validate(). Validate().