@@ -21,7 +21,7 @@ import (
21
21
"github.com/ONSdigital/dp-api-router/middleware"
22
22
"github.com/ONSdigital/dp-api-router/schema"
23
23
24
- "github.com/ONSdigital/dp-kafka/v2"
24
+ kafka "github.com/ONSdigital/dp-kafka/v2"
25
25
"github.com/ONSdigital/dp-kafka/v2/kafkatest"
26
26
dphttp "github.com/ONSdigital/dp-net/http"
27
27
dprequest "github.com/ONSdigital/dp-net/request"
43
43
testTimeMillisOutbound int64 = 1587884752456
44
44
testBody = []byte {1 , 2 , 3 , 4 }
45
45
errMarshal = errors .New ("avro marshal error" )
46
- errDo = errors .New ("identity client Do failed" )
47
46
)
48
47
49
48
// valid identity response for testing
@@ -52,10 +51,11 @@ var testIdentityResponse = &dprequest.IdentityResponse{
52
51
}
53
52
54
53
// utility function to generate handlers for testing, which return the provided status code and body
55
- func testHandler (statusCode int , body []byte ) http.Handler {
54
+ func testHandler (statusCode int , body []byte , c C ) http.Handler {
56
55
return http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
57
56
w .WriteHeader (statusCode )
58
- w .Write (body )
57
+ _ , err := w .Write (body )
58
+ c .So (err , ShouldBeNil )
59
59
})
60
60
}
61
61
@@ -89,7 +89,6 @@ func createHTTPClientMock(retCode int, retBody interface{}) *dphttp.ClienterMock
89
89
return []string {}
90
90
},
91
91
SetPathsWithNoRetriesFunc : func ([]string ) {
92
- return
93
92
},
94
93
DoFunc : func (ctx context.Context , req * http.Request ) (* http.Response , error ) {
95
94
body , _ := json .Marshal (retBody )
@@ -215,7 +214,7 @@ func TestAuditHandlerHeaders(t *testing.T) {
215
214
216
215
Convey ("And a valid audit handler with successful downstream" , func (c C ) {
217
216
p , a := createValidAuditHandler ()
218
- auditHandler := a (testHandler (http .StatusOK , testBody ))
217
+ auditHandler := a (testHandler (http .StatusOK , testBody , c ))
219
218
220
219
// execute request and wait for 2 audit events
221
220
auditEvents := serveAndCaptureAudit (c , w , req , auditHandler , p .Channels ().Output , 2 )
@@ -248,7 +247,7 @@ func TestAuditHandlerHeaders(t *testing.T) {
248
247
249
248
Convey ("And a failing audit handler with successful downstream" , func (c C ) {
250
249
p , a := createFailingAuditHandler ()
251
- auditHandler := a (testHandler (http .StatusOK , testBody ))
250
+ auditHandler := a (testHandler (http .StatusOK , testBody , c ))
252
251
253
252
// execute request and don't expect audit events
254
253
serveAndCaptureAudit (c , w , req , auditHandler , p .Channels ().Output , 0 )
@@ -270,7 +269,7 @@ func TestAuditHandlerHeaders(t *testing.T) {
270
269
271
270
Convey ("And a valid audit handler with successful downstream" , func (c C ) {
272
271
p , a := createValidAuditHandler ()
273
- auditHandler := a (testHandler (http .StatusOK , testBody ))
272
+ auditHandler := a (testHandler (http .StatusOK , testBody , c ))
274
273
275
274
// execute request and wait for 2 audit events
276
275
auditEvents := serveAndCaptureAudit (c , w , req , auditHandler , p .Channels ().Output , 2 )
@@ -303,7 +302,7 @@ func TestAuditHandlerHeaders(t *testing.T) {
303
302
304
303
Convey ("And a failing audit handler with successful downstream" , func (c C ) {
305
304
p , a := createFailingAuditHandler ()
306
- auditHandler := a (testHandler (http .StatusOK , testBody ))
305
+ auditHandler := a (testHandler (http .StatusOK , testBody , c ))
307
306
308
307
// execute request and don't expect audit events
309
308
serveAndCaptureAudit (c , w , req , auditHandler , p .Channels ().Output , 0 )
@@ -328,7 +327,7 @@ func TestAuditHandlerHeaders(t *testing.T) {
328
327
329
328
Convey ("And a valid audit handler with successful downstream" , func (c C ) {
330
329
p , a := createValidAuditHandler ()
331
- auditHandler := a (testHandler (http .StatusOK , testBody ))
330
+ auditHandler := a (testHandler (http .StatusOK , testBody , c ))
332
331
333
332
// execute request and wait for 2 audit events
334
333
auditEvents := serveAndCaptureAudit (c , w , req , auditHandler , p .Channels ().Output , 2 )
@@ -387,7 +386,7 @@ func TestAuditHandler(t *testing.T) {
387
386
388
387
Convey ("And a valid audit handler with unsuccessful (Forbidden) downstream" , func (c C ) {
389
388
p , a := createValidAuditHandler ()
390
- auditHandler := a (testHandler (http .StatusForbidden , testBody ))
389
+ auditHandler := a (testHandler (http .StatusForbidden , testBody , c ))
391
390
392
391
// execute request and wait for 2 audit events
393
392
auditEvents := serveAndCaptureAudit (c , w , req , auditHandler , p .Channels ().Output , 2 )
@@ -420,7 +419,7 @@ func TestAuditHandler(t *testing.T) {
420
419
421
420
Convey ("And a failing audit handler with unsuccessful (Forbidden) downstream" , func (c C ) {
422
421
p , a := createFailingAuditHandler ()
423
- auditHandler := a (testHandler (http .StatusForbidden , testBody ))
422
+ auditHandler := a (testHandler (http .StatusForbidden , testBody , c ))
424
423
425
424
// execute request and don't expect audit events
426
425
serveAndCaptureAudit (c , w , req , auditHandler , p .Channels ().Output , 0 )
@@ -455,7 +454,7 @@ func TestAuditHandler(t *testing.T) {
455
454
p := kafkatest .NewMessageProducer (true )
456
455
a := event .NewAvroProducer (p .Channels ().Output , failingMarshaller )
457
456
enableZebedeeAudit := true
458
- auditHandler := middleware .AuditHandler (a , cliMock , testZebedeeURL , testVersionPrefix , enableZebedeeAudit , nil )(testHandler (http .StatusForbidden , testBody ))
457
+ auditHandler := middleware .AuditHandler (a , cliMock , testZebedeeURL , testVersionPrefix , enableZebedeeAudit , nil )(testHandler (http .StatusForbidden , testBody , c ))
459
458
460
459
// execute request and expect only 1 audit event
461
460
auditEvents := serveAndCaptureAudit (c , w , req , auditHandler , p .Channels ().Output , 1 )
@@ -484,7 +483,7 @@ func TestAuditIgnoreSkip(t *testing.T) {
484
483
Convey ("And a valid audit handler without downstream" , func (c C ) {
485
484
486
485
p , a := createValidAuditHandler ()
487
- auditHandler := a (testHandler (http .StatusForbidden , testBody ))
486
+ auditHandler := a (testHandler (http .StatusForbidden , testBody , c ))
488
487
489
488
// execute request and don't wait for audit events
490
489
serveAndCaptureAudit (c , w , req , auditHandler , p .Channels ().Output , 0 )
@@ -506,7 +505,7 @@ func TestAuditIgnoreSkip(t *testing.T) {
506
505
Convey ("And a valid audit handler without downstream" , func (c C ) {
507
506
508
507
p , a := createValidAuditHandler ()
509
- auditHandler := a (testHandler (http .StatusForbidden , testBody ))
508
+ auditHandler := a (testHandler (http .StatusForbidden , testBody , c ))
510
509
511
510
// execute request and wait for 2 audit events
512
511
auditEvents := serveAndCaptureAudit (c , w , req , auditHandler , p .Channels ().Output , 2 )
@@ -542,7 +541,7 @@ func TestSkipZebedeeAudit(t *testing.T) {
542
541
},
543
542
}
544
543
auditMiddleware := middleware .AuditHandler (auditProducer , cliMock , testZebedeeURL , testVersionPrefix , enableZebedeeAudit , routerMock )
545
- auditHandler := auditMiddleware (testHandler (http .StatusOK , testBody ))
544
+ auditHandler := auditMiddleware (testHandler (http .StatusOK , testBody , c ))
546
545
547
546
Convey ("When the handler receives a Zebedee request" , func (c C ) {
548
547
req , err := http .NewRequest (http .MethodGet , "/data" , nil )
@@ -581,7 +580,7 @@ func TestSkipZebedeeAudit(t *testing.T) {
581
580
},
582
581
}
583
582
auditMiddleware := middleware .AuditHandler (auditProducer , cliMock , testZebedeeURL , testVersionPrefix , enableZebedeeAudit , routerMock )
584
- auditHandler := auditMiddleware (testHandler (http .StatusOK , testBody ))
583
+ auditHandler := auditMiddleware (testHandler (http .StatusOK , testBody , c ))
585
584
586
585
Convey ("When the handler receives a request for a known route (not zebedee)" , func (c C ) {
587
586
req , err := http .NewRequest (http .MethodGet , "/v1/datasets?q1=v1&q2=v2" , nil )
0 commit comments