Skip to content

fix: the filename is missing when downloading #566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions console/atest-ui/src/views/TestCase.vue
Original file line number Diff line number Diff line change
Expand Up @@ -375,19 +375,34 @@ function determineBodyType(e: TestCase) {
});
}

function base64ToBinary(base64: string): Uint8Array {
const binaryString = atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}

const isResponseFile = ref(false)
function downloadResponseFile(){
API.DownloadResponseFile({
body: testResult.value.body
}, (e) => {
if (e && e.data) {
try {
const bytes = atob(e.data);
const bytes = base64ToBinary(e.data);
const blob = new Blob([bytes], { type: 'mimeType' });
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = e.filename.substring("isFilePath-".length);
if (e.filename.indexOf('isFilePath-') === -1) {
link.download = e.filename;
} else {
link.download = e.filename.substring("isFilePath-".length);
}

console.log(e.filename);
document.body.appendChild(link);
link.click();

Expand Down
7 changes: 5 additions & 2 deletions pkg/runner/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,18 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
func HandleLargeResponseBody(resp SimpleResponse, suite string, caseName string) (SimpleResponse, error) {
const maxSize = 5120
prefix := "isFilePath-" + strings.Join([]string{suite, caseName}, "-")
if len(resp.Body) > 0 {
if len(resp.RawBody) == 0 && len(resp.Body) > 0 {
resp.RawBody = []byte(resp.Body)
}

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

fmt.Println("response body is too large, will be saved to file", "size", len(resp.RawBody), "path", tmpFile.Name())
if _, err = tmpFile.Write(resp.RawBody); err != nil {
return resp, fmt.Errorf("failed to write response body to file: %w", err)
}
Expand All @@ -265,6 +265,9 @@ func HandleLargeResponseBody(resp SimpleResponse, suite string, caseName string)
return resp, fmt.Errorf("failed to get absolute file path: %w", err)
}
resp.Body = filepath.Base(absFilePath)

// save the original filename into a new file
_ = os.WriteFile(absFilePath+"name", []byte(resp.getFileName()), 0644)
return resp, nil
}
return resp, nil
Expand Down
12 changes: 11 additions & 1 deletion pkg/runner/runner.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 API Testing Authors.
Copyright 2023-2024 API Testing Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"io"
"strings"

"github.com/linuxsuren/api-testing/pkg/testing"
fakeruntime "github.com/linuxsuren/go-fake-runtime"
Expand Down Expand Up @@ -51,6 +52,15 @@ type SimpleResponse struct {
StatusCode int
}

func (s SimpleResponse) getFileName() string {
for k, v := range s.Header {
if k == "Content-Disposition" {
return strings.TrimSuffix(strings.TrimPrefix(v, `attachment; filename="`), `"`)
}
}
return ""
}

// NewDefaultUnimplementedRunner initializes an unimplementedRunner using the default values.
func NewDefaultUnimplementedRunner() UnimplementedRunner {
return UnimplementedRunner{
Expand Down
14 changes: 14 additions & 0 deletions pkg/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,17 @@ func TestUnimplementedRunner(t *testing.T) {

runner.WithAPISuggestLimit(0)
}

func TestSimpleResponse(t *testing.T) {
t.Run("get fileName", func(t *testing.T) {
// without filename
assert.Empty(t, SimpleResponse{}.getFileName())

// normal case
assert.Equal(t, "a.txt", SimpleResponse{
Header: map[string]string{
"Content-Disposition": `attachment; filename="a.txt"`,
},
}.getFileName())
})
}
Loading
Loading