@@ -218,6 +218,49 @@ test_server_basic(void)
218
218
nng_aio_wait (st .aio );
219
219
NUTS_PASS (nng_aio_result (st .aio ));
220
220
221
+ NUTS_HTTP_STATUS (st .conn , NNG_HTTP_STATUS_OK );
222
+
223
+ ptr = nng_http_get_header (st .conn , "Content-Length" );
224
+ NUTS_TRUE (ptr != NULL );
225
+ NUTS_TRUE (atoi (ptr ) == (int ) strlen (doc1 ));
226
+
227
+ iov .iov_len = strlen (doc1 );
228
+ iov .iov_buf = chunk ;
229
+ NUTS_PASS (nng_aio_set_iov (st .aio , 1 , & iov ));
230
+ nng_http_read_all (st .conn , st .aio );
231
+ nng_aio_wait (st .aio );
232
+ NUTS_PASS (nng_aio_result (st .aio ));
233
+ NUTS_TRUE (nng_aio_count (st .aio ) == strlen (doc1 ));
234
+ NUTS_TRUE (memcmp (chunk , doc1 , strlen (doc1 )) == 0 );
235
+
236
+ server_free (& st );
237
+ }
238
+
239
+ static void
240
+ test_server_canonify (void )
241
+ {
242
+ struct server_test st ;
243
+ char chunk [256 ];
244
+ const void * ptr ;
245
+ nng_iov iov ;
246
+ nng_http_handler * h ;
247
+
248
+ NUTS_PASS (nng_http_handler_alloc_static (
249
+ & h , "/home/index.html" , doc1 , strlen (doc1 ), "text/html" ));
250
+
251
+ server_setup (& st , h );
252
+
253
+ NUTS_PASS (nng_http_set_uri (
254
+ st .conn , "/someplace/..////home/./%69ndex.html" , NULL ));
255
+ nng_http_write_request (st .conn , st .aio );
256
+
257
+ nng_aio_wait (st .aio );
258
+ NUTS_PASS (nng_aio_result (st .aio ));
259
+
260
+ nng_http_read_response (st .conn , st .aio );
261
+ nng_aio_wait (st .aio );
262
+ NUTS_PASS (nng_aio_result (st .aio ));
263
+
221
264
NUTS_TRUE (nng_http_get_status (st .conn ) == NNG_HTTP_STATUS_OK );
222
265
223
266
ptr = nng_http_get_header (st .conn , "Content-Length" );
@@ -300,7 +343,60 @@ test_server_404(void)
300
343
nng_aio_wait (st .aio );
301
344
NUTS_PASS (nng_aio_result (st .aio ));
302
345
303
- NUTS_TRUE (nng_http_get_status (st .conn ) == NNG_HTTP_STATUS_NOT_FOUND );
346
+ NUTS_HTTP_STATUS (st .conn , NNG_HTTP_STATUS_NOT_FOUND );
347
+
348
+ server_free (& st );
349
+ }
350
+
351
+ static void
352
+ test_server_no_authoritive_form (void )
353
+ {
354
+ struct server_test st ;
355
+ nng_http_handler * h ;
356
+
357
+ NUTS_PASS (nng_http_handler_alloc_static (
358
+ & h , "/home.html" , doc1 , strlen (doc1 ), "text/html" ));
359
+
360
+ server_setup (& st , h );
361
+
362
+ NUTS_PASS (
363
+ nng_http_set_uri (st .conn , "http://127.0.0.1/home.html" , NULL ));
364
+ nng_http_write_request (st .conn , st .aio );
365
+
366
+ nng_aio_wait (st .aio );
367
+ NUTS_PASS (nng_aio_result (st .aio ));
368
+
369
+ nng_http_read_response (st .conn , st .aio );
370
+ nng_aio_wait (st .aio );
371
+ NUTS_PASS (nng_aio_result (st .aio ));
372
+
373
+ NUTS_HTTP_STATUS (st .conn , NNG_HTTP_STATUS_BAD_REQUEST );
374
+
375
+ server_free (& st );
376
+ }
377
+
378
+ static void
379
+ test_server_bad_canonify (void )
380
+ {
381
+ struct server_test st ;
382
+ nng_http_handler * h ;
383
+
384
+ server_setup (& st , NULL );
385
+
386
+ NUTS_PASS (nng_http_handler_alloc_static (
387
+ & h , "/home.html" , doc1 , strlen (doc1 ), "text/html" ));
388
+
389
+ NUTS_PASS (nng_http_set_uri (st .conn , "/%home.html" , NULL ));
390
+ nng_http_write_request (st .conn , st .aio );
391
+
392
+ nng_aio_wait (st .aio );
393
+ NUTS_PASS (nng_aio_result (st .aio ));
394
+
395
+ nng_http_read_response (st .conn , st .aio );
396
+ nng_aio_wait (st .aio );
397
+ NUTS_PASS (nng_aio_result (st .aio ));
398
+
399
+ NUTS_HTTP_STATUS (st .conn , NNG_HTTP_STATUS_BAD_REQUEST );
304
400
305
401
server_free (& st );
306
402
}
@@ -323,8 +419,7 @@ test_server_bad_version(void)
323
419
nng_aio_wait (st .aio );
324
420
NUTS_PASS (nng_aio_result (st .aio ));
325
421
326
- NUTS_TRUE (nng_http_get_status (st .conn ) ==
327
- NNG_HTTP_STATUS_HTTP_VERSION_NOT_SUPP );
422
+ NUTS_HTTP_STATUS (st .conn , NNG_HTTP_STATUS_HTTP_VERSION_NOT_SUPP );
328
423
329
424
server_free (& st );
330
425
}
@@ -346,7 +441,7 @@ test_server_missing_host(void)
346
441
nng_aio_wait (st .aio );
347
442
NUTS_PASS (nng_aio_result (st .aio ));
348
443
349
- NUTS_TRUE ( nng_http_get_status ( st .conn ) == 400 );
444
+ NUTS_HTTP_STATUS ( st .conn , NNG_HTTP_STATUS_BAD_REQUEST );
350
445
351
446
server_free (& st );
352
447
}
@@ -388,10 +483,7 @@ test_server_wrong_method(void)
388
483
nng_aio_wait (st .aio );
389
484
NUTS_PASS (nng_aio_result (st .aio ));
390
485
391
- NUTS_TRUE (nng_http_get_status (st .conn ) ==
392
- NNG_HTTP_STATUS_METHOD_NOT_ALLOWED );
393
- NUTS_MSG ("Got result %d: %s" , nng_http_get_status (st .conn ),
394
- nng_http_get_reason (st .conn ));
486
+ NUTS_HTTP_STATUS (st .conn , NNG_HTTP_STATUS_METHOD_NOT_ALLOWED );
395
487
396
488
server_free (& st );
397
489
}
@@ -417,7 +509,7 @@ test_server_post_handler(void)
417
509
nng_http_set_body (st .conn , txdata , strlen (txdata ));
418
510
nng_http_set_method (st .conn , "POST" );
419
511
NUTS_PASS (httpdo (& st , (void * * ) & rxdata , & size ));
420
- NUTS_TRUE ( nng_http_get_status ( st .conn ) == NNG_HTTP_STATUS_OK );
512
+ NUTS_HTTP_STATUS ( st .conn , NNG_HTTP_STATUS_OK );
421
513
NUTS_TRUE (size == strlen (txdata ));
422
514
NUTS_TRUE (strncmp (txdata , rxdata , size ) == 0 );
423
515
nng_free (rxdata , size );
@@ -429,9 +521,7 @@ test_server_post_handler(void)
429
521
nng_http_set_body (st .conn , txdata , strlen (txdata ));
430
522
431
523
NUTS_PASS (httpdo (& st , & data , & size ));
432
- NUTS_TRUE (nng_http_get_status (st .conn ) ==
433
- NNG_HTTP_STATUS_METHOD_NOT_ALLOWED );
434
- NUTS_MSG ("HTTP status was %u" , nng_http_get_status (st .conn ));
524
+ NUTS_HTTP_STATUS (st .conn , NNG_HTTP_STATUS_METHOD_NOT_ALLOWED );
435
525
nng_free (data , size );
436
526
437
527
server_free (& st );
@@ -440,25 +530,22 @@ test_server_post_handler(void)
440
530
static void
441
531
test_server_get_redirect (void )
442
532
{
443
- char fullurl [256 ];
444
533
const char * dest ;
445
534
void * data ;
446
535
size_t size ;
447
536
nng_http_handler * h ;
448
537
struct server_test st ;
449
538
450
- // We'll use a 303 to ensure codes carry thru
539
+ // We'll use a 303 (SEE OTHER) to ensure codes carry thru
451
540
NUTS_PASS (nng_http_handler_alloc_redirect (
452
- & h , "/here" , 303 , "http://127.0.0.1/there" ));
541
+ & h , "/here" , NNG_HTTP_STATUS_SEE_OTHER , "http://127.0.0.1/there" ));
453
542
server_setup (& st , h );
454
543
455
544
NUTS_PASS (nng_http_set_uri (st .conn , "/here" , NULL ));
456
545
nng_http_set_method (st .conn , "GET" );
457
546
458
547
NUTS_PASS (httpdo (& st , & data , & size ));
459
- NUTS_TRUE (nng_http_get_status (st .conn ) == 303 );
460
- NUTS_MSG ("HTTP status got %d, expected %d (url %s)" ,
461
- nng_http_get_status (st .conn ), 303 , fullurl );
548
+ NUTS_HTTP_STATUS (st .conn , NNG_HTTP_STATUS_SEE_OTHER );
462
549
NUTS_TRUE ((dest = nng_http_get_header (st .conn , "Location" )) != NULL );
463
550
NUTS_MATCH (dest , "http://127.0.0.1/there" );
464
551
nng_free (data , size );
@@ -469,26 +556,23 @@ test_server_get_redirect(void)
469
556
static void
470
557
test_server_tree_redirect (void )
471
558
{
472
- char fullurl [256 ];
473
559
const char * dest ;
474
560
void * data ;
475
561
size_t size ;
476
562
nng_http_handler * h ;
477
563
struct server_test st ;
478
564
479
- // We'll use a 303 to ensure codes carry thru
480
- NUTS_PASS (nng_http_handler_alloc_redirect (
481
- & h , "/here" , 303 , "http://127.0.0.1/there" ));
565
+ // We'll use a permanent redirect to ensure codes carry thru
566
+ NUTS_PASS (nng_http_handler_alloc_redirect (& h , "/here" ,
567
+ NNG_HTTP_STATUS_PERMANENT_REDIRECT , "http://127.0.0.1/there" ));
482
568
nng_http_handler_set_tree (h );
483
569
server_setup (& st , h );
484
570
485
571
NUTS_PASS (nng_http_set_uri (st .conn , "/here/i/go/again" , NULL ));
486
572
nng_http_set_method (st .conn , "GET" );
487
573
488
574
NUTS_PASS (httpdo (& st , & data , & size ));
489
- NUTS_TRUE (nng_http_get_status (st .conn ) == 303 );
490
- NUTS_MSG ("HTTP status got %d, expected %d (url %s)" ,
491
- nng_http_get_status (st .conn ), 303 , fullurl );
575
+ NUTS_HTTP_STATUS (st .conn , NNG_HTTP_STATUS_PERMANENT_REDIRECT );
492
576
NUTS_TRUE ((dest = nng_http_get_header (st .conn , "Location" )) != NULL );
493
577
NUTS_MATCH (dest , "http://127.0.0.1/there/i/go/again" );
494
578
nng_free (data , size );
@@ -933,8 +1017,11 @@ test_serve_subdir_index(void)
933
1017
934
1018
NUTS_TESTS = {
935
1019
{ "server basic" , test_server_basic },
1020
+ { "server canonify" , test_server_canonify },
936
1021
{ "server head" , test_server_head },
937
1022
{ "server 404" , test_server_404 },
1023
+ { "server authoritiative form" , test_server_no_authoritive_form },
1024
+ { "server bad canonify" , test_server_bad_canonify },
938
1025
{ "server bad version" , test_server_bad_version },
939
1026
{ "server missing host" , test_server_missing_host },
940
1027
{ "server wrong method" , test_server_wrong_method },
0 commit comments