@@ -465,6 +465,134 @@ nopoll_bool test_02b (void) {
465
465
return nopoll_true ;
466
466
}
467
467
468
+ /* Test URL redirection with status code 3xx */
469
+ nopoll_bool test_url_redirection (void ) {
470
+
471
+ noPollCtx * ctx ;
472
+ noPollConn * conn ;
473
+ int status = 0 ;
474
+ char * message = NULL ;
475
+ char * redirect_url = NULL ;
476
+ char * port = NULL ;
477
+
478
+ /* create context */
479
+ ctx = create_ctx ();
480
+
481
+ /* call to create a connection */
482
+ printf ("Test url_redirection: creating connection localhost:6789 (errno=%d)\n" , errno );
483
+ conn = nopoll_conn_new (ctx , "localhost" , "6789" , NULL , NULL , NULL , NULL );
484
+ if (! nopoll_conn_is_ok (conn )) {
485
+ printf ("ERROR: Expected to find proper client connection status, but found error.. (conn=%p, conn->session=%d, NOPOLL_INVALID_SOCKET=%d, errno=%d, strerr=%s)..\n" ,
486
+ conn , (int ) nopoll_conn_socket (conn ), (int ) NOPOLL_INVALID_SOCKET , errno , strerror (errno ));
487
+ return nopoll_false ;
488
+ }
489
+
490
+ printf ("Test url_redirection: waiting until connection is ok (errno=%d)\n" , errno );
491
+
492
+ if (! nopoll_conn_wait_for_status_until_connection_ready (conn , 5 , & status , & message )) {
493
+ printf ("INFO: Failed to connect with server, received redirection URL is \"%s\" with status code %d.\n" ,message ,status );
494
+ }
495
+
496
+ if (status != 0 && message != NULL ) {
497
+ if (status >= 300 && status <= 399 ) {
498
+ redirect_url = strchr (message , ':' );
499
+ if (redirect_url ) {
500
+ redirect_url += 8 ;
501
+ port = strchr (redirect_url , ':' );
502
+ if (port ){
503
+ int url_index = port - redirect_url ;
504
+ * (redirect_url + (url_index )) = '\0' ;
505
+ port ++ ;
506
+ printf ("INFO: Extracted received URL is %s and port is %s.\n" ,redirect_url , port );
507
+ }
508
+ else {
509
+ printf ("ERROR: Unable to get port number.\n" );
510
+ }
511
+ }
512
+ else {
513
+ printf ("ERROR: Unable to get redirection URL.\n" );
514
+ }
515
+ }
516
+
517
+ if (!redirect_url || !port || (status < 300 || status > 399 )) {
518
+ printf ("ERROR: Received Invalid data from server, closing the connection.\n" );
519
+ return nopoll_false ;
520
+ }
521
+
522
+ printf ("INFO: Connecting to new redirection URL.\n" );
523
+ nopoll_conn_close (conn );
524
+
525
+ /* call to create a connection */
526
+
527
+ conn = nopoll_conn_new (ctx , redirect_url , port , NULL , NULL , NULL , NULL );
528
+ if (! nopoll_conn_is_ok (conn )) {
529
+ printf ("ERROR: Expected to find proper client connection status, but found error.. (conn=%p, conn->session=%d, NOPOLL_INVALID_SOCKET=%d, errno=%d, strerr=%s)..\n" ,
530
+ conn , (int ) nopoll_conn_socket (conn ), (int ) NOPOLL_INVALID_SOCKET , errno , strerror (errno ));
531
+ return nopoll_false ;
532
+ }
533
+
534
+ printf ("Test url_redirection: waiting until connection is ok (errno=%d)\n" , errno );
535
+ if (! nopoll_conn_wait_until_connection_ready (conn , 5 )) {
536
+ printf ("ERROR: failed to fully establish connection nopoll_conn_wait_until_connection_ready (conn, 5) failed..\n" );
537
+ }
538
+ }
539
+
540
+ else {
541
+ printf ("ERROR: Expected status code is 3xx along with new redirection URL, but not received\n" );
542
+ return nopoll_false ;
543
+ }
544
+
545
+ /* finish connection */
546
+ nopoll_conn_close (conn );
547
+
548
+ /* finish */
549
+ nopoll_ctx_unref (ctx );
550
+
551
+ return nopoll_true ;
552
+ }
553
+
554
+ /* Test non_101 & non_3xx status code */
555
+ nopoll_bool test_non_redirection_status (void ) {
556
+
557
+ noPollCtx * ctx ;
558
+ noPollConn * conn ;
559
+ int status = 0 ;
560
+ char * message = NULL ;
561
+
562
+ /* create context */
563
+ ctx = create_ctx ();
564
+
565
+ /* call to create a connection */
566
+ printf ("Test test_non_redirection_status: creating connection localhost:9876 (errno=%d)\n" , errno );
567
+ conn = nopoll_conn_new (ctx , "localhost" , "9876" , NULL , NULL , NULL , NULL );
568
+ if (! nopoll_conn_is_ok (conn )) {
569
+ printf ("ERROR: Expected to find proper client connection status, but found error.. (conn=%p, conn->session=%d, NOPOLL_INVALID_SOCKET=%d, errno=%d, strerr=%s)..\n" ,
570
+ conn , (int ) nopoll_conn_socket (conn ), (int ) NOPOLL_INVALID_SOCKET , errno , strerror (errno ));
571
+ return nopoll_false ;
572
+ }
573
+
574
+ printf ("Test test_non_redirection_status: waiting until connection is ok (errno=%d)\n" , errno );
575
+
576
+ if (! nopoll_conn_wait_for_status_until_connection_ready (conn , 5 , & status , & message )) {
577
+ printf ("INFO: Failed to connect with server, received reply \"%s\" with status code %d \n" ,message ,status );
578
+ }
579
+
580
+ if (status == 500 ) {
581
+ printf ("Test non_redirection_status: Received Expected status %d \n" ,status );
582
+ /* finish connection */
583
+ nopoll_conn_close (conn );
584
+
585
+ /* finish */
586
+ nopoll_ctx_unref (ctx );
587
+
588
+ return nopoll_true ;
589
+ }
590
+ else {
591
+ printf ("ERROR: Expected status code from server as 500, but received something else\n" );
592
+ return nopoll_false ;
593
+ }
594
+ }
595
+
468
596
469
597
nopoll_bool test_03 (void ) {
470
598
noPollCtx * ctx ;
@@ -2957,6 +3085,20 @@ int main (int argc, char ** argv)
2957
3085
return -1 ;
2958
3086
}
2959
3087
3088
+ /* test URL redirection with status code 3xx */
3089
+ if (test_url_redirection ()) {
3090
+ printf ("Test url_redirection: Server URL redirection Support [ OK ]\n" );
3091
+ }else {
3092
+ printf ("Test url_redirection: Server URL redirection Support [ FAILED ]\n" );
3093
+ return -1 ;
3094
+ }
3095
+
3096
+ if (test_non_redirection_status ()) {
3097
+ printf ("Test non_redirection_status: Server support for non_101 status [ OK ]\n" );
3098
+ }else {
3099
+ printf ("Test non_redirection_status: Server support for non_101 status [ FAILED ]\n" );
3100
+ return -1 ;
3101
+ }
2960
3102
/* test sending pong (without ping) */
2961
3103
2962
3104
/* test streaming api */
0 commit comments