Skip to content

Commit

Permalink
fix: Header().Has works properly for checking multiple values (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki authored May 20, 2024
1 parent 8664ec3 commit cf3d1bf
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
33 changes: 33 additions & 0 deletions tooling/check/check_has_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package check

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCheckHas(t *testing.T) {
// Create a Check instance
check := Has("value1", "value2", "value3")

// Test data
testData := []string{"value1", "value2", "value3"}

// Call .Check on the instance
checkOutput := check.Check(testData)

assert.True(t, checkOutput.Success, checkOutput.Reason)
}

func TestCheckHasFailure(t *testing.T) {
// Create a Check instance
check := Has("value3")

// Test data
testData := []string{"value1", "value2"}

// Call .Check on the instance
checkOutput := check.Check(testData)

assert.False(t, checkOutput.Success, checkOutput.Reason)
}
46 changes: 46 additions & 0 deletions tooling/test/header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package test

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestHeader(t *testing.T) {
// Manually create a response object with headers
resp := &http.Response{
Header: http.Header{
"Access-Control-Allow-Headers": []string{"Content-Type, Range, User-Agent, X-Requested-With"},
},
}

// Extract headers from the response
headers := resp.Header["Access-Control-Allow-Headers"]

// Test the HeaderBuilder function
hb := Header("Access-Control-Allow-Headers").Has("Content-Type", "Range", "User-Agent", "X-Requested-With")

checkOutput := hb.Check_.Check(headers)

// Check if the headers satisfy the conditions
assert.True(t, checkOutput.Success, checkOutput.Reason)
}

func TestHeaderFailure(t *testing.T) {
resp := &http.Response{
Header: http.Header{
// missing X-Requested-With
"Access-Control-Allow-Headers": []string{"Content-Type, Range, User-Agent"},
},
}
// Extract headers from the response
headers := resp.Header["Access-Control-Allow-Headers"]

// Test the HeaderBuilder function
hb := Header("Access-Control-Allow-Headers").Has("Content-Type", "Range", "User-Agent", "X-Requested-With")

checkOutput := hb.Check_.Check(headers)

assert.False(t, checkOutput.Success, checkOutput.Reason)
}
4 changes: 3 additions & 1 deletion tooling/test/sugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ func (h HeaderBuilder) Equals(value string, args ...any) HeaderBuilder {
}

func (h HeaderBuilder) Has(values ...string) HeaderBuilder {
h.Check_ = check.Has(values...)
for _, value := range values {
h.Check_ = check.IsUniqAnd(check.Contains(value))
}
return h
}

Expand Down

0 comments on commit cf3d1bf

Please sign in to comment.