Skip to content

Commit 430d912

Browse files
authored
fix: content disposition filename parsing error (#613)
Co-authored-by: rick <[email protected]>
1 parent accdb0e commit 430d912

File tree

2 files changed

+54
-31
lines changed

2 files changed

+54
-31
lines changed

pkg/runner/runner.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ type SimpleResponse struct {
5555
func (s SimpleResponse) getFileName() string {
5656
for k, v := range s.Header {
5757
if k == "Content-Disposition" {
58-
return strings.TrimSuffix(strings.TrimPrefix(v, `attachment; filename="`), `"`)
58+
v = strings.ReplaceAll(v, "attachment; filename=", "attachment;filename=")
59+
v = strings.ReplaceAll(v, `filename="`, "filename=")
60+
return strings.TrimSuffix(strings.TrimPrefix(v, `attachment;filename=`), `"`)
5961
}
6062
}
6163
return ""

pkg/runner/runner_test.go

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2023-2024 API Testing Authors.
2+
Copyright 2023-2025 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.
@@ -17,47 +17,68 @@ limitations under the License.
1717
package runner
1818

1919
import (
20-
"testing"
20+
"testing"
2121

22-
atest "github.com/linuxsuren/api-testing/pkg/testing"
23-
"github.com/stretchr/testify/assert"
22+
atest "github.com/linuxsuren/api-testing/pkg/testing"
23+
"github.com/stretchr/testify/assert"
2424
)
2525

2626
func TestRunnerFactory(t *testing.T) {
27-
runner := GetTestSuiteRunner(&atest.TestSuite{})
28-
assert.IsType(t, NewSimpleTestCaseRunner(), runner)
27+
runner := GetTestSuiteRunner(&atest.TestSuite{})
28+
assert.IsType(t, NewSimpleTestCaseRunner(), runner)
2929

30-
runner = GetTestSuiteRunner(&atest.TestSuite{Spec: atest.APISpec{Kind: "grpc", RPC: &atest.RPCDesc{}}})
31-
assert.IsType(t, NewGRPCTestCaseRunner("", atest.RPCDesc{}), runner)
30+
runner = GetTestSuiteRunner(&atest.TestSuite{Spec: atest.APISpec{Kind: "grpc", RPC: &atest.RPCDesc{}}})
31+
assert.IsType(t, NewGRPCTestCaseRunner("", atest.RPCDesc{}), runner)
3232
}
3333

3434
func TestUnimplementedRunner(t *testing.T) {
35-
runner := NewDefaultUnimplementedRunner()
36-
output, err := runner.RunTestCase(&atest.TestCase{}, nil, nil)
37-
assert.Nil(t, output)
38-
assert.Error(t, err)
35+
runner := NewDefaultUnimplementedRunner()
36+
output, err := runner.RunTestCase(&atest.TestCase{}, nil, nil)
37+
assert.Nil(t, output)
38+
assert.Error(t, err)
3939

40-
runner.WithWriteLevel("debug")
41-
runner.WithTestReporter(nil)
40+
runner.WithWriteLevel("debug")
41+
runner.WithTestReporter(nil)
4242

43-
var results []*atest.TestCase
44-
results, err = runner.GetSuggestedAPIs(nil, "")
45-
assert.Nil(t, results)
46-
assert.NoError(t, err)
43+
var results []*atest.TestCase
44+
results, err = runner.GetSuggestedAPIs(nil, "")
45+
assert.Nil(t, results)
46+
assert.NoError(t, err)
4747

48-
runner.WithAPISuggestLimit(0)
48+
runner.WithAPISuggestLimit(0)
4949
}
5050

5151
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-
})
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+
// without space
64+
assert.Equal(t, "a.txt", SimpleResponse{
65+
Header: map[string]string{
66+
"Content-Disposition": `attachment;filename="a.txt"`,
67+
},
68+
}.getFileName())
69+
70+
// without quote
71+
assert.Equal(t, "a.txt", SimpleResponse{
72+
Header: map[string]string{
73+
"Content-Disposition": `attachment; filename=a.txt`,
74+
},
75+
}.getFileName())
76+
77+
// without quote and space
78+
assert.Equal(t, "a.txt", SimpleResponse{
79+
Header: map[string]string{
80+
"Content-Disposition": `attachment;filename=a.txt`,
81+
},
82+
}.getFileName())
83+
})
6384
}

0 commit comments

Comments
 (0)