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