fix: content disposition filename parsing error (#613)
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
parent
accdb0e4ef
commit
430d9127c6
|
@ -55,7 +55,9 @@ type SimpleResponse struct {
|
||||||
func (s SimpleResponse) getFileName() string {
|
func (s SimpleResponse) getFileName() string {
|
||||||
for k, v := range s.Header {
|
for k, v := range s.Header {
|
||||||
if k == "Content-Disposition" {
|
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 ""
|
return ""
|
||||||
|
|
|
@ -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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
@ -17,47 +17,68 @@ limitations under the License.
|
||||||
package runner
|
package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
atest "github.com/linuxsuren/api-testing/pkg/testing"
|
atest "github.com/linuxsuren/api-testing/pkg/testing"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRunnerFactory(t *testing.T) {
|
func TestRunnerFactory(t *testing.T) {
|
||||||
runner := GetTestSuiteRunner(&atest.TestSuite{})
|
runner := GetTestSuiteRunner(&atest.TestSuite{})
|
||||||
assert.IsType(t, NewSimpleTestCaseRunner(), runner)
|
assert.IsType(t, NewSimpleTestCaseRunner(), runner)
|
||||||
|
|
||||||
runner = GetTestSuiteRunner(&atest.TestSuite{Spec: atest.APISpec{Kind: "grpc", RPC: &atest.RPCDesc{}}})
|
runner = GetTestSuiteRunner(&atest.TestSuite{Spec: atest.APISpec{Kind: "grpc", RPC: &atest.RPCDesc{}}})
|
||||||
assert.IsType(t, NewGRPCTestCaseRunner("", atest.RPCDesc{}), runner)
|
assert.IsType(t, NewGRPCTestCaseRunner("", atest.RPCDesc{}), runner)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnimplementedRunner(t *testing.T) {
|
func TestUnimplementedRunner(t *testing.T) {
|
||||||
runner := NewDefaultUnimplementedRunner()
|
runner := NewDefaultUnimplementedRunner()
|
||||||
output, err := runner.RunTestCase(&atest.TestCase{}, nil, nil)
|
output, err := runner.RunTestCase(&atest.TestCase{}, nil, nil)
|
||||||
assert.Nil(t, output)
|
assert.Nil(t, output)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
runner.WithWriteLevel("debug")
|
runner.WithWriteLevel("debug")
|
||||||
runner.WithTestReporter(nil)
|
runner.WithTestReporter(nil)
|
||||||
|
|
||||||
var results []*atest.TestCase
|
var results []*atest.TestCase
|
||||||
results, err = runner.GetSuggestedAPIs(nil, "")
|
results, err = runner.GetSuggestedAPIs(nil, "")
|
||||||
assert.Nil(t, results)
|
assert.Nil(t, results)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
runner.WithAPISuggestLimit(0)
|
runner.WithAPISuggestLimit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSimpleResponse(t *testing.T) {
|
func TestSimpleResponse(t *testing.T) {
|
||||||
t.Run("get fileName", func(t *testing.T) {
|
t.Run("get fileName", func(t *testing.T) {
|
||||||
// without filename
|
// without filename
|
||||||
assert.Empty(t, SimpleResponse{}.getFileName())
|
assert.Empty(t, SimpleResponse{}.getFileName())
|
||||||
|
|
||||||
// normal case
|
// normal case
|
||||||
assert.Equal(t, "a.txt", SimpleResponse{
|
assert.Equal(t, "a.txt", SimpleResponse{
|
||||||
Header: map[string]string{
|
Header: map[string]string{
|
||||||
"Content-Disposition": `attachment; filename="a.txt"`,
|
"Content-Disposition": `attachment; filename="a.txt"`,
|
||||||
},
|
},
|
||||||
}.getFileName())
|
}.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())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue