Skip to content

Commit 31b5e3f

Browse files
committed
support to download bianry files on ui
1 parent 11af337 commit 31b5e3f

File tree

5 files changed

+43
-29
lines changed

5 files changed

+43
-29
lines changed

console/atest-ui/src/views/TestCase.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,10 +1335,10 @@ const renameTestCase = (name: string) => {
13351335
<Codemirror v-if="!isResponseFile" v-model="testResult.bodyText"/>
13361336
<div v-if="isResponseFile" style="padding-top: 10px;">
13371337
<el-row>
1338-
<el-col :span="8">
1338+
<el-col :span="10">
13391339
<div>Response body is too large, please download to view.</div>
13401340
</el-col>
1341-
<el-col :span="4">
1341+
<el-col :span="2">
13421342
<el-button type="primary" @click="downloadResponseFile">Download</el-button>
13431343
</el-col>
13441344
</el-row>

pkg/runner/http.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"fmt"
2323
"io"
2424
"net/http"
25+
"os"
26+
"path/filepath"
2527
"strconv"
2628
"strings"
2729
"time"
@@ -225,13 +227,49 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
225227

226228
err = errors.Join(err, jsonSchemaValidation(testcase.Expect.Schema, responseBodyData))
227229
} else {
230+
switch respType {
231+
case util.OctetStream, util.Image:
232+
var data []byte
233+
if data, err = io.ReadAll(resp.Body); err == nil {
234+
r.simpleResponse.RawBody = data
235+
r.simpleResponse, err = HandleLargeResponseBody(r.simpleResponse, testcase.Group, testcase.Name)
236+
}
237+
}
228238
r.log.Debug("skip to read the body due to it is not struct content: %q\n", respType)
229239
}
230240

231241
r.cookies = append(r.cookies, resp.Cookies()...)
232242
return
233243
}
234244

245+
func HandleLargeResponseBody(resp SimpleResponse, suite string, caseName string) (SimpleResponse, error) {
246+
const maxSize = 5120
247+
prefix := "isFilePath-" + strings.Join([]string{suite, caseName}, "-")
248+
if len(resp.Body) > 0 {
249+
resp.RawBody = []byte(resp.Body)
250+
}
251+
252+
if len(resp.RawBody) > maxSize {
253+
fmt.Println("response body is too large, will be saved to file", "size", len(resp.RawBody))
254+
tmpFile, err := os.CreateTemp("", prefix+"-")
255+
defer tmpFile.Close()
256+
if err != nil {
257+
return resp, fmt.Errorf("failed to create file: %w", err)
258+
}
259+
260+
if _, err = tmpFile.Write(resp.RawBody); err != nil {
261+
return resp, fmt.Errorf("failed to write response body to file: %w", err)
262+
}
263+
absFilePath, err := filepath.Abs(tmpFile.Name())
264+
if err != nil {
265+
return resp, fmt.Errorf("failed to get absolute file path: %w", err)
266+
}
267+
resp.Body = filepath.Base(absFilePath)
268+
return resp, nil
269+
}
270+
return resp, nil
271+
}
272+
235273
func ammendHeaders(headers http.Header, body []byte) {
236274
// add content-length if it's missing
237275
if val := headers.Get(util.ContentLength); val == "" {

pkg/runner/runner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type ResponseRecord interface {
4747
type SimpleResponse struct {
4848
Header map[string]string
4949
Body string
50+
RawBody []byte
5051
StatusCode int
5152
}
5253

pkg/server/remote_server.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *TestResult, er
262262
output, testErr := suiteRunner.RunTestCase(&testCase, dataContext, ctx)
263263
if getter, ok := suiteRunner.(runner.ResponseRecord); ok {
264264
resp := getter.GetResponseRecord()
265-
resp, err = handleLargeResponseBody(resp, suite.Name, testCase.Name)
265+
resp, err = runner.HandleLargeResponseBody(resp, suite.Name, testCase.Name)
266266
reply.TestCaseResult = append(reply.TestCaseResult, &TestCaseResult{
267267
StatusCode: int32(resp.StatusCode),
268268
Body: resp.Body,
@@ -343,32 +343,6 @@ func (s *server) BatchRun(srv Runner_BatchRunServer) (err error) {
343343
}
344344
}
345345
}
346-
return
347-
}
348-
349-
func handleLargeResponseBody(resp runner.SimpleResponse, suite string, caseName string) (reply runner.SimpleResponse, err error) {
350-
const maxSize = 5120
351-
prefix := "isFilePath-" + strings.Join([]string{suite, caseName}, "-")
352-
353-
if len(resp.Body) > maxSize {
354-
remoteServerLogger.Logger.Info("response body is too large, will be saved to file", "size", len(resp.Body))
355-
tmpFile, err := os.CreateTemp("", prefix+"-")
356-
defer tmpFile.Close()
357-
if err != nil {
358-
return resp, fmt.Errorf("failed to create file: %w", err)
359-
}
360-
361-
if _, err = tmpFile.Write([]byte(resp.Body)); err != nil {
362-
return resp, fmt.Errorf("failed to write response body to file: %w", err)
363-
}
364-
absFilePath, err := filepath.Abs(tmpFile.Name())
365-
if err != nil {
366-
return resp, fmt.Errorf("failed to get absolute file path: %w", err)
367-
}
368-
resp.Body = filepath.Base(absFilePath)
369-
return resp, nil
370-
}
371-
return resp, nil
372346
}
373347

374348
func (s *server) DownloadResponseFile(ctx context.Context, in *TestCase) (reply *FileData, err error) {

pkg/util/default.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const (
7878
YAML = "application/yaml"
7979
ZIP = "application/zip"
8080
OctetStream = "application/octet-stream"
81+
Image = "image/jpeg"
8182
Plain = "text/plain"
8283
Authorization = "Authorization"
8384
)

0 commit comments

Comments
 (0)