Skip to content

Commit 01f5c11

Browse files
committed
Add regression tests for URL redirection.
1 parent f0887dc commit 01f5c11

File tree

3 files changed

+189
-2
lines changed

3 files changed

+189
-2
lines changed

src/nopoll_conn.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5035,11 +5035,11 @@ nopoll_bool nopoll_conn_wait_for_status_until_connection_ready (noPollConn
50355035
nopoll_log (conn->ctx, NOPOLL_LEVEL_DEBUG, "nopoll_conn_wait_for_status_until_connection_ready() response: status: %d" ,*status );
50365036
return nopoll_false; /* retry as server returns error http code */
50375037
}
5038-
else if(result)
5038+
else if(result && message != NULL)
50395039
{
50405040
*message = nopoll_strdup_printf("Success");
50415041
}
5042-
else if(!result)
5042+
else if(!result && message != NULL)
50435043
{
50445044
*message = nopoll_strdup_printf("Failure");
50455045
}

test/nopoll-regression-client.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,134 @@ nopoll_bool test_02b (void) {
465465
return nopoll_true;
466466
}
467467

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+
468596

469597
nopoll_bool test_03 (void) {
470598
noPollCtx * ctx;
@@ -2957,6 +3085,20 @@ int main (int argc, char ** argv)
29573085
return -1;
29583086
}
29593087

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+
}
29603102
/* test sending pong (without ping) */
29613103

29623104
/* test streaming api */

test/nopoll-regression-listener.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#include <openssl/x509v3.h>
4747
#include <openssl/ssl.h>
4848
#include <openssl/err.h>
49+
#include <unistd.h>
50+
#include <pthread.h>
4951

5052
int connection_close_count = 0;
5153

@@ -375,6 +377,44 @@ noPollPtr ssl_context_creator (noPollCtx * ctx, noPollConn * conn, noPollConnOpt
375377
printf ("RETURNING: ssl context reference %p\n", ssl_ctx);
376378
return ssl_ctx;
377379
}
380+
void *start_listener(char *reply, int port) {
381+
382+
NOPOLL_SOCKET listener, client_sock;
383+
struct sockaddr_in serveraddr;
384+
385+
listener = socket(AF_INET, SOCK_STREAM, 0);
386+
memset( &serveraddr,0, sizeof(serveraddr));
387+
388+
serveraddr.sin_family = AF_INET;
389+
serveraddr.sin_addr.s_addr = htons(INADDR_ANY);
390+
serveraddr.sin_port = htons(port);
391+
392+
printf("noPoll listener started at: %s:%d.\n", inet_ntoa(serveraddr.sin_addr), htons(serveraddr.sin_port));
393+
bind(listener, (struct sockaddr *) &serveraddr, sizeof(serveraddr));
394+
395+
listen(listener, 1);
396+
397+
client_sock = accept(listener, (struct sockaddr*) NULL, NULL);
398+
printf("INFO: Received request at %s:%d, replying to client %s",inet_ntoa(serveraddr.sin_addr), htons(serveraddr.sin_port),reply);
399+
400+
if(write(client_sock, reply, strlen(reply)+1)){
401+
close(client_sock);
402+
}
403+
else {
404+
printf("ERROR: Unable to write data on socket\n");
405+
}
406+
return NULL;
407+
}
408+
409+
void *listener_for_URL_redirection (void *vargp) {
410+
char server_response[] = "HTTP/1.1 307 Temporary Redirect\r\nLocation: http://localhost:1234\r\n\r\n";
411+
return start_listener(server_response,6789);
412+
}
413+
414+
void *listener_for_non_101_status (void *vargp) {
415+
char server_response[] = "HTTP/1.1 500 Internal Server Error\r\n\r\n";
416+
return start_listener(server_response,9876);
417+
}
378418

379419
int main (int argc, char ** argv)
380420
{
@@ -428,6 +468,11 @@ int main (int argc, char ** argv)
428468
iterator++;
429469
}
430470

471+
/* create listener for URL redirection */
472+
pthread_t tid1, tid2;
473+
pthread_create(&tid1, NULL, listener_for_URL_redirection, NULL);
474+
pthread_create(&tid2, NULL, listener_for_non_101_status, NULL);
475+
431476
/* call to create a listener */
432477
listener = nopoll_listener_new (ctx, "0.0.0.0", "1234");
433478
if (! nopoll_conn_is_ok (listener)) {

0 commit comments

Comments
 (0)