Skip to content

Commit

Permalink
fix: content disposition filename parsing error
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Feb 8, 2025
1 parent 664451e commit 43d44c1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
Binary file modified console/atest-desktop/api-testing.icns
Binary file not shown.
4 changes: 3 additions & 1 deletion pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
81 changes: 51 additions & 30 deletions pkg/runner/runner_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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())

// normal case
assert.Equal(t, "a.txt", SimpleResponse{
Header: map[string]string{
"Content-Disposition": `attachment; filename="a.txt"`,
},
}.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())

// 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())
})
}

0 comments on commit 43d44c1

Please sign in to comment.