Skip to content

Commit cf3d1bf

Browse files
authored
fix: Header().Has works properly for checking multiple values (#207)
1 parent 8664ec3 commit cf3d1bf

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

Diff for: tooling/check/check_has_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package check
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestCheckHas(t *testing.T) {
10+
// Create a Check instance
11+
check := Has("value1", "value2", "value3")
12+
13+
// Test data
14+
testData := []string{"value1", "value2", "value3"}
15+
16+
// Call .Check on the instance
17+
checkOutput := check.Check(testData)
18+
19+
assert.True(t, checkOutput.Success, checkOutput.Reason)
20+
}
21+
22+
func TestCheckHasFailure(t *testing.T) {
23+
// Create a Check instance
24+
check := Has("value3")
25+
26+
// Test data
27+
testData := []string{"value1", "value2"}
28+
29+
// Call .Check on the instance
30+
checkOutput := check.Check(testData)
31+
32+
assert.False(t, checkOutput.Success, checkOutput.Reason)
33+
}

Diff for: tooling/test/header_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package test
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestHeader(t *testing.T) {
11+
// Manually create a response object with headers
12+
resp := &http.Response{
13+
Header: http.Header{
14+
"Access-Control-Allow-Headers": []string{"Content-Type, Range, User-Agent, X-Requested-With"},
15+
},
16+
}
17+
18+
// Extract headers from the response
19+
headers := resp.Header["Access-Control-Allow-Headers"]
20+
21+
// Test the HeaderBuilder function
22+
hb := Header("Access-Control-Allow-Headers").Has("Content-Type", "Range", "User-Agent", "X-Requested-With")
23+
24+
checkOutput := hb.Check_.Check(headers)
25+
26+
// Check if the headers satisfy the conditions
27+
assert.True(t, checkOutput.Success, checkOutput.Reason)
28+
}
29+
30+
func TestHeaderFailure(t *testing.T) {
31+
resp := &http.Response{
32+
Header: http.Header{
33+
// missing X-Requested-With
34+
"Access-Control-Allow-Headers": []string{"Content-Type, Range, User-Agent"},
35+
},
36+
}
37+
// Extract headers from the response
38+
headers := resp.Header["Access-Control-Allow-Headers"]
39+
40+
// Test the HeaderBuilder function
41+
hb := Header("Access-Control-Allow-Headers").Has("Content-Type", "Range", "User-Agent", "X-Requested-With")
42+
43+
checkOutput := hb.Check_.Check(headers)
44+
45+
assert.False(t, checkOutput.Success, checkOutput.Reason)
46+
}

Diff for: tooling/test/sugar.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,9 @@ func (h HeaderBuilder) Equals(value string, args ...any) HeaderBuilder {
436436
}
437437

438438
func (h HeaderBuilder) Has(values ...string) HeaderBuilder {
439-
h.Check_ = check.Has(values...)
439+
for _, value := range values {
440+
h.Check_ = check.IsUniqAnd(check.Contains(value))
441+
}
440442
return h
441443
}
442444

0 commit comments

Comments
 (0)