From a0d9bcaba68760b7b6265d3f609a5a59be980497 Mon Sep 17 00:00:00 2001 From: buyuxiang <347586493@qq.com> Date: Fri, 8 Apr 2022 09:27:02 +0800 Subject: [PATCH] fix: EnableHTTP2() -> HTTP2(); HTTP/2.0 -> HTTP/2 --- docs/CHANGELOG.md | 2 +- hrp/step_request.go | 97 ++++++++++++++++++++++++++------------ hrp/tests/protocol_test.go | 8 ++-- 3 files changed, 72 insertions(+), 35 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index acc4e594..58ce08e9 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -12,7 +12,7 @@ - change: lock funplugin version when creating scaffold project - fix: call referenced api/testcase with relative path - 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** diff --git a/hrp/step_request.go b/hrp/step_request.go index 9715b1b6..8b04cbf2 100644 --- a/hrp/step_request.go +++ b/hrp/step_request.go @@ -39,7 +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"` + HTTP2 bool `json:"http2,omitempty" yaml:"http2,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"` @@ -60,7 +60,7 @@ func newRequestBuilder(parser *Parser, config *TConfig, stepRequest *Request) *r request := &http.Request{ Header: make(http.Header), } - if stepRequest.EnableHTTP2 { + if stepRequest.HTTP2 { request.ProtoMajor = 2 request.ProtoMinor = 0 } else { @@ -326,7 +326,7 @@ func runStepRequest(r *SessionRunner, step *TStep) (stepResult *StepResult, err // do request action start := time.Now() var resp *http.Response - if step.Request.EnableHTTP2 { + if step.Request.HTTP2 { resp, err = r.hrpRunner.http2Client.Do(rb.req) } else { resp, err = r.hrpRunner.httpClient.Do(rb.req) @@ -490,11 +490,24 @@ func (s *StepRequest) SetupHook(hook string) *StepRequest { 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. func (s *StepRequest) GET(url string) *StepRequestWithOptionalArgs { - s.step.Request = &Request{ - Method: httpGET, - URL: url, + if s.step.Request != nil { + s.step.Request.Method = httpGET + s.step.Request.URL = url + } else { + s.step.Request = &Request{ + Method: httpGET, + URL: url, + } } return &StepRequestWithOptionalArgs{ step: s.step, @@ -503,9 +516,14 @@ func (s *StepRequest) GET(url string) *StepRequestWithOptionalArgs { // HEAD makes a HTTP HEAD request. func (s *StepRequest) HEAD(url string) *StepRequestWithOptionalArgs { - s.step.Request = &Request{ - Method: httpHEAD, - URL: url, + if s.step.Request != nil { + s.step.Request.Method = httpHEAD + s.step.Request.URL = url + } else { + s.step.Request = &Request{ + Method: httpHEAD, + URL: url, + } } return &StepRequestWithOptionalArgs{ step: s.step, @@ -514,9 +532,14 @@ func (s *StepRequest) HEAD(url string) *StepRequestWithOptionalArgs { // POST makes a HTTP POST request. func (s *StepRequest) POST(url string) *StepRequestWithOptionalArgs { - s.step.Request = &Request{ - Method: httpPOST, - URL: url, + if s.step.Request != nil { + s.step.Request.Method = httpPOST + s.step.Request.URL = url + } else { + s.step.Request = &Request{ + Method: httpPOST, + URL: url, + } } return &StepRequestWithOptionalArgs{ step: s.step, @@ -525,9 +548,14 @@ func (s *StepRequest) POST(url string) *StepRequestWithOptionalArgs { // PUT makes a HTTP PUT request. func (s *StepRequest) PUT(url string) *StepRequestWithOptionalArgs { - s.step.Request = &Request{ - Method: httpPUT, - URL: url, + if s.step.Request != nil { + s.step.Request.Method = httpPUT + s.step.Request.URL = url + } else { + s.step.Request = &Request{ + Method: httpPUT, + URL: url, + } } return &StepRequestWithOptionalArgs{ step: s.step, @@ -536,9 +564,14 @@ func (s *StepRequest) PUT(url string) *StepRequestWithOptionalArgs { // DELETE makes a HTTP DELETE request. func (s *StepRequest) DELETE(url string) *StepRequestWithOptionalArgs { - s.step.Request = &Request{ - Method: httpDELETE, - URL: url, + if s.step.Request != nil { + s.step.Request.Method = httpDELETE + s.step.Request.URL = url + } else { + s.step.Request = &Request{ + Method: httpDELETE, + URL: url, + } } return &StepRequestWithOptionalArgs{ step: s.step, @@ -547,9 +580,14 @@ func (s *StepRequest) DELETE(url string) *StepRequestWithOptionalArgs { // OPTIONS makes a HTTP OPTIONS request. func (s *StepRequest) OPTIONS(url string) *StepRequestWithOptionalArgs { - s.step.Request = &Request{ - Method: httpOPTIONS, - URL: url, + if s.step.Request != nil { + s.step.Request.Method = httpOPTIONS + s.step.Request.URL = url + } else { + s.step.Request = &Request{ + Method: httpOPTIONS, + URL: url, + } } return &StepRequestWithOptionalArgs{ step: s.step, @@ -558,9 +596,14 @@ func (s *StepRequest) OPTIONS(url string) *StepRequestWithOptionalArgs { // PATCH makes a HTTP PATCH request. func (s *StepRequest) PATCH(url string) *StepRequestWithOptionalArgs { - s.step.Request = &Request{ - Method: httpPATCH, - URL: url, + if s.step.Request != nil { + s.step.Request.Method = httpPATCH + s.step.Request.URL = url + } else { + s.step.Request = &Request{ + Method: httpPATCH, + URL: url, + } } return &StepRequestWithOptionalArgs{ step: s.step, @@ -670,12 +713,6 @@ 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 diff --git a/hrp/tests/protocol_test.go b/hrp/tests/protocol_test.go index c202ffec..bdf128d5 100644 --- a/hrp/tests/protocol_test.go +++ b/hrp/tests/protocol_test.go @@ -27,18 +27,18 @@ func TestProtocol(t *testing.T) { AssertEqual("status_code", 200, "check status code"). AssertEqual("proto", "HTTP/1.1", "check protocol type"). AssertLengthEqual("body.json.foo1", 4, "check body foo1"), - hrp.NewStep("HTTP2.0 get"). + hrp.NewStep("HTTP/2 get"). + HTTP2(). GET("/get"). - EnableHTTP2(). WithParams(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}). WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}). Validate(). AssertEqual("status_code", 200, "check status code"). AssertEqual("proto", "HTTP/2.0", "check protocol type"). AssertLengthEqual("body.args.foo1", 4, "check param foo1"), - hrp.NewStep("HTTP/2.0 post"). + hrp.NewStep("HTTP/2 post"). + HTTP2(). POST("/post"). - EnableHTTP2(). WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}). WithBody(map[string]interface{}{"foo1": "foo1", "foo2": "foo2"}). Validate().