From 865c753ea91387ed112e54a7366d3c4246d3f575 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 18 Feb 2022 18:50:39 +0800 Subject: [PATCH 1/3] fix: omit pseudo header names for HTTP/1, e.g. :authority --- docs/CHANGELOG.md | 4 ++++ runner.go | 21 ++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index e244f80c..9409946e 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,9 @@ # Release History +## v0.6.2 (2022-02-18) + +- fix: omit pseudo header names for HTTP/1, e.g. :authority + ## v0.6.1 (2022-02-17) - fix: set request Content-Type for posting json only when not specified diff --git a/runner.go b/runner.go index 8f2ca646..6e0ff4eb 100644 --- a/runner.go +++ b/runner.go @@ -628,6 +628,7 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro ProtoMinor: 1, Close: true, // prevent the connection from being re-used } + var contentType string // prepare request headers if len(step.Request.Headers) > 0 { @@ -636,12 +637,19 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro return stepResult, errors.Wrap(err, "parse headers failed") } for key, value := range headers { + // omit pseudo header names for HTTP/1, e.g. :authority, :method, :path, :scheme + if strings.HasPrefix(key, ":") { + continue + } req.Header.Add(key, value) - } - } - if length := req.Header.Get("Content-Length"); length != "" { - if l, err := strconv.ParseInt(length, 10, 64); err == nil { - req.ContentLength = l + + // prepare content length + if strings.EqualFold(key, "Content-Length") && value != "" { + contentType = value + if l, err := strconv.ParseInt(value, 10, 64); err == nil { + req.ContentLength = l + } + } } } @@ -689,7 +697,6 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro var dataBytes []byte switch vv := data.(type) { case map[string]interface{}: - contentType := req.Header.Get("Content-Type") if strings.HasPrefix(contentType, "application/x-www-form-urlencoded") { // post form data formData := make(url.Values) @@ -703,7 +710,7 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro if err != nil { return stepResult, err } - if req.Header.Get("Content-Type") == "" { + if contentType == "" { req.Header.Set("Content-Type", "application/json; charset=utf-8") } } From 0f3dd1b6d6e14aba2cee037beea1e752edddaade Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 18 Feb 2022 19:39:41 +0800 Subject: [PATCH 2/3] fix: generate headers.\"Content-Type\" in har2case --- docs/CHANGELOG.md | 1 + internal/har2case/core.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 9409946e..f98a1a93 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -3,6 +3,7 @@ ## v0.6.2 (2022-02-18) - fix: omit pseudo header names for HTTP/1, e.g. :authority +- fix: generate `headers.\"Content-Type\"` in har2case ## v0.6.1 (2022-02-17) diff --git a/internal/har2case/core.go b/internal/har2case/core.go index 9683ddb4..e5820883 100644 --- a/internal/har2case/core.go +++ b/internal/har2case/core.go @@ -267,7 +267,7 @@ func (s *tStep) makeValidate(entry *Entry) error { // assert Content-Type if strings.EqualFold(header.Name, "Content-Type") { s.Validators = append(s.Validators, hrp.Validator{ - Check: "headers.Content-Type", + Check: "headers.\"Content-Type\"", Assert: "equals", Expect: header.Value, Message: "assert response header Content-Type", From c6ce47ba89172dab0e80cfa293f54dba6c8e7da1 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 18 Feb 2022 21:01:48 +0800 Subject: [PATCH 3/3] fix: unittest --- examples/postman-echo.json | 70 +++++++++++++++++----------------- examples/postman-echo.yaml | 70 +++++++++++++++++----------------- internal/har2case/core_test.go | 2 +- runner.go | 3 +- 4 files changed, 72 insertions(+), 73 deletions(-) diff --git a/examples/postman-echo.json b/examples/postman-echo.json index 9c26ec78..483852b0 100644 --- a/examples/postman-echo.json +++ b/examples/postman-echo.json @@ -33,7 +33,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -75,7 +75,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -129,7 +129,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -177,7 +177,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -231,7 +231,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -285,7 +285,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -337,7 +337,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -374,7 +374,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -420,7 +420,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -460,7 +460,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -500,7 +500,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -540,7 +540,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -589,7 +589,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "text/plain; charset=utf-8", "msg": "assert response header Content-Type" @@ -625,7 +625,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -660,7 +660,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -699,7 +699,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "text/plain; charset=utf-8", "msg": "assert response header Content-Type" @@ -733,7 +733,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -766,7 +766,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -832,7 +832,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -871,7 +871,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "text/html; charset=utf-8", "msg": "assert response header Content-Type" @@ -904,7 +904,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -949,7 +949,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -994,7 +994,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -1033,7 +1033,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "text/html; charset=utf-8", "msg": "assert response header Content-Type" @@ -1069,7 +1069,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -1112,7 +1112,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -1155,7 +1155,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -1198,7 +1198,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -1241,7 +1241,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -1284,7 +1284,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -1326,7 +1326,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -1405,7 +1405,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -1448,7 +1448,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -1492,7 +1492,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" @@ -1534,7 +1534,7 @@ "msg": "assert response status code" }, { - "check": "headers.Content-Type", + "check": "headers.\"Content-Type\"", "assert": "equals", "expect": "application/json; charset=utf-8", "msg": "assert response header Content-Type" diff --git a/examples/postman-echo.yaml b/examples/postman-echo.yaml index 78b5e9b5..ea92ed12 100644 --- a/examples/postman-echo.yaml +++ b/examples/postman-echo.yaml @@ -23,7 +23,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -53,7 +53,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -91,7 +91,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -125,7 +125,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -163,7 +163,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -201,7 +201,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -237,7 +237,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -263,7 +263,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -295,7 +295,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -323,7 +323,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -351,7 +351,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -379,7 +379,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -413,7 +413,7 @@ teststeps: assert: equals expect: 302 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: text/plain; charset=utf-8 msg: assert response header Content-Type @@ -439,7 +439,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -464,7 +464,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -492,7 +492,7 @@ teststeps: assert: equals expect: 302 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: text/plain; charset=utf-8 msg: assert response header Content-Type @@ -516,7 +516,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -539,7 +539,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -585,7 +585,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -612,7 +612,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: text/html; charset=utf-8 msg: assert response header Content-Type @@ -635,7 +635,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -666,7 +666,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -697,7 +697,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -724,7 +724,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: text/html; charset=utf-8 msg: assert response header Content-Type @@ -749,7 +749,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -779,7 +779,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -809,7 +809,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -839,7 +839,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -869,7 +869,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -899,7 +899,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -928,7 +928,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -982,7 +982,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -1012,7 +1012,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -1043,7 +1043,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type @@ -1072,7 +1072,7 @@ teststeps: assert: equals expect: 200 msg: assert response status code - - check: headers.Content-Type + - check: headers."Content-Type" assert: equals expect: application/json; charset=utf-8 msg: assert response header Content-Type diff --git a/internal/har2case/core_test.go b/internal/har2case/core_test.go index 0e17cfe2..96b0c232 100644 --- a/internal/har2case/core_test.go +++ b/internal/har2case/core_test.go @@ -101,7 +101,7 @@ func TestMakeTestCase(t *testing.T) { if !assert.Equal(t, "status_code", tCase.TestSteps[0].Validators[0].Check) { t.Fail() } - if !assert.Equal(t, "headers.Content-Type", tCase.TestSteps[0].Validators[1].Check) { + if !assert.Equal(t, "headers.\"Content-Type\"", tCase.TestSteps[0].Validators[1].Check) { t.Fail() } if !assert.Equal(t, "body.url", tCase.TestSteps[0].Validators[2].Check) { diff --git a/runner.go b/runner.go index 6e0ff4eb..ab685f15 100644 --- a/runner.go +++ b/runner.go @@ -628,7 +628,6 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro ProtoMinor: 1, Close: true, // prevent the connection from being re-used } - var contentType string // prepare request headers if len(step.Request.Headers) > 0 { @@ -645,7 +644,6 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro // prepare content length if strings.EqualFold(key, "Content-Length") && value != "" { - contentType = value if l, err := strconv.ParseInt(value, 10, 64); err == nil { req.ContentLength = l } @@ -697,6 +695,7 @@ func (r *caseRunner) runStepRequest(step *TStep) (stepResult *stepData, err erro var dataBytes []byte switch vv := data.(type) { case map[string]interface{}: + contentType := req.Header.Get("Content-Type") if strings.HasPrefix(contentType, "application/x-www-form-urlencoded") { // post form data formData := make(url.Values)