Skip to content

Commit 66f724a

Browse files
authored
fix: the filename is missing when downloading (#566)
Co-authored-by: rick <[email protected]>
1 parent 072cd42 commit 66f724a

File tree

5 files changed

+1143
-1093
lines changed

5 files changed

+1143
-1093
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,19 +375,34 @@ function determineBodyType(e: TestCase) {
375375
});
376376
}
377377
378+
function base64ToBinary(base64: string): Uint8Array {
379+
const binaryString = atob(base64);
380+
const len = binaryString.length;
381+
const bytes = new Uint8Array(len);
382+
for (let i = 0; i < len; i++) {
383+
bytes[i] = binaryString.charCodeAt(i);
384+
}
385+
return bytes;
386+
}
387+
378388
const isResponseFile = ref(false)
379389
function downloadResponseFile(){
380390
API.DownloadResponseFile({
381391
body: testResult.value.body
382392
}, (e) => {
383393
if (e && e.data) {
384394
try {
385-
const bytes = atob(e.data);
395+
const bytes = base64ToBinary(e.data);
386396
const blob = new Blob([bytes], { type: 'mimeType' });
387397
const link = document.createElement('a');
388398
link.href = window.URL.createObjectURL(blob);
389-
link.download = e.filename.substring("isFilePath-".length);
399+
if (e.filename.indexOf('isFilePath-') === -1) {
400+
link.download = e.filename;
401+
} else {
402+
link.download = e.filename.substring("isFilePath-".length);
403+
}
390404
405+
console.log(e.filename);
391406
document.body.appendChild(link);
392407
link.click();
393408

pkg/runner/http.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,18 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
245245
func HandleLargeResponseBody(resp SimpleResponse, suite string, caseName string) (SimpleResponse, error) {
246246
const maxSize = 5120
247247
prefix := "isFilePath-" + strings.Join([]string{suite, caseName}, "-")
248-
if len(resp.Body) > 0 {
248+
if len(resp.RawBody) == 0 && len(resp.Body) > 0 {
249249
resp.RawBody = []byte(resp.Body)
250250
}
251251

252252
if len(resp.RawBody) > maxSize {
253-
fmt.Println("response body is too large, will be saved to file", "size", len(resp.RawBody))
254253
tmpFile, err := os.CreateTemp("", prefix+"-")
255254
defer tmpFile.Close()
256255
if err != nil {
257256
return resp, fmt.Errorf("failed to create file: %w", err)
258257
}
259258

259+
fmt.Println("response body is too large, will be saved to file", "size", len(resp.RawBody), "path", tmpFile.Name())
260260
if _, err = tmpFile.Write(resp.RawBody); err != nil {
261261
return resp, fmt.Errorf("failed to write response body to file: %w", err)
262262
}
@@ -265,6 +265,9 @@ func HandleLargeResponseBody(resp SimpleResponse, suite string, caseName string)
265265
return resp, fmt.Errorf("failed to get absolute file path: %w", err)
266266
}
267267
resp.Body = filepath.Base(absFilePath)
268+
269+
// save the original filename into a new file
270+
_ = os.WriteFile(absFilePath+"name", []byte(resp.getFileName()), 0644)
268271
return resp, nil
269272
}
270273
return resp, nil

pkg/runner/runner.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2023 API Testing Authors.
2+
Copyright 2023-2024 API Testing Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23+
"strings"
2324

2425
"github.com/linuxsuren/api-testing/pkg/testing"
2526
fakeruntime "github.com/linuxsuren/go-fake-runtime"
@@ -51,6 +52,15 @@ type SimpleResponse struct {
5152
StatusCode int
5253
}
5354

55+
func (s SimpleResponse) getFileName() string {
56+
for k, v := range s.Header {
57+
if k == "Content-Disposition" {
58+
return strings.TrimSuffix(strings.TrimPrefix(v, `attachment; filename="`), `"`)
59+
}
60+
}
61+
return ""
62+
}
63+
5464
// NewDefaultUnimplementedRunner initializes an unimplementedRunner using the default values.
5565
func NewDefaultUnimplementedRunner() UnimplementedRunner {
5666
return UnimplementedRunner{

pkg/runner/runner_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,17 @@ func TestUnimplementedRunner(t *testing.T) {
4747

4848
runner.WithAPISuggestLimit(0)
4949
}
50+
51+
func TestSimpleResponse(t *testing.T) {
52+
t.Run("get fileName", func(t *testing.T) {
53+
// without filename
54+
assert.Empty(t, SimpleResponse{}.getFileName())
55+
56+
// normal case
57+
assert.Equal(t, "a.txt", SimpleResponse{
58+
Header: map[string]string{
59+
"Content-Disposition": `attachment; filename="a.txt"`,
60+
},
61+
}.getFileName())
62+
})
63+
}

0 commit comments

Comments
 (0)