-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathresponse_test.go
205 lines (197 loc) · 4.89 KB
/
response_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) Jim Lambert
// SPDX-License-Identifier: MIT
package gldap
import (
"bufio"
"bytes"
"io"
"sync"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_newResponseWriter(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
tests := []struct {
name string
writer *bufio.Writer
wLock *sync.Mutex
logger hclog.Logger
connID int
requestID int
wantErr bool
wantErrIs error
wantErrContains string
}{
{
name: "missing-writer",
wLock: &sync.Mutex{},
logger: hclog.Default(),
connID: 1,
requestID: 1,
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing writer",
},
{
name: "missing-lock",
writer: bufio.NewWriter(&buf),
logger: hclog.Default(),
connID: 1,
requestID: 1,
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing writer lock",
},
{
name: "missing-logger",
writer: bufio.NewWriter(&buf),
wLock: &sync.Mutex{},
connID: 1,
requestID: 1,
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing logger",
},
{
name: "missing-connID",
writer: bufio.NewWriter(&buf),
wLock: &sync.Mutex{},
logger: hclog.Default(),
requestID: 1,
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing conn ID",
},
{
name: "missing-requestID",
writer: bufio.NewWriter(&buf),
wLock: &sync.Mutex{},
logger: hclog.Default(),
connID: 1,
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing request ID",
},
{
name: "valid",
writer: bufio.NewWriter(&buf),
wLock: &sync.Mutex{},
logger: hclog.Default(),
connID: 1,
requestID: 1,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
w, err := newResponseWriter(tc.writer, tc.wLock, tc.logger, tc.connID, tc.requestID)
if tc.wantErr {
require.Error(err)
require.Nil(w)
if tc.wantErrIs != nil {
assert.ErrorIs(err, tc.wantErrIs)
}
if tc.wantErrContains != "" {
assert.Contains(err.Error(), tc.wantErrContains)
}
return
}
require.NoError(err)
require.NotNil(w)
assert.Equal(w.writer, tc.writer)
assert.Equal(w.writerMu, tc.wLock)
assert.Equal(w.logger, tc.logger)
assert.Equal(w.connID, tc.connID)
assert.Equal(w.requestID, tc.requestID)
})
}
}
func TestResponseWriter_Write(t *testing.T) {
tests := []struct {
name string
logger hclog.Logger
response Response
want string
wantErr bool
wantErrIs error
wantErrContains string
}{
{
name: "missing-response",
logger: hclog.New(&hclog.LoggerOptions{
Name: "TestResponseWriter_Write-logger",
Level: hclog.Error,
}),
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing response",
},
{
name: "valid",
logger: hclog.New(&hclog.LoggerOptions{
Name: "TestResponseWriter_Write-logger",
Level: hclog.Debug,
Output: func() io.Writer { var buf bytes.Buffer; return bufio.NewWriter(&buf) }(),
}),
response: &testResponse{
baseResponse: &baseResponse{
messageID: 1,
},
data: "test",
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
var buf bytes.Buffer
w, err := newResponseWriter(
bufio.NewWriter(&buf),
&sync.Mutex{},
tc.logger,
1, 1)
require.NoError(err)
err = w.Write(tc.response)
if tc.wantErr {
require.Error(err)
if tc.wantErrIs != nil {
assert.ErrorIs(err, tc.wantErrIs)
}
if tc.wantErrContains != "" {
assert.Contains(err.Error(), tc.wantErrContains)
}
return
}
require.NoError(err)
assert.Equal(tc.response.packet().Bytes(), buf.Bytes())
})
}
}
func Test_baseResponse(t *testing.T) {
assert := assert.New(t)
b := &baseResponse{}
t.Run("SetResultCode", func(t *testing.T) {
b.SetResultCode(42)
assert.Equal(int16(42), b.code)
})
t.Run("SetDiagnosticMessage", func(t *testing.T) {
b.SetDiagnosticMessage("test")
assert.Equal("test", b.diagMessage)
})
t.Run("SetMatchedDN", func(t *testing.T) {
b.SetMatchedDN("matched")
assert.Equal("matched", b.matchedDN)
})
}
type testResponse struct {
*baseResponse
data string
}
func (r *testResponse) packet() *packet {
p := beginResponse(r.messageID)
addOptionalResponseChildren(p, WithDiagnosticMessage(r.data))
return &packet{Packet: p}
}