@@ -15,59 +15,61 @@ import (
1515 "github.com/envoyproxy/envoy/source/extensions/dynamic_modules/sdk/go/shared/mocks"
1616 "github.com/stretchr/testify/require"
1717 "go.uber.org/mock/gomock"
18+
19+ "github.com/tetratelabs/built-on-envoy/extensions/composer/pkg"
1820)
1921
2022// --- Tests for joinBody ---
2123
2224func TestJoinBody_SingleChunk (t * testing.T ) {
2325 chunk := []byte ("hello world" )
24- result := joinBody ([][] byte { chunk })
26+ result := joinBody ([]shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( chunk ) })
2527 require .Equal (t , chunk , result )
2628}
2729
2830func TestJoinBody_MultipleChunks (t * testing.T ) {
29- result := joinBody ([][] byte { []byte ("foo" ), []byte ("bar" ), []byte ("baz" )})
31+ result := joinBody ([]shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( []byte ("foo" )), pkg . UnsafeBufferFromBytes ( []byte ("bar" )), pkg . UnsafeBufferFromBytes ( []byte ("baz" ) )})
3032 require .Equal (t , []byte ("foobarbaz" ), result )
3133}
3234
3335func TestJoinBody_EmptySlice (t * testing.T ) {
34- result := joinBody ([][] byte {})
36+ result := joinBody ([]shared. UnsafeEnvoyBuffer {})
3537 require .Empty (t , result )
3638}
3739
3840func TestJoinBody_EmptyChunks (t * testing.T ) {
39- result := joinBody ([][]byte {{}, []byte ("data" ), {} })
41+ result := joinBody ([]shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( []byte {}), pkg . UnsafeBufferFromBytes ( []byte ("data" )), pkg . UnsafeBufferFromBytes ([] byte {}) })
4042 require .Equal (t , []byte ("data" ), result )
4143}
4244
4345// --- Tests for headerValue ---
4446
4547func TestHeaderValue_Found (t * testing.T ) {
46- headers := [][2 ]string {
47- {":method" , "POST" },
48- {":path" , "/api/v1" },
49- {":status" , "200" },
48+ headers := [][2 ]shared. UnsafeEnvoyBuffer {
49+ {pkg . UnsafeBufferFromBytes ([] byte ( ":method" )), pkg . UnsafeBufferFromBytes ([] byte ( "POST" )) },
50+ {pkg . UnsafeBufferFromBytes ([] byte ( ":path" )), pkg . UnsafeBufferFromBytes ([] byte ( "/api/v1" )) },
51+ {pkg . UnsafeBufferFromBytes ([] byte ( ":status" )), pkg . UnsafeBufferFromBytes ([] byte ( "200" )) },
5052 }
5153 require .Equal (t , "POST" , headerValue (headers , ":method" ))
5254 require .Equal (t , "/api/v1" , headerValue (headers , ":path" ))
5355 require .Equal (t , "200" , headerValue (headers , ":status" ))
5456}
5557
5658func TestHeaderValue_NotFound (t * testing.T ) {
57- headers := [][2 ]string {
58- {":method" , "POST" },
59+ headers := [][2 ]shared. UnsafeEnvoyBuffer {
60+ {pkg . UnsafeBufferFromBytes ([] byte ( ":method" )), pkg . UnsafeBufferFromBytes ([] byte ( "POST" )) },
5961 }
6062 require .Empty (t , headerValue (headers , ":status" ))
6163}
6264
6365func TestHeaderValue_EmptyHeaders (t * testing.T ) {
64- require .Empty (t , headerValue ([][2 ]string {}, ":method" ))
66+ require .Empty (t , headerValue ([][2 ]shared. UnsafeEnvoyBuffer {}, ":method" ))
6567}
6668
6769func TestHeaderValue_ReturnsFirstMatch (t * testing.T ) {
68- headers := [][2 ]string {
69- {"x-header" , "first" },
70- {"x-header" , "second" },
70+ headers := [][2 ]shared. UnsafeEnvoyBuffer {
71+ {pkg . UnsafeBufferFromBytes ([] byte ( "x-header" )), pkg . UnsafeBufferFromBytes ([] byte ( "first" )) },
72+ {pkg . UnsafeBufferFromBytes ([] byte ( "x-header" )), pkg . UnsafeBufferFromBytes ([] byte ( "second" )) },
7173 }
7274 require .Equal (t , "first" , headerValue (headers , "x-header" ))
7375}
@@ -123,11 +125,13 @@ func TestGetCalloutHeaders_ValidBody(t *testing.T) {
123125 APIKey : "my-secret-key" ,
124126 }
125127
126- headers , calloutBody , err := getCalloutHeaders (args )
128+ rawHeaders , calloutBody , err := getCalloutHeaders (args )
127129 require .NoError (t , err )
128- require .NotNil (t , headers )
130+ require .NotNil (t , rawHeaders )
129131 require .NotNil (t , calloutBody )
130132
133+ headers := toUnsafeEnvoyBufferHeaders (rawHeaders )
134+
131135 // Verify path contains guardrail ID and version
132136 require .Equal (t , "/guardrail/my-guardrail/version/DRAFT/apply" , headerValue (headers , ":path" ))
133137 // Verify required HTTP/2 pseudo-headers
@@ -158,7 +162,7 @@ func TestGetCalloutHeaders_MultipleUserMessages(t *testing.T) {
158162
159163 headers , calloutBody , err := getCalloutHeaders (args )
160164 require .NoError (t , err )
161- require .Equal (t , "/guardrail/g1/version/1/apply" , headerValue (headers , ":path" ))
165+ require .Equal (t , "/guardrail/g1/version/1/apply" , headerValue (toUnsafeEnvoyBufferHeaders ( headers ) , ":path" ))
162166
163167 var req ApplyGuardrailRequest
164168 require .NoError (t , json .Unmarshal (calloutBody , & req ))
@@ -382,7 +386,7 @@ func TestOnHttpCalloutDone_CalloutFailure(t *testing.T) {
382386 },
383387 }
384388
385- a .OnHttpCalloutDone (1 , shared .HttpCalloutReset , [][2 ]string {}, [][] byte {})
389+ a .OnHttpCalloutDone (1 , shared .HttpCalloutReset , [][2 ]shared. UnsafeEnvoyBuffer {}, []shared. UnsafeEnvoyBuffer {})
386390}
387391
388392func TestOnHttpCalloutDone_NonSuccessHTTPStatus (t * testing.T ) {
@@ -402,8 +406,8 @@ func TestOnHttpCalloutDone_NonSuccessHTTPStatus(t *testing.T) {
402406 }
403407
404408 a .OnHttpCalloutDone (1 , shared .HttpCalloutSuccess ,
405- [][2 ]string {{ ":status" , "503" }},
406- [][] byte { []byte (`{"error":"service unavailable"}` )},
409+ [][2 ]shared. UnsafeEnvoyBuffer {{ pkg . UnsafeBufferFromBytes ([] byte ( ":status" )), pkg . UnsafeBufferFromBytes ([] byte ( "503" )) }},
410+ []shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( []byte (`{"error":"service unavailable"}` ) )},
407411 )
408412}
409413
@@ -431,8 +435,8 @@ func TestOnHttpCalloutDone_NoInterventionLastGuardrail(t *testing.T) {
431435 }
432436
433437 a .OnHttpCalloutDone (1 , shared .HttpCalloutSuccess ,
434- [][2 ]string {{ ":status" , "200" }},
435- [][] byte { respBody },
438+ [][2 ]shared. UnsafeEnvoyBuffer {{ pkg . UnsafeBufferFromBytes ([] byte ( ":status" )), pkg . UnsafeBufferFromBytes ([] byte ( "200" )) }},
439+ []shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( respBody ) },
436440 )
437441
438442 // The original body should have been appended to the buffered body
@@ -456,8 +460,8 @@ func TestOnHttpCalloutDone_BlockedByContentPolicy(t *testing.T) {
456460 }
457461
458462 a .OnHttpCalloutDone (1 , shared .HttpCalloutSuccess ,
459- [][2 ]string {{ ":status" , "200" }},
460- [][] byte { blockedContentPolicyResponse ("g1" , "1" )},
463+ [][2 ]shared. UnsafeEnvoyBuffer {{ pkg . UnsafeBufferFromBytes ([] byte ( ":status" )), pkg . UnsafeBufferFromBytes ([] byte ( "200" )) }},
464+ []shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( blockedContentPolicyResponse ("g1" , "1" ) )},
461465 )
462466}
463467
@@ -478,8 +482,8 @@ func TestOnHttpCalloutDone_BlockedByTopicPolicy(t *testing.T) {
478482 }
479483
480484 a .OnHttpCalloutDone (1 , shared .HttpCalloutSuccess ,
481- [][2 ]string {{ ":status" , "200" }},
482- [][] byte { blockedTopicPolicyResponse ("g1" , "1" )},
485+ [][2 ]shared. UnsafeEnvoyBuffer {{ pkg . UnsafeBufferFromBytes ([] byte ( ":status" )), pkg . UnsafeBufferFromBytes ([] byte ( "200" )) }},
486+ []shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( blockedTopicPolicyResponse ("g1" , "1" ) )},
483487 )
484488}
485489
@@ -500,8 +504,8 @@ func TestOnHttpCalloutDone_BlockedByPIIPolicy(t *testing.T) {
500504 }
501505
502506 a .OnHttpCalloutDone (1 , shared .HttpCalloutSuccess ,
503- [][2 ]string {{ ":status" , "200" }},
504- [][] byte { blockedPIIResponse ("g1" , "1" )},
507+ [][2 ]shared. UnsafeEnvoyBuffer {{ pkg . UnsafeBufferFromBytes ([] byte ( ":status" )), pkg . UnsafeBufferFromBytes ([] byte ( "200" )) }},
508+ []shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( blockedPIIResponse ("g1" , "1" ) )},
505509 )
506510}
507511
@@ -530,8 +534,8 @@ func TestOnHttpCalloutDone_MaskedLastGuardrail(t *testing.T) {
530534 }
531535
532536 a .OnHttpCalloutDone (1 , shared .HttpCalloutSuccess ,
533- [][2 ]string {{ ":status" , "200" }},
534- [][] byte { respBody },
537+ [][2 ]shared. UnsafeEnvoyBuffer {{ pkg . UnsafeBufferFromBytes ([] byte ( ":status" )), pkg . UnsafeBufferFromBytes ([] byte ( "200" )) }},
538+ []shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( respBody ) },
535539 )
536540
537541 // The masked body should have been appended (not the original)
@@ -577,8 +581,8 @@ func TestOnHttpCalloutDone_NoInterventionTriggersNextGuardrail(t *testing.T) {
577581 }
578582
579583 a .OnHttpCalloutDone (1 , shared .HttpCalloutSuccess ,
580- [][2 ]string {{ ":status" , "200" }},
581- [][] byte { respBody },
584+ [][2 ]shared. UnsafeEnvoyBuffer {{ pkg . UnsafeBufferFromBytes ([] byte ( ":status" )), pkg . UnsafeBufferFromBytes ([] byte ( "200" )) }},
585+ []shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( respBody ) },
582586 )
583587}
584588
@@ -616,8 +620,8 @@ func TestOnHttpCalloutDone_MaskedTriggersNextGuardrail(t *testing.T) {
616620 }
617621
618622 a .OnHttpCalloutDone (1 , shared .HttpCalloutSuccess ,
619- [][2 ]string {{ ":status" , "200" }},
620- [][] byte { respBody },
623+ [][2 ]shared. UnsafeEnvoyBuffer {{ pkg . UnsafeBufferFromBytes ([] byte ( ":status" )), pkg . UnsafeBufferFromBytes ([] byte ( "200" )) }},
624+ []shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( respBody ) },
621625 )
622626}
623627
@@ -657,8 +661,8 @@ func TestOnHttpCalloutDone_NextGuardrailCalloutFailure(t *testing.T) {
657661 }
658662
659663 a .OnHttpCalloutDone (1 , shared .HttpCalloutSuccess ,
660- [][2 ]string {{ ":status" , "200" }},
661- [][] byte { respBody },
664+ [][2 ]shared. UnsafeEnvoyBuffer {{ pkg . UnsafeBufferFromBytes ([] byte ( ":status" )), pkg . UnsafeBufferFromBytes ([] byte ( "200" )) }},
665+ []shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( respBody ) },
662666 )
663667}
664668
@@ -689,8 +693,8 @@ func TestOnHttpCalloutDone_BodyMultipleChunks(t *testing.T) {
689693 }
690694
691695 a .OnHttpCalloutDone (1 , shared .HttpCalloutSuccess ,
692- [][2 ]string {{ ":status" , "200" }},
693- [][] byte { chunk1 , chunk2 },
696+ [][2 ]shared. UnsafeEnvoyBuffer {{ pkg . UnsafeBufferFromBytes ([] byte ( ":status" )), pkg . UnsafeBufferFromBytes ([] byte ( "200" )) }},
697+ []shared. UnsafeEnvoyBuffer { pkg . UnsafeBufferFromBytes ( chunk1 ), pkg . UnsafeBufferFromBytes ( chunk2 ) },
694698 )
695699
696700 require .Equal (t , originalBody , fakeBuffer .Body )
0 commit comments