-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_id_test.go
55 lines (39 loc) · 1022 Bytes
/
request_id_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package utils
import (
"crypto/rand"
"errors"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/nano-interactive/go-utils/v2/__mocks__/io"
)
func TestGetRequestId(t *testing.T) {
// Arrange
t.Parallel()
assert := require.New(t)
// Act
requestId, err := GetRequestId()
// Assert
assert.Len(requestId, MaxEncodedLength)
assert.Nil(err)
}
func TestGetRequestId_ReturnError_On_Crypto_Read_Call(t *testing.T) {
assert := require.New(t)
reader := rand.Reader
t.Cleanup(func() {
rand.Reader = reader
})
mockReader := &io.MockReader{}
rand.Reader = mockReader
mockReader.On("Read", mock.Anything).Return(0, errors.New("failed to read enough bytes"))
requestId, err := GetRequestId()
assert.EqualError(err, "failed to read enough bytes")
assert.Empty(requestId)
mockReader.AssertNumberOfCalls(t, "Read", 1)
mockReader.AssertExpectations(t)
}
func BenchmarkGetRequestId(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = GetRequestId()
}
}