Skip to content

Commit a691229

Browse files
committed
improved coverage
1 parent 871cf8e commit a691229

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

http/response_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package http
22

33
import (
4+
"io"
45
"testing"
56

67
"github.com/indigo-web/indigo/kv"
@@ -18,3 +19,21 @@ func TestResponse(t *testing.T) {
1819
require.Equal(t, "application/json", contentType)
1920
})
2021
}
22+
23+
func TestSliceReader(t *testing.T) {
24+
var message []byte
25+
r := &sliceReader{data: []byte("Hello, world!")}
26+
buff := make([]byte, 5)
27+
28+
for {
29+
n, err := r.Read(buff)
30+
message = append(message, buff[:n]...)
31+
switch err {
32+
case nil:
33+
case io.EOF:
34+
return
35+
default:
36+
require.Fail(t, err.Error())
37+
}
38+
}
39+
}

http/status/codes_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ func Benchmark(b *testing.B) {
2525
_ = StringCode(code)
2626
}
2727
}
28+
29+
func TestString(t *testing.T) {
30+
require.Equal(t, "Nonstandard", String(1))
31+
require.Equal(t, "OK", String(200))
32+
require.Equal(t, "Nonstandard", String(maxCodeValue))
33+
}

internal/buffer/buffer_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ func TestBuffer(t *testing.T) {
9494
testTrunc(t, 1)
9595
testTrunc(t, 5)
9696
})
97+
98+
t.Run("by single byte", func(t *testing.T) {
99+
buff := New(5, 5)
100+
for range 5 {
101+
require.True(t, buff.AppendByte('a'))
102+
}
103+
104+
require.False(t, buff.AppendByte('a'))
105+
require.Equal(t, "aaaaa", string(buff.Finish()))
106+
})
97107
}
98108

99109
func testDiscard(t *testing.T, n int) {

internal/codecutil/cache_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package codecutil
2+
3+
import (
4+
"testing"
5+
6+
"github.com/indigo-web/indigo/http/codec"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
type mockCodec struct {
11+
Instantiated bool
12+
}
13+
14+
func (m *mockCodec) Token() string {
15+
return "mock"
16+
}
17+
18+
func (m *mockCodec) New() codec.Instance {
19+
m.Instantiated = true
20+
return nil
21+
}
22+
23+
func TestLazyInstantiation(t *testing.T) {
24+
mock := new(mockCodec)
25+
cache := NewCache([]codec.Codec{mock}, "")
26+
require.False(t, mock.Instantiated)
27+
_ = cache.Get("rand")
28+
require.False(t, mock.Instantiated)
29+
_ = cache.Get("mock")
30+
require.True(t, mock.Instantiated)
31+
}
32+
33+
func TestAcceptEncoding(t *testing.T) {
34+
t.Run("0", func(t *testing.T) {
35+
require.Equal(t, "identity", AcceptEncoding([]codec.Codec{}))
36+
})
37+
38+
t.Run("1", func(t *testing.T) {
39+
require.Equal(t, "gzip", AcceptEncoding([]codec.Codec{codec.NewGZIP()}))
40+
})
41+
42+
t.Run("2", func(t *testing.T) {
43+
require.Equal(t, "gzip, zstd", AcceptEncoding([]codec.Codec{codec.NewGZIP(), codec.NewZSTD()}))
44+
})
45+
}

router/inbuilt/static_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package inbuilt
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestSafePath(t *testing.T) {
10+
for _, tc := range []string{
11+
"/",
12+
"/./",
13+
"/./.",
14+
"././.",
15+
} {
16+
require.True(t, isSafe(tc))
17+
}
18+
19+
for _, tc := range []string{
20+
"/..",
21+
"../",
22+
"/../",
23+
} {
24+
require.False(t, isSafe(tc))
25+
}
26+
}

0 commit comments

Comments
 (0)