Skip to content

Commit

Permalink
fix: cannot get correct value when the header consist mutiple values (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen authored Oct 13, 2023
1 parent 36429f9 commit fa31bb1
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 7 deletions.
1 change: 0 additions & 1 deletion extensions/store-git/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,3 @@ require (
)

replace github.com/linuxsuren/api-testing => ../../.
replace github.com/antonmedv/expr => github.com/linuxsuren/expr v1.20.0-unknown
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,3 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
skywalking.apache.org/repo/goapi v0.0.0-20230314034821-0c5a44bb767a // indirect
)

replace github.com/antonmedv/expr v1.15.0 => github.com/linuxsuren/expr v1.20.0-unknown
5 changes: 3 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b/go.mod h1:1KcenG0jGW
github.com/alecthomas/kingpin/v2 v2.3.1/go.mod h1:oYL5vtsvEHZGHxU7DMp32Dvx+qL+ptGn6lWaot2vCNE=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/antonmedv/expr v1.15.0 h1:sBHNMx1i+b1lZfkBFGhicvSLW6RLnca3R0B7jWrk8iM=
github.com/antonmedv/expr v1.15.0/go.mod h1:0E/6TxnOlRNp81GMzX9QfDPAmHo2Phg00y4JUv1ihsE=
github.com/apache/arrow/go/v10 v10.0.1/go.mod h1:YvhnlEePVnBS4+0z3fhPfUy7W1Ikj0Ih0vcRo/gZ1M0=
github.com/apache/arrow/go/v11 v11.0.0/go.mod h1:Eg5OsL5H+e299f7u5ssuXsuHQVEGC4xei5aX110hRiI=
github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2FXSqgU=
Expand Down Expand Up @@ -667,8 +669,6 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/linuxsuren/expr v1.20.0-unknown h1:jZWEk27ejKomVw4ngSTTI+q8GApESZOYVeCEkU5PJwE=
github.com/linuxsuren/expr v1.20.0-unknown/go.mod h1:0E/6TxnOlRNp81GMzX9QfDPAmHo2Phg00y4JUv1ihsE=
github.com/linuxsuren/go-fake-runtime v0.0.0-20230426144714-1a7a0d160d3f h1:TfAzkLxq/agwMBbccTx/f/dlmFWIBLWRGCWjI4IOlK8=
github.com/linuxsuren/go-fake-runtime v0.0.0-20230426144714-1a7a0d160d3f/go.mod h1:zmh6J78hSnWZo68faMA2eKOdaEp8eFbERHi3ZB9xHCQ=
github.com/lyft/protoc-gen-star v0.6.0/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=
Expand Down Expand Up @@ -731,6 +731,7 @@ golang.org/x/oauth2 v0.8.0/go.mod h1:yr7u4HXZRm1R1kBWqr/xKNqewf0plRYoB7sla+BCIXE
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
google.golang.org/api v0.114.0/go.mod h1:ifYI2ZsFK6/uGddGfAD5BMxlnkBqCmqHSDUVi45N5Yg=
Expand Down
2 changes: 1 addition & 1 deletion pkg/runner/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
}
}

respType := resp.Header.Get(util.ContentType)
respType := util.GetFirstHeaderValue(resp.Header, util.ContentType)
if output, err = verifyResponseBodyData(testcase.Name, testcase.Expect, respType, responseBodyData); err != nil {
return
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/runner/http_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/**
MIT License
Copyright (c) 2023 API Testing Authors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package runner

import (
Expand Down
14 changes: 13 additions & 1 deletion pkg/util/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ SOFTWARE.
// Package util provides a set of common functions
package util

import "strings"
import (
"net/http"
"strings"
)

// MakeSureNotNil makes sure the parameter is not nil
func MakeSureNotNil[T any](inter T) T {
Expand Down Expand Up @@ -62,6 +65,15 @@ func EmptyThenDefault(val, defVal string) string {
return val
}

// GetFirstHeaderValue retursn the first value of the header
func GetFirstHeaderValue(header http.Header, key string) (val string) {
values := header[key]
if len(values) > 0 {
val = values[0]
}
return
}

// ContentType is the HTTP header key
const (
ContentType = "Content-Type"
Expand Down
59 changes: 59 additions & 0 deletions pkg/util/default_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
/**
MIT License
Copyright (c) 2023 API Testing Authors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package util_test

import (
"net/http"
"testing"

"github.com/linuxsuren/api-testing/pkg/util"
Expand Down Expand Up @@ -49,3 +74,37 @@ func TestEmptyThenDefault(t *testing.T) {
assert.Equal(t, 1, util.ZeroThenDefault(0, 1))
assert.Equal(t, 1, util.ZeroThenDefault(1, 2))
}

func TestGetFirstHeaderValue(t *testing.T) {
tests := []struct {
name string
header http.Header
key string
expect string
}{{
name: "empty header",
header: http.Header{},
key: "abc",
expect: "",
}, {
name: "not empty header",
header: http.Header{
"abc": []string{"def"},
},
key: "abc",
expect: "def",
}, {
name: "not empty header, has multiple values",
header: http.Header{
"abc": []string{"def", "ghi"},
},
key: "abc",
expect: "def",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := util.GetFirstHeaderValue(tt.header, tt.key)
assert.Equal(t, tt.expect, result, result)
})
}
}

0 comments on commit fa31bb1

Please sign in to comment.