Skip to content

Commit

Permalink
test: Increase pkg/common/utils coverage to more than 93% (cloudwego#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardo3202 committed Jul 24, 2024
1 parent 72cfd01 commit 12057c4
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion pkg/common/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,34 @@ import (

// test assert func
func TestUtilsAssert(t *testing.T) {
// nothing to test
assertPanic := func() (panicked bool) {
defer func() {
if r := recover(); r != nil {
panicked = true
}
}()
Assert(false, "should panic")
return false
}

// Checking if the assertPanic function results in a panic as expected.
// We expect a true value because it should panic.
assert.DeepEqual(t, true, assertPanic())

// Checking if a true assertion does not result in a panic.
// We create a wrapper around Assert to capture if it panics when it should not.
noPanic := func() (panicked bool) {
defer func() {
if r := recover(); r != nil {
panicked = true
}
}()
Assert(true, "should not panic")
return false
}

// We expect a false value because it should not panic.
assert.DeepEqual(t, false, noPanic())
}

func TestUtilsIsTrueString(t *testing.T) {
Expand Down Expand Up @@ -142,3 +169,18 @@ func TestFilterContentType(t *testing.T) {
contentType = FilterContentType(contentType)
assert.DeepEqual(t, "text/plain", contentType)
}

func TestNormalizeHeaderKeyEdgeCases(t *testing.T) {
empty := []byte("")
NormalizeHeaderKey(empty, false)
assert.DeepEqual(t, []byte(""), empty)
NormalizeHeaderKey(empty, true)
assert.DeepEqual(t, []byte(""), empty)
}

func TestFilterContentTypeEdgeCases(t *testing.T) {
simpleContentType := "text/plain"
assert.DeepEqual(t, "text/plain", FilterContentType(simpleContentType))
complexContentType := "text/html; charset=utf-8; format=flowed"
assert.DeepEqual(t, "text/html", FilterContentType(complexContentType))
}

0 comments on commit 12057c4

Please sign in to comment.