8
8
"bytes"
9
9
"io"
10
10
"log"
11
+ "net/http"
12
+ "net/textproto"
11
13
"net/url"
12
14
"testing"
13
15
@@ -23,10 +25,12 @@ func TestReqToLog(t *testing.T) {
23
25
defaultConfig := createDefaultConfig ().(* Config )
24
26
25
27
tests := []struct {
26
- desc string
27
- sc * bufio.Scanner
28
- query url.Values
29
- tt func (t * testing.T , reqLog plog.Logs , reqLen int , settings receiver.Settings )
28
+ desc string
29
+ sc * bufio.Scanner
30
+ headers http.Header
31
+ query url.Values
32
+ config * Config
33
+ tt func (t * testing.T , reqLog plog.Logs , reqLen int , settings receiver.Settings )
30
34
}{
31
35
{
32
36
desc : "Valid query valid event" ,
@@ -112,11 +116,103 @@ func TestReqToLog(t *testing.T) {
112
116
require .Equal (t , 2 , scopeLogsScope .Attributes ().Len ())
113
117
},
114
118
},
119
+ {
120
+ desc : "Headers not added by default" ,
121
+ headers : http.Header {
122
+ textproto .CanonicalMIMEHeaderKey ("X-Foo" ): []string {"1" },
123
+ textproto .CanonicalMIMEHeaderKey ("X-Bar" ): []string {"2" },
124
+ },
125
+ sc : func () * bufio.Scanner {
126
+ reader := io .NopCloser (bytes .NewReader ([]byte ("this is a: log" )))
127
+ return bufio .NewScanner (reader )
128
+ }(),
129
+ tt : func (t * testing.T , reqLog plog.Logs , reqLen int , _ receiver.Settings ) {
130
+ require .Equal (t , 1 , reqLen )
131
+
132
+ attributes := reqLog .ResourceLogs ().At (0 ).Resource ().Attributes ()
133
+ require .Equal (t , 0 , attributes .Len ())
134
+
135
+ scopeLogsScope := reqLog .ResourceLogs ().At (0 ).ScopeLogs ().At (0 ).Scope ()
136
+ require .Equal (t , 2 , scopeLogsScope .Attributes ().Len ()) // expect no additional attributes even though headers are set
137
+ },
138
+ },
139
+ {
140
+ desc : "Headers added if ConvertHeadersToAttributes enabled" ,
141
+ headers : http.Header {
142
+ textproto .CanonicalMIMEHeaderKey ("X-Foo" ): []string {"1" },
143
+ textproto .CanonicalMIMEHeaderKey ("X-Bar" ): []string {"2" },
144
+ },
145
+ config : & Config {
146
+ Path : defaultPath ,
147
+ HealthPath : defaultHealthPath ,
148
+ ReadTimeout : defaultReadTimeout ,
149
+ WriteTimeout : defaultWriteTimeout ,
150
+ ConvertHeadersToAttributes : true ,
151
+ },
152
+ sc : func () * bufio.Scanner {
153
+ reader := io .NopCloser (bytes .NewReader ([]byte ("this is a: log" )))
154
+ return bufio .NewScanner (reader )
155
+ }(),
156
+ tt : func (t * testing.T , reqLog plog.Logs , reqLen int , _ receiver.Settings ) {
157
+ require .Equal (t , 1 , reqLen )
158
+
159
+ attributes := reqLog .ResourceLogs ().At (0 ).Resource ().Attributes ()
160
+ require .Equal (t , 0 , attributes .Len ())
161
+
162
+ scopeLogsScope := reqLog .ResourceLogs ().At (0 ).ScopeLogs ().At (0 ).Scope ()
163
+ require .Equal (t , 4 , scopeLogsScope .Attributes ().Len ()) // expect no additional attributes even though headers are set
164
+ v , exists := scopeLogsScope .Attributes ().Get ("header.x_foo" )
165
+ require .True (t , exists )
166
+ require .Equal (t , "1" , v .AsString ())
167
+ v , exists = scopeLogsScope .Attributes ().Get ("header.x_bar" )
168
+ require .True (t , exists )
169
+ require .Equal (t , "2" , v .AsString ())
170
+ },
171
+ },
172
+ {
173
+ desc : "Required header skipped" ,
174
+ headers : http.Header {
175
+ textproto .CanonicalMIMEHeaderKey ("X-Foo" ): []string {"1" },
176
+ textproto .CanonicalMIMEHeaderKey ("X-Bar" ): []string {"2" },
177
+ textproto .CanonicalMIMEHeaderKey ("X-Required-Header" ): []string {"password" },
178
+ },
179
+ config : & Config {
180
+ Path : defaultPath ,
181
+ HealthPath : defaultHealthPath ,
182
+ ReadTimeout : defaultReadTimeout ,
183
+ WriteTimeout : defaultWriteTimeout ,
184
+ RequiredHeader : RequiredHeader {Key : "X-Required-Header" , Value : "password" },
185
+ ConvertHeadersToAttributes : true ,
186
+ },
187
+ sc : func () * bufio.Scanner {
188
+ reader := io .NopCloser (bytes .NewReader ([]byte ("this is a: log" )))
189
+ return bufio .NewScanner (reader )
190
+ }(),
191
+ tt : func (t * testing.T , reqLog plog.Logs , reqLen int , _ receiver.Settings ) {
192
+ require .Equal (t , 1 , reqLen )
193
+
194
+ attributes := reqLog .ResourceLogs ().At (0 ).Resource ().Attributes ()
195
+ require .Equal (t , 0 , attributes .Len ())
196
+
197
+ scopeLogsScope := reqLog .ResourceLogs ().At (0 ).ScopeLogs ().At (0 ).Scope ()
198
+ require .Equal (t , 4 , scopeLogsScope .Attributes ().Len ()) // expect no additional attributes even though headers are set
199
+ _ , exists := scopeLogsScope .Attributes ().Get ("header.x_foo" )
200
+ require .True (t , exists )
201
+ _ , exists = scopeLogsScope .Attributes ().Get ("header.x_bar" )
202
+ require .True (t , exists )
203
+ _ , exists = scopeLogsScope .Attributes ().Get ("header.x_required_header" )
204
+ require .False (t , exists )
205
+ },
206
+ },
115
207
}
116
208
117
209
for _ , test := range tests {
118
210
t .Run (test .desc , func (t * testing.T ) {
119
- reqLog , reqLen := reqToLog (test .sc , test .query , defaultConfig , receivertest .NewNopSettings (metadata .Type ))
211
+ testConfig := defaultConfig
212
+ if test .config != nil {
213
+ testConfig = test .config
214
+ }
215
+ reqLog , reqLen := reqToLog (test .sc , test .headers , test .query , testConfig , receivertest .NewNopSettings (metadata .Type ))
120
216
test .tt (t , reqLog , reqLen , receivertest .NewNopSettings (metadata .Type ))
121
217
})
122
218
}
0 commit comments