@@ -20,6 +20,7 @@ import (
20
20
"encoding/json"
21
21
"fmt"
22
22
"io"
23
+ "math/rand"
23
24
"net/http"
24
25
"net/http/httptest"
25
26
"strings"
@@ -267,4 +268,112 @@ var _ = Describe("WorkspaceKinds Handler", func() {
267
268
Expect (rs .StatusCode ).To (Equal (http .StatusNotFound ))
268
269
})
269
270
})
271
+
272
+ Context ("with unsupported request parameters" , Ordered , func () {
273
+
274
+ var (
275
+ a App
276
+ validResourceName string
277
+ invalidResourceName string
278
+ validMaxLengthName string
279
+ invalidLengthName string
280
+ )
281
+
282
+ // generateResourceName generates a random string of the specified length and allowed chars.
283
+ generateResourceName := func (length int ) string {
284
+ const allowedChars = "abcdefghijklmnopqrstuvwxyz0123456789-"
285
+
286
+ var sb strings.Builder
287
+ for i := 0 ; i < length ; i ++ {
288
+ if i == 0 || i == length - 1 {
289
+ sb .WriteByte (allowedChars [rand .Intn (len (allowedChars )- 1 )])
290
+ } else {
291
+ sb .WriteByte (allowedChars [rand .Intn (len (allowedChars ))])
292
+ }
293
+ }
294
+ return sb .String ()
295
+ }
296
+
297
+ BeforeAll (func () {
298
+ validResourceName = "test"
299
+ invalidResourceName = validResourceName + string (rune (rand .Intn (0x10FFFF - 128 )+ 128 ))
300
+ validMaxLengthName = generateResourceName (253 )
301
+ invalidLengthName = generateResourceName (254 )
302
+
303
+ repos := repositories .NewRepositories (k8sClient )
304
+ a = App {
305
+ Config : config.EnvConfig {
306
+ Port : 4000 ,
307
+ },
308
+ repositories : repos ,
309
+ }
310
+ })
311
+
312
+ It ("should return 400 status code for a invalid workspacekind name" , func () {
313
+ By ("creating the HTTP request" )
314
+ path := strings .Replace (WorkspaceKindsByNamePath , ":" + WorkspaceNamePathParam , invalidResourceName , 1 )
315
+ req , err := http .NewRequest (http .MethodGet , path , http .NoBody )
316
+ Expect (err ).NotTo (HaveOccurred (), "Failed to create HTTP request" )
317
+
318
+ By ("executing GetWorkspaceKindHandler" )
319
+ ps := httprouter.Params {
320
+ httprouter.Param {
321
+ Key : WorkspaceNamePathParam ,
322
+ Value : invalidResourceName ,
323
+ },
324
+ }
325
+ rr := httptest .NewRecorder ()
326
+ a .GetWorkspaceKindHandler (rr , req , ps )
327
+ rs := rr .Result ()
328
+ defer rs .Body .Close ()
329
+
330
+ By ("verifying the HTTP response status code" )
331
+ Expect (rs .StatusCode ).To (Equal (http .StatusBadRequest ), "Expected HTTP status 400 Bad Request" )
332
+ })
333
+
334
+ It ("should return 400 status code for a workspace longer than 253" , func () {
335
+ By ("creating the HTTP request" )
336
+ path := strings .Replace (WorkspaceKindsByNamePath , ":" + WorkspaceNamePathParam , invalidLengthName , 1 )
337
+ req , err := http .NewRequest (http .MethodGet , path , http .NoBody )
338
+ Expect (err ).NotTo (HaveOccurred (), "Failed to create HTTP request" )
339
+
340
+ By ("executing GetWorkspaceKindHandler" )
341
+ ps := httprouter.Params {
342
+ httprouter.Param {
343
+ Key : WorkspaceNamePathParam ,
344
+ Value : invalidLengthName ,
345
+ },
346
+ }
347
+ rr := httptest .NewRecorder ()
348
+ a .GetWorkspaceKindHandler (rr , req , ps )
349
+ rs := rr .Result ()
350
+ defer rs .Body .Close ()
351
+
352
+ By ("verifying the HTTP response status code" )
353
+ Expect (rs .StatusCode ).To (Equal (http .StatusBadRequest ), "Expected HTTP status 400 Bad Request" )
354
+
355
+ })
356
+
357
+ It ("should return 200 status code for a workspace with a length of 253 characters" , func () {
358
+ By ("creating the HTTP request" )
359
+ path := strings .Replace (WorkspaceKindsByNamePath , ":" + WorkspaceNamePathParam , validMaxLengthName , 1 )
360
+ req , err := http .NewRequest (http .MethodGet , path , http .NoBody )
361
+ Expect (err ).NotTo (HaveOccurred (), "Failed to create HTTP request" )
362
+
363
+ By ("executing GetWorkspaceKindHandler" )
364
+ ps := httprouter.Params {
365
+ httprouter.Param {
366
+ Key : WorkspaceNamePathParam ,
367
+ Value : validMaxLengthName ,
368
+ },
369
+ }
370
+ rr := httptest .NewRecorder ()
371
+ a .GetWorkspaceKindHandler (rr , req , ps )
372
+ rs := rr .Result ()
373
+ defer rs .Body .Close ()
374
+
375
+ By ("verifying the HTTP response status code" )
376
+ Expect (rs .StatusCode ).To (Equal (http .StatusNotFound ), "Expected HTTP status 404 Not Found" )
377
+ })
378
+ })
270
379
})
0 commit comments