@@ -7,11 +7,13 @@ import (
77
88 "github.com/42atomys/webhooked/format"
99 "github.com/42atomys/webhooked/internal/config"
10+ "github.com/42atomys/webhooked/internal/fasthttpz"
1011 "github.com/42atomys/webhooked/security"
1112 securityNoop "github.com/42atomys/webhooked/security/noop"
1213 "github.com/42atomys/webhooked/storage"
1314 storageNoop "github.com/42atomys/webhooked/storage/noop"
1415 "github.com/stretchr/testify/assert"
16+ "github.com/stretchr/testify/require"
1517 "github.com/valyala/fasthttp"
1618)
1719
@@ -22,10 +24,9 @@ func TestNewExecutor(t *testing.T) {
2224}
2325
2426func TestDefaultExecutor_IncomingRequest_SpecNotFound (t * testing.T ) {
25- // Setup
2627 executor := NewExecutor (& config.Config {})
2728
28- ctx := & fasthttp.RequestCtx {}
29+ ctx := & fasthttpz. RequestCtx { RequestCtx : & fasthttp.RequestCtx {} }
2930 ctx .Request .SetRequestURI ("/nonexistent/path" )
3031
3132 // Execute
@@ -37,12 +38,9 @@ func TestDefaultExecutor_IncomingRequest_SpecNotFound(t *testing.T) {
3738}
3839
3940func TestDefaultExecutor_IncomingRequest_Success (t * testing.T ) {
40- // Setup test configuration
41- setupTestConfig (t )
41+ executor := NewExecutor (setupTestConfig (t ))
4242
43- executor := NewExecutor (& config.Config {})
44-
45- ctx := & fasthttp.RequestCtx {}
43+ ctx := & fasthttpz.RequestCtx {RequestCtx : & fasthttp.RequestCtx {}}
4644 ctx .Request .SetRequestURI ("/webhooks/v1alpha2/test" )
4745 ctx .Request .Header .SetMethod ("POST" )
4846 ctx .Request .SetBody ([]byte (`{"test": "data"}` ))
@@ -56,12 +54,9 @@ func TestDefaultExecutor_IncomingRequest_Success(t *testing.T) {
5654}
5755
5856func TestDefaultExecutor_IncomingRequest_SecurityFailure (t * testing.T ) {
59- // Setup test configuration with security that will fail
60- setupTestConfigWithFailingSecurity (t )
61-
62- executor := NewExecutor (& config.Config {})
57+ executor := NewExecutor (setupTestConfigWithFailingSecurity (t ))
6358
64- ctx := & fasthttp.RequestCtx {}
59+ ctx := & fasthttpz. RequestCtx { RequestCtx : & fasthttp.RequestCtx {} }
6560 ctx .Request .SetRequestURI ("/webhooks/v1alpha2/secure-test" )
6661 ctx .Request .Header .SetMethod ("POST" )
6762 ctx .Request .SetBody ([]byte (`{"test": "data"}` ))
@@ -74,6 +69,21 @@ func TestDefaultExecutor_IncomingRequest_SecurityFailure(t *testing.T) {
7469 assert .Equal (t , fasthttp .StatusUnauthorized , ctx .Response .StatusCode ())
7570}
7671
72+ func TestDefaultExecutor_IncomingRequest_SecurityError (t * testing.T ) {
73+ executor := NewExecutor (setupTestConfigWithFailingSecurity (t ))
74+
75+ ctx := & fasthttpz.RequestCtx {RequestCtx : & fasthttp.RequestCtx {}}
76+ ctx .Request .SetRequestURI ("/webhooks/v1alpha2/secure-test-error" )
77+ ctx .Request .Header .SetMethod ("POST" )
78+ ctx .Request .SetBody ([]byte (`{"test": "data"}` ))
79+
80+ // Execute
81+ err := executor .IncomingRequest (context .Background (), ctx )
82+
83+ // Assert
84+ assert .Error (t , err )
85+ assert .Equal (t , fasthttp .StatusInternalServerError , ctx .Response .StatusCode ())
86+ }
7787func TestDefaultExecutor_pipelineOrder (t * testing.T ) {
7888 executor := & DefaultExecutor {}
7989 pipeline := executor .pipelineOrder ()
@@ -92,7 +102,7 @@ func TestDefaultExecutor_pipelineSecure_Success(t *testing.T) {
92102 },
93103 }
94104
95- ctx := & fasthttp.RequestCtx {}
105+ ctx := & fasthttpz. RequestCtx { RequestCtx : & fasthttp.RequestCtx {} }
96106
97107 resultCtx , err := executor .pipelineSecure (context .Background (), ctx , webhook )
98108
@@ -107,7 +117,7 @@ func TestDefaultExecutor_pipelineResponse_NoTemplate(t *testing.T) {
107117 Response : config.Response {},
108118 }
109119
110- ctx := & fasthttp.RequestCtx {}
120+ ctx := & fasthttpz. RequestCtx { RequestCtx : & fasthttp.RequestCtx {} }
111121
112122 resultCtx , err := executor .pipelineResponse (context .Background (), ctx , webhook )
113123
@@ -130,7 +140,7 @@ func TestDefaultExecutor_pipelineStore_Success(t *testing.T) {
130140 },
131141 }
132142
133- ctx := & fasthttp.RequestCtx {}
143+ ctx := & fasthttpz. RequestCtx { RequestCtx : & fasthttp.RequestCtx {} }
134144 ctx .Request .SetBody ([]byte (`{"test": "data"}` ))
135145
136146 resultCtx , err := executor .pipelineStore (context .Background (), ctx , webhook )
@@ -141,22 +151,66 @@ func TestDefaultExecutor_pipelineStore_Success(t *testing.T) {
141151
142152// Helper functions for test setup
143153
144- func setupTestConfig (t * testing.T ) {
145- // Since we can't modify the global config easily in tests,
146- // we'll skip these tests that require global config manipulation
147- t .Skip ("Skipping test that requires global config modification" )
154+ func setupTestConfig (t * testing.T ) * config.Config {
155+ config := & config.Config {
156+ APIVersion : config .APIVersionV1Alpha2 ,
157+ Kind : config .KindConfiguration ,
158+ Specs : []* config.Spec {
159+ {
160+ Webhooks : []* config.Webhook {
161+ {
162+ Name : "success-test" ,
163+ EntrypointURL : "/test" ,
164+ Security : security.Security {
165+ Type : "noop" ,
166+ Specs : & securityNoop.NoopSecuritySpec {},
167+ },
168+ },
169+ },
170+ },
171+ },
172+ }
173+
174+ require .NoError (t , config .Validate ())
175+ return config
148176}
149177
150- func setupTestConfigWithFailingSecurity (t * testing.T ) {
151- // Since we can't modify the global config easily in tests,
152- // we'll skip these tests that require global config manipulation
153- t .Skip ("Skipping test that requires global config modification" )
178+ func setupTestConfigWithFailingSecurity (t * testing.T ) * config.Config {
179+ config := & config.Config {
180+ APIVersion : config .APIVersionV1Alpha2 ,
181+ Kind : config .KindConfiguration ,
182+ Specs : []* config.Spec {
183+ {
184+ Webhooks : []* config.Webhook {
185+ {
186+ Name : "secure-test" ,
187+ EntrypointURL : "/secure-test" ,
188+ Security : security.Security {
189+ Type : "failling" ,
190+ Specs : & mockFailingSecurity {},
191+ },
192+ },
193+ {
194+ Name : "secure-test-error" ,
195+ EntrypointURL : "/secure-test-error" ,
196+ Security : security.Security {
197+ Type : "error" ,
198+ Specs : & mockErrorSecurity {},
199+ },
200+ },
201+ },
202+ },
203+ },
204+ }
205+
206+ require .NoError (t , config .Validate ())
207+ return config
154208}
155209
156210// Mock security implementation that always fails
157211type mockFailingSecurity struct {}
158212
159- func (m * mockFailingSecurity ) IsSecure (ctx * fasthttp .RequestCtx ) (bool , error ) {
213+ func (m * mockFailingSecurity ) IsSecure (ctx context. Context , rctx * fasthttpz .RequestCtx ) (bool , error ) {
160214 return false , nil
161215}
162216
@@ -171,7 +225,7 @@ func (m *mockFailingSecurity) Initialize() error {
171225// Mock security implementation that returns an error
172226type mockErrorSecurity struct {}
173227
174- func (m * mockErrorSecurity ) IsSecure (ctx * fasthttp .RequestCtx ) (bool , error ) {
228+ func (m * mockErrorSecurity ) IsSecure (ctx context. Context , rctx * fasthttpz .RequestCtx ) (bool , error ) {
175229 return false , errors .New ("security check failed" )
176230}
177231
@@ -193,7 +247,7 @@ func TestDefaultExecutor_pipelineSecure_Error(t *testing.T) {
193247 },
194248 }
195249
196- ctx := & fasthttp.RequestCtx {}
250+ ctx := & fasthttpz. RequestCtx { RequestCtx : & fasthttp.RequestCtx {} }
197251
198252 resultCtx , err := executor .pipelineSecure (context .Background (), ctx , webhook )
199253
@@ -212,7 +266,7 @@ func TestDefaultExecutor_pipelineSecure_Unauthorized(t *testing.T) {
212266 },
213267 }
214268
215- ctx := & fasthttp.RequestCtx {}
269+ ctx := & fasthttpz. RequestCtx { RequestCtx : & fasthttp.RequestCtx {} }
216270
217271 resultCtx , err := executor .pipelineSecure (context .Background (), ctx , webhook )
218272
0 commit comments