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.
@ -17,47 +17,68 @@ limitations under the License.
package runner
import (
"testing"
"testing"
atest "github.com/linuxsuren/api-testing/pkg/testing"
"github.com/stretchr/testify/assert"
atest "github.com/linuxsuren/api-testing/pkg/testing"
"github.com/stretchr/testify/assert"
)
func TestRunnerFactory(t *testing.T) {
runner := GetTestSuiteRunner(&atest.TestSuite{})
assert.IsType(t, NewSimpleTestCaseRunner(), runner)
runner := GetTestSuiteRunner(&atest.TestSuite{})
assert.IsType(t, NewSimpleTestCaseRunner(), runner)
runner = GetTestSuiteRunner(&atest.TestSuite{Spec: atest.APISpec{Kind: "grpc", RPC: &atest.RPCDesc{}}})
assert.IsType(t, NewGRPCTestCaseRunner("", atest.RPCDesc{}), runner)
runner = GetTestSuiteRunner(&atest.TestSuite{Spec: atest.APISpec{Kind: "grpc", RPC: &atest.RPCDesc{}}})
assert.IsType(t, NewGRPCTestCaseRunner("", atest.RPCDesc{}), runner)
}
func TestUnimplementedRunner(t *testing.T) {
runner := NewDefaultUnimplementedRunner()
output, err := runner.RunTestCase(&atest.TestCase{}, nil, nil)
assert.Nil(t, output)
assert.Error(t, err)
runner := NewDefaultUnimplementedRunner()
output, err := runner.RunTestCase(&atest.TestCase{}, nil, nil)
assert.Nil(t, output)
assert.Error(t, err)
runner.WithWriteLevel("debug")
runner.WithTestReporter(nil)
runner.WithWriteLevel("debug")
runner.WithTestReporter(nil)
var results []*atest.TestCase
results, err = runner.GetSuggestedAPIs(nil, "")
assert.Nil(t, results)
assert.NoError(t, err)
var results []*atest.TestCase
results, err = runner.GetSuggestedAPIs(nil, "")
assert.Nil(t, results)
assert.NoError(t, err)
runner.WithAPISuggestLimit(0)
runner.WithAPISuggestLimit(0)
}
func TestSimpleResponse(t *testing.T) {
t.Run("get fileName", func(t *testing.T) {
// without filename
assert.Empty(t, SimpleResponse{}.getFileName())
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())
})
// normal case
assert.Equal(t, "a.txt", SimpleResponse{
Header: map[string]string{
"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())
})
}