Skip to content

Commit

Permalink
feat: support to get headers from binary request
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Nov 27, 2024
1 parent 0f02984 commit a8d1370
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
26 changes: 26 additions & 0 deletions e2e/test-suite-common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,32 @@ items:
}
}
}
- name: createTestCaseForBinary
request:
api: /suites/{{.param.suiteName}}/cases
method: POST
header:
X-Store-Name: "{{.param.store}}"
body: |
{
"suiteName": "{{.param.suiteName}}",
"data": {
"name": "binary",
"request": {
"api": "{{.param.server}}/get",
"method": "GET"
}
}
}
- name: runBinaryCase
request:
api: /suites/{{.param.suiteName}}/cases/binary/run
method: POST
header:
X-Store-Name: "{{.param.store}}"
expect:
header:
Content-Type: application/octet-stream
- name: getTestCase
request:
api: /suites/{{.param.suiteName}}/cases/{{.param.caseName}}
Expand Down
17 changes: 11 additions & 6 deletions pkg/runner/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,11 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte

respType := util.GetFirstHeaderValue(resp.Header, util.ContentType)

r.withSimpleResponseRecord(resp)
if isNonBinaryContent(respType) {
var responseBodyData []byte
var rErr error
if responseBodyData, rErr = r.withResponseRecord(resp); rErr != nil {
if responseBodyData, rErr = r.withResponseBodyRecord(resp); rErr != nil {
err = errors.Join(err, rErr)
return
}
Expand Down Expand Up @@ -300,19 +301,23 @@ func generateRandomValue(param spec.Parameter) interface{} {
}
}

func (r *simpleTestCaseRunner) withResponseRecord(resp *http.Response) (responseBodyData []byte, err error) {
responseBodyData, err = io.ReadAll(resp.Body)
func (r *simpleTestCaseRunner) withSimpleResponseRecord(resp *http.Response) {
r.simpleResponse = SimpleResponse{
StatusCode: resp.StatusCode,
Header: make(map[string]string),
Body: string(responseBodyData),
}

// add some headers for convienience
ammendHeaders(resp.Header, responseBodyData)
for key := range resp.Header {
r.simpleResponse.Header[key] = resp.Header.Get(key)
}
}

func (r *simpleTestCaseRunner) withResponseBodyRecord(resp *http.Response) (responseBodyData []byte, err error) {
responseBodyData, err = io.ReadAll(resp.Body)
r.simpleResponse.Body = string(responseBodyData)

// add some headers for convenience
ammendHeaders(resp.Header, responseBodyData)
return
}

Expand Down
1 change: 0 additions & 1 deletion tools/make/run.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ run-server: ## Run the API Testing server
run-server: build-ui run-backend
run-backend:
go run . server --local-storage 'bin/*.yaml' --console-path ${ATEST_UI}/dist \
--mock-config bin/mock.yaml \
--extension-registry ghcr.io --download-timeout 90s

.PHONY: run-console
Expand Down

0 comments on commit a8d1370

Please sign in to comment.