@@ -37,6 +37,13 @@ import (
37
37
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
38
38
)
39
39
40
+ func assertHecSuccessResponse (t * testing.T , resp * http.Response , body any ) {
41
+ status := resp .StatusCode
42
+ assert .Equal (t , http .StatusOK , status )
43
+ assert .Equal (t , httpJSONTypeHeader , resp .Header .Get (httpContentTypeHeader ))
44
+ assert .Equal (t , map [string ]any {"code" : float64 (0 ), "text" : "Success" }, body )
45
+ }
46
+
40
47
func Test_splunkhecreceiver_NewLogsReceiver (t * testing.T ) {
41
48
defaultConfig := createDefaultConfig ().(* Config )
42
49
emptyEndpointConfig := createDefaultConfig ().(* Config )
@@ -167,14 +174,15 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
167
174
tests := []struct {
168
175
name string
169
176
req * http.Request
170
- assertResponse func (t * testing.T , status int , body any )
177
+ assertResponse func (t * testing.T , resp * http. Response , body any )
171
178
assertSink func (t * testing.T , sink * consumertest.LogsSink )
172
179
assertMetricsSink func (t * testing.T , sink * consumertest.MetricsSink )
173
180
}{
174
181
{
175
182
name : "incorrect_method" ,
176
183
req : httptest .NewRequest ("PUT" , "http://localhost/foo" , nil ),
177
- assertResponse : func (t * testing.T , status int , body any ) {
184
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
185
+ status := resp .StatusCode
178
186
assert .Equal (t , http .StatusBadRequest , status )
179
187
assert .Equal (t , "Only \" POST\" method is supported" , body )
180
188
},
@@ -188,7 +196,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
188
196
req .Header .Set ("Content-Type" , "application/not-json" )
189
197
return req
190
198
}(),
191
- assertResponse : func (t * testing.T , status int , body any ) {
199
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
200
+ status := resp .StatusCode
192
201
assert .Equal (t , http .StatusOK , status )
193
202
assert .Equal (t , map [string ]any {
194
203
"text" : "Success" ,
@@ -203,7 +212,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
203
212
req .Header .Set ("Content-Encoding" , "superzipper" )
204
213
return req
205
214
}(),
206
- assertResponse : func (t * testing.T , status int , body any ) {
215
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
216
+ status := resp .StatusCode
207
217
assert .Equal (t , http .StatusUnsupportedMediaType , status )
208
218
assert .Equal (t , `"Content-Encoding" must be "gzip" or empty` , body )
209
219
},
@@ -214,7 +224,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
214
224
req := httptest .NewRequest ("POST" , "http://localhost/foo" , bytes .NewReader ([]byte {1 , 2 , 3 , 4 }))
215
225
return req
216
226
}(),
217
- assertResponse : func (t * testing.T , status int , body any ) {
227
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
228
+ status := resp .StatusCode
218
229
assert .Equal (t , http .StatusBadRequest , status )
219
230
assert .Equal (t , map [string ]any {"code" : float64 (6 ), "text" : "Invalid data format" }, body )
220
231
},
@@ -225,7 +236,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
225
236
req := httptest .NewRequest ("POST" , "http://localhost/foo" , bytes .NewReader (nil ))
226
237
return req
227
238
}(),
228
- assertResponse : func (t * testing.T , status int , body any ) {
239
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
240
+ status := resp .StatusCode
229
241
assert .Equal (t , http .StatusBadRequest , status )
230
242
assert .Equal (t , map [string ]any {"code" : float64 (5 ), "text" : "No data" }, body )
231
243
},
@@ -238,7 +250,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
238
250
req := httptest .NewRequest ("POST" , "http://localhost/foo" , bytes .NewReader (msgBytes ))
239
251
return req
240
252
}(),
241
- assertResponse : func (t * testing.T , status int , body any ) {
253
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
254
+ status := resp .StatusCode
242
255
assert .Equal (t , http .StatusBadRequest , status )
243
256
assert .Equal (t , map [string ]any {"code" : float64 (6 ), "text" : "Invalid data format" }, body )
244
257
},
@@ -253,7 +266,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
253
266
req := httptest .NewRequest ("POST" , "http://localhost/foo" , bytes .NewReader (msgBytes ))
254
267
return req
255
268
}(),
256
- assertResponse : func (t * testing.T , status int , body any ) {
269
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
270
+ status := resp .StatusCode
257
271
assert .Equal (t , http .StatusBadRequest , status )
258
272
assert .Equal (t , map [string ]any {"code" : float64 (12 ), "text" : "Event field is required" }, body )
259
273
},
@@ -268,7 +282,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
268
282
req := httptest .NewRequest ("POST" , "http://localhost/foo" , bytes .NewReader (msgBytes ))
269
283
return req
270
284
}(),
271
- assertResponse : func (t * testing.T , status int , body any ) {
285
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
286
+ status := resp .StatusCode
272
287
assert .Equal (t , http .StatusBadRequest , status )
273
288
assert .Equal (t , map [string ]any {"code" : float64 (13 ), "text" : "Event field cannot be blank" }, body )
274
289
},
@@ -281,9 +296,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
281
296
req := httptest .NewRequest ("POST" , "http://localhost/foo" , bytes .NewReader (msgBytes ))
282
297
return req
283
298
}(),
284
- assertResponse : func (t * testing.T , status int , body any ) {
285
- assert .Equal (t , http .StatusOK , status )
286
- assert .Equal (t , map [string ]any {"code" : float64 (0 ), "text" : "Success" }, body )
299
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
300
+ assertHecSuccessResponse (t , resp , body )
287
301
},
288
302
assertSink : func (t * testing.T , sink * consumertest.LogsSink ) {
289
303
assert .Equal (t , 1 , len (sink .AllLogs ()))
@@ -300,9 +314,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
300
314
req := httptest .NewRequest ("POST" , "http://localhost/foo" , bytes .NewReader (msgBytes ))
301
315
return req
302
316
}(),
303
- assertResponse : func (t * testing.T , status int , body any ) {
304
- assert .Equal (t , http .StatusOK , status )
305
- assert .Equal (t , map [string ]any {"code" : float64 (0 ), "text" : "Success" }, body )
317
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
318
+ assertHecSuccessResponse (t , resp , body )
306
319
},
307
320
assertSink : func (t * testing.T , sink * consumertest.LogsSink ) {
308
321
assert .Equal (t , 0 , len (sink .AllLogs ()))
@@ -327,9 +340,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
327
340
req .Header .Set ("Content-Encoding" , "gzip" )
328
341
return req
329
342
}(),
330
- assertResponse : func (t * testing.T , status int , body any ) {
331
- assert .Equal (t , http .StatusOK , status )
332
- assert .Equal (t , map [string ]any {"code" : float64 (0 ), "text" : "Success" }, body )
343
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
344
+ assertHecSuccessResponse (t , resp , body )
333
345
},
334
346
},
335
347
{
@@ -342,7 +354,8 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
342
354
req .Header .Set ("Content-Encoding" , "gzip" )
343
355
return req
344
356
}(),
345
- assertResponse : func (t * testing.T , status int , body any ) {
357
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
358
+ status := resp .StatusCode
346
359
assert .Equal (t , http .StatusBadRequest , status )
347
360
assert .Equal (t , `Error on gzip body` , body )
348
361
},
@@ -373,7 +386,7 @@ func Test_splunkhecReceiver_handleReq(t *testing.T) {
373
386
fmt .Println (string (respBytes ))
374
387
assert .NoError (t , json .Unmarshal (respBytes , & body ))
375
388
376
- tt .assertResponse (t , resp . StatusCode , body )
389
+ tt .assertResponse (t , resp , body )
377
390
if tt .assertSink != nil {
378
391
tt .assertSink (t , sink )
379
392
}
@@ -928,12 +941,13 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
928
941
tests := []struct {
929
942
name string
930
943
req * http.Request
931
- assertResponse func (t * testing.T , status int , body any )
944
+ assertResponse func (t * testing.T , resp * http. Response , body any )
932
945
}{
933
946
{
934
947
name : "incorrect_method" ,
935
948
req : httptest .NewRequest ("PUT" , "http://localhost/foo" , nil ),
936
- assertResponse : func (t * testing.T , status int , body any ) {
949
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
950
+ status := resp .StatusCode
937
951
assert .Equal (t , http .StatusBadRequest , status )
938
952
assert .Equal (t , `Only "POST" method is supported` , body )
939
953
},
@@ -945,7 +959,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
945
959
req .Header .Set ("Content-Type" , "application/not-json" )
946
960
return req
947
961
}(),
948
- assertResponse : func (t * testing.T , status int , body any ) {
962
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
963
+ status := resp .StatusCode
949
964
assert .Equal (t , http .StatusOK , status )
950
965
},
951
966
},
@@ -956,7 +971,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
956
971
req .Header .Set ("Content-Encoding" , "superzipper" )
957
972
return req
958
973
}(),
959
- assertResponse : func (t * testing.T , status int , body any ) {
974
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
975
+ status := resp .StatusCode
960
976
assert .Equal (t , http .StatusUnsupportedMediaType , status )
961
977
assert .Equal (t , `"Content-Encoding" must be "gzip" or empty` , body )
962
978
},
@@ -967,7 +983,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
967
983
req := httptest .NewRequest ("POST" , "http://localhost/foo" , bytes .NewReader (nil ))
968
984
return req
969
985
}(),
970
- assertResponse : func (t * testing.T , status int , body any ) {
986
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
987
+ status := resp .StatusCode
971
988
assert .Equal (t , http .StatusBadRequest , status )
972
989
assert .Equal (t , map [string ]any {"code" : float64 (5 ), "text" : "No data" }, body )
973
990
},
@@ -979,7 +996,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
979
996
req := httptest .NewRequest ("POST" , "http://localhost/foo" , strings .NewReader ("foo\n bar" ))
980
997
return req
981
998
}(),
982
- assertResponse : func (t * testing.T , status int , body any ) {
999
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
1000
+ status := resp .StatusCode
983
1001
assert .Equal (t , http .StatusOK , status )
984
1002
},
985
1003
},
@@ -991,8 +1009,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
991
1009
req := httptest .NewRequest ("POST" , "http://localhost/foo" , bytes .NewReader (msgBytes ))
992
1010
return req
993
1011
}(),
994
- assertResponse : func (t * testing.T , status int , body any ) {
995
- assert . Equal (t , http . StatusOK , status )
1012
+ assertResponse : func (t * testing.T , resp * http. Response , body any ) {
1013
+ assertHecSuccessResponse (t , resp , body )
996
1014
},
997
1015
},
998
1016
{
@@ -1011,8 +1029,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
1011
1029
req .Header .Set ("Content-Encoding" , "gzip" )
1012
1030
return req
1013
1031
}(),
1014
- assertResponse : func (t * testing.T , status int , body any ) {
1015
- assert . Equal (t , http . StatusOK , status )
1032
+ assertResponse : func (t * testing.T , resp * http. Response , body any ) {
1033
+ assertHecSuccessResponse (t , resp , body )
1016
1034
},
1017
1035
},
1018
1036
{
@@ -1025,7 +1043,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
1025
1043
req .Header .Set ("Content-Encoding" , "gzip" )
1026
1044
return req
1027
1045
}(),
1028
- assertResponse : func (t * testing.T , status int , body any ) {
1046
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
1047
+ status := resp .StatusCode
1029
1048
assert .Equal (t , http .StatusBadRequest , status )
1030
1049
assert .Equal (t , `Error on gzip body` , body )
1031
1050
},
@@ -1044,7 +1063,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
1044
1063
1045
1064
return req
1046
1065
}(),
1047
- assertResponse : func (t * testing.T , status int , body any ) {
1066
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
1067
+ status := resp .StatusCode
1048
1068
assert .Equal (t , http .StatusBadRequest , status )
1049
1069
assert .Equal (t , map [string ]any {"code" : float64 (6 ), "text" : "Invalid data format" }, body )
1050
1070
},
@@ -1063,7 +1083,8 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
1063
1083
1064
1084
return req
1065
1085
}(),
1066
- assertResponse : func (t * testing.T , status int , body any ) {
1086
+ assertResponse : func (t * testing.T , resp * http.Response , body any ) {
1087
+ status := resp .StatusCode
1067
1088
assert .Equal (t , http .StatusBadRequest , status )
1068
1089
assert .Equal (t , map [string ]any {"code" : float64 (6 ), "text" : "Invalid data format" }, body )
1069
1090
},
@@ -1094,7 +1115,7 @@ func Test_splunkhecReceiver_handleRawReq(t *testing.T) {
1094
1115
assert .NoError (t , json .Unmarshal (respBytes , & body ))
1095
1116
}
1096
1117
1097
- tt .assertResponse (t , resp . StatusCode , body )
1118
+ tt .assertResponse (t , resp , body )
1098
1119
})
1099
1120
}
1100
1121
}
0 commit comments