fix: content disposition filename parsing error (#613)

Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
Rick 2025-02-08 16:42:08 +08:00 committed by GitHub
parent accdb0e4ef
commit 430d9127c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 30 deletions

View File

@ -55,7 +55,9 @@ type SimpleResponse struct {
func (s SimpleResponse) getFileName() string {
for k, v := range s.Header {
if k == "Content-Disposition" {
return strings.TrimSuffix(strings.TrimPrefix(v, `attachment; filename="`), `"`)
v = strings.ReplaceAll(v, "attachment; filename=", "attachment;filename=")
v = strings.ReplaceAll(v, `filename="`, "filename=")
return strings.TrimSuffix(strings.TrimPrefix(v, `attachment;filename=`), `"`)
}
}
return ""

View File

@ -1,5 +1,5 @@
/*
Copyright 2023-2024 API Testing Authors.
Copyright 2023-2025 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.
@ -59,5 +59,26 @@ func TestSimpleResponse(t *testing.T) {
"Content-Disposition": `attachment; filename="a.txt"`,
},
}.getFileName())
// without space
assert.Equal(t, "a.txt", SimpleResponse{
Header: map[string]string{
"Content-Disposition": `attachment;filename="a.txt"`,
},
}.getFileName())
// without quote
assert.Equal(t, "a.txt", SimpleResponse{
Header: map[string]string{
"Content-Disposition": `attachment; filename=a.txt`,
},
}.getFileName())
// without quote and space
assert.Equal(t, "a.txt", SimpleResponse{
Header: map[string]string{
"Content-Disposition": `attachment;filename=a.txt`,
},
}.getFileName())
})
}