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