7272 // Introduced in Go 1.21
7373 "Certificate required" : regexp .MustCompile (`certificate required` ),
7474 "Unknown CA" : regexp .MustCompile (`unknown certificate authority` ),
75+ "Too Many Requests" : regexp .MustCompile (`Too Many Requests` ),
7576 }
7677)
7778
@@ -98,6 +99,7 @@ type TestInputs struct {
9899 Username string
99100 Password string
100101 ClientCertificate string
102+ Requests int
101103}
102104
103105func TestYAMLFiles (t * testing.T ) {
@@ -364,6 +366,20 @@ func TestServerBehaviour(t *testing.T) {
364366 ClientCertificate : "client2_selfsigned" ,
365367 ExpectedError : ErrorMap ["Invalid client cert" ],
366368 },
369+ {
370+ Name : "valid rate limiter (no rate limiter set up) that doesn't block" ,
371+ YAMLConfigPath : "testdata/web_config_rate_limiter_nonblocking.yaml" ,
372+ UseTLSClient : false ,
373+ Requests : 10 ,
374+ ExpectedError : nil ,
375+ },
376+ {
377+ Name : "valid rate limiter with an interval of one second" ,
378+ YAMLConfigPath : "testdata/web_config_rate_limiter_one_second.yaml" ,
379+ UseTLSClient : false ,
380+ Requests : 10 ,
381+ ExpectedError : ErrorMap ["Too Many Requests" ],
382+ },
367383 }
368384 for _ , testInputs := range testTables {
369385 t .Run (testInputs .Name , testInputs .Test )
@@ -511,35 +527,41 @@ func (test *TestInputs) Test(t *testing.T) {
511527 if test .Username != "" {
512528 req .SetBasicAuth (test .Username , test .Password )
513529 }
530+
514531 return client .Do (req )
515532 }
516533 go func () {
517534 time .Sleep (250 * time .Millisecond )
518- r , err := ClientConnection ()
519- if err != nil {
520- recordConnectionError (err )
521- return
522- }
523535
524- if test .ActualCipher != 0 {
525- if r .TLS .CipherSuite != test .ActualCipher {
526- recordConnectionError (
527- fmt .Errorf ("bad cipher suite selected. Expected: %s, got: %s" ,
528- tls .CipherSuiteName (test .ActualCipher ),
529- tls .CipherSuiteName (r .TLS .CipherSuite ),
530- ),
531- )
536+ for req := 0 ; req <= test .Requests ; req ++ {
537+
538+ r , err := ClientConnection ()
539+
540+ if err != nil {
541+ recordConnectionError (err )
542+ return
532543 }
533- }
534544
535- body , err := io .ReadAll (r .Body )
536- if err != nil {
537- recordConnectionError (err )
538- return
539- }
540- if string (body ) != "Hello World!" {
541- recordConnectionError (errors .New (string (body )))
542- return
545+ if test .ActualCipher != 0 {
546+ if r .TLS .CipherSuite != test .ActualCipher {
547+ recordConnectionError (
548+ fmt .Errorf ("bad cipher suite selected. Expected: %s, got: %s" ,
549+ tls .CipherSuiteName (test .ActualCipher ),
550+ tls .CipherSuiteName (r .TLS .CipherSuite ),
551+ ),
552+ )
553+ }
554+ }
555+
556+ body , err := io .ReadAll (r .Body )
557+ if err != nil {
558+ recordConnectionError (err )
559+ return
560+ }
561+ if string (body ) != "Hello World!" {
562+ recordConnectionError (errors .New (string (body )))
563+ return
564+ }
543565 }
544566 recordConnectionError (nil )
545567 }()
0 commit comments