TPSGI-Router.t 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env perl
  2. use 5.040;
  3. use warnings FATAL => 'all';
  4. use re '/aasx';
  5. use Test2::V0;
  6. use Test2::Plugin::NoWarnings;
  7. use Mojo::JSON ();
  8. use FindBin::libs;
  9. use TPSGI::Router;
  10. sub named { return 'named' }
  11. sub coderef { return 'coderef' }
  12. sub path_parameter {
  13. my ( $name, %schema ) = @_;
  14. return { name => $name, in => 'path', required => Mojo::JSON::true, schema => { type => 'string', %schema } };
  15. }
  16. sub operation {
  17. my (%operation) = @_;
  18. return { 'x-tpsgi-callback' => \&coderef, responses => { 200 => { description => 'ok' } }, %operation };
  19. }
  20. sub document {
  21. my (%paths) = @_;
  22. return { openapi => '3.0.3', info => { title => 'test', version => '1' }, paths => \%paths };
  23. }
  24. my $router = TPSGI::Router->new(
  25. 'Test.pm' => document(
  26. '/sitemap/static.xml' => { get => operation() },
  27. '/sitemap/{map}.xml' => { get => operation( parameters => [ path_parameter('map') ] ) },
  28. '/sitemap/{map}.xml.gz' => { get => operation( parameters => [ path_parameter('map') ] ) },
  29. '/lib/{module+}' => { get => operation( parameters => [ path_parameter('module+') ] ) },
  30. '/lib/index' => { get => operation() },
  31. '/{year}-{month}/{slug}' => { get => operation( parameters => [ map { path_parameter($_) } qw{year month slug} ] ) },
  32. '/humans.txt' => { get => operation( 'x-tpsgi-callback' => 'main::named' ) },
  33. '/post/{slug}' => { get => operation( parameters => [ path_parameter('slug') ] ) },
  34. '/form' => {
  35. post => operation(
  36. requestBody => {
  37. content => {
  38. 'application/x-www-form-urlencoded' => {
  39. schema => {
  40. type => 'object',
  41. additionalProperties => Mojo::JSON::false,
  42. properties => { count => { type => 'integer' } },
  43. },
  44. },
  45. },
  46. },
  47. ),
  48. },
  49. '/search' => {
  50. get => operation(
  51. parameters => [
  52. { name => 'limit', in => 'query', schema => { type => 'integer' } },
  53. { name => 'exact', in => 'query', schema => { type => 'boolean' } },
  54. ],
  55. ),
  56. },
  57. '/loose' => { get => operation() },
  58. '/news/{slug}' => { get => operation( parameters => [ path_parameter('slug') ] ) },
  59. '/{section}/latest' => { get => operation( parameters => [ path_parameter('section') ] ) },
  60. ),
  61. );
  62. subtest 'a literal template wins over a variable' => sub {
  63. is( $router->match( 'GET', '/sitemap/static.xml' )->{template}, '/sitemap/static.xml', 'the static sitemap is the static route' );
  64. my $route = $router->match( 'GET', '/sitemap/1.xml' );
  65. is( $route->{template}, '/sitemap/{map}.xml', 'and any other reaches the template' );
  66. is( $route->{path}, { map => 1 }, 'with the variable' );
  67. is( $router->match( 'GET', '/sitemap/1.xml.gz' )->{template}, '/sitemap/{map}.xml.gz', 'and the compressed one its own template' );
  68. is( $router->match( 'GET', '/lib/index' )->{template}, '/lib/index', 'a literal path wins over {name+}' );
  69. is( $router->match( 'GET', '/news/latest' )->{template}, '/news/{slug}', 'and an earlier literal segment wins over more literal characters' );
  70. };
  71. subtest 'variables' => sub {
  72. my $route = $router->match( 'GET', '/2026-10/hello' );
  73. is( $route->{path}, { year => 2026, month => 10, slug => 'hello' }, 'two variables in one segment' );
  74. $route = $router->match( 'GET', '/lib/Trog/Auth.html' );
  75. is( $route->{path}, { module => 'Trog/Auth.html' }, '{module+} takes the rest of the path, slashes and all, as module' );
  76. my $cafe = "caf\N{LATIN SMALL LETTER E WITH ACUTE}";
  77. is( $router->match( 'GET', "/post/$cafe" )->{path}, { slug => $cafe }, 'a variable takes a letter outside ASCII' );
  78. is( $router->match( 'GET', "/post/a\nb" ), undef, 'and no newline' );
  79. is( $router->match( 'GET', '/post/a/b' ), undef, 'and no slash' );
  80. is( $router->match( 'GET', "/lib/a\nb" ), undef, 'nor does {name+} take a newline' );
  81. };
  82. subtest 'a literal character is only itself' => sub {
  83. is( $router->match( 'GET', '/humans.txt' )->{template}, '/humans.txt', 'the dot matches a dot' );
  84. is( $router->match( 'GET', '/humansXtxt' ), undef, 'and nothing else' );
  85. is( $router->match( 'GET', '/sitemap/1Xxml' ), undef, 'in a segment with a variable as well' );
  86. };
  87. subtest 'callbacks' => sub {
  88. is( $router->match( 'GET', '/loose' )->{callback}->(), 'coderef', 'a CODE reference is the callback' );
  89. is( $router->match( 'GET', '/humans.txt' )->{callback}->(), 'named', 'and a name is resolved to its sub' );
  90. like(
  91. dies { TPSGI::Router->new( 'Bad.pm' => document( '/x' => { get => { responses => { 200 => { description => 'ok' } } } } ) ) },
  92. qr/Bad[.]pm:[ ]GET[ ]\/x[ ]has[ ]no[ ]x-tpsgi-callback/,
  93. 'an operation without a callback dies'
  94. );
  95. like(
  96. dies { TPSGI::Router->new( 'Bad.pm' => document( '/x' => { get => operation( 'x-tpsgi-callback' => 'main::no_such_sub' ) } ) ) },
  97. qr/no_such_sub,[ ]which[ ]is[ ]not[ ]a[ ]defined/,
  98. 'and so does a name that is not a sub'
  99. );
  100. };
  101. subtest 'documents that are refused' => sub {
  102. like(
  103. dies { TPSGI::Router->new( 'Bad.pm' => { openapi => '3.0.3', paths => {} } ) },
  104. qr/is[ ]not[ ]a[ ]valid[ ]OpenAPI[ ]3[ ]document/,
  105. 'an invalid document dies with the reason'
  106. );
  107. like(
  108. dies { TPSGI::Router->new( 'Bad.pm' => document( '/a/{x}' => { get => operation() }, '/a/{y}' => { post => operation() } ) ) },
  109. qr/[}][ ]and[ ]\/a\/[{].[}][ ]match[ ]the[ ]same/,
  110. 'two templates that differ only in their variable names die'
  111. );
  112. like(
  113. dies { TPSGI::Router->new( 'One.pm' => document( '/a' => { get => operation() } ), 'Two.pm' => document( '/a' => { get => operation() } ) ) },
  114. qr/Two[.]pm:[ ]GET[ ]\/a[ ]is[ ]also[ ]a[ ]route/,
  115. 'one template and method in two routers dies'
  116. );
  117. ok(
  118. lives { TPSGI::Router->new( 'One.pm' => document( '/a' => { get => operation() } ), 'Two.pm' => document( '/a' => { post => operation() } ) ) },
  119. 'but two methods of one template in two routers do not'
  120. ) or note $@;
  121. like( dies { TPSGI::Router::compile_template('/a/{x+}/b') }, qr/not[ ]the[ ]whole[ ]last[ ]segment/, '{name+} before the last segment dies' );
  122. like( dies { TPSGI::Router::compile_template('/a/b{x+}') }, qr/not[ ]the[ ]whole[ ]last[ ]segment/, 'and so does {name+} with text beside it' );
  123. like( dies { TPSGI::Router::compile_template('/a/{x') }, qr/a[ ]brace[ ]that[ ]is[ ]not/, 'and a brace that is not a variable' );
  124. };
  125. subtest 'methods' => sub {
  126. is( $router->match( 'HEAD', '/loose' )->{method}, 'get', 'HEAD falls back to get' );
  127. my $route = $router->match( 'POST', '/loose' );
  128. is( $route->{status}, 405, 'a path whose template has no operation for the method is a 405' );
  129. is( $route->{allow}, [qw{GET HEAD}], 'which names the methods it does have' );
  130. is( $router->match( 'GET', '/nowhere' ), undef, 'and a path that no template matches is nothing at all' );
  131. };
  132. subtest 'validation' => sub {
  133. my $route = $router->match( 'GET', '/search' );
  134. my %query = ( limit => '5', exact => 'true' );
  135. is( [ $router->validate( $route, query => \%query ) ], [], 'a valid query passes' );
  136. is( Mojo::JSON::encode_json( \%query ), '{"exact":true,"limit":5}', 'and arrives coerced: a number and a boolean, not two strings' );
  137. my ( $status, @errors ) = $router->validate( $route, query => { limit => 'many' } );
  138. is( $status, 400, 'a value that its schema refuses is a 400' );
  139. like( \@errors, [qr/\/limit:[ ]Expected[ ]integer/], 'for that reason' );
  140. ( $status, @errors ) = $router->validate( $route, query => { limit => 1, junk => 1 } );
  141. is( $status, 400, 'an undeclared query key is a 400' );
  142. like( \@errors, [qr/\/junk:/], 'and named' );
  143. is( [ $router->validate( $route, query => { limit => 1, route => '/x' } ) ], [], 'except a key that tPSGI sets itself' );
  144. is( [ $router->validate( $router->match( 'GET', '/loose' ), query => { anything => 1 } ) ], [], 'and an operation that declares nothing takes anything' );
  145. $route = $router->match( 'POST', '/form' );
  146. my $form_type = 'application/x-www-form-urlencoded';
  147. is( [ $router->validate( $route, body => { count => '3' }, content_type => $form_type ) ], [], 'a form that its schema takes passes' );
  148. ( $status, @errors ) = $router->validate( $route, body => { count => 3, junk => 1 }, content_type => $form_type );
  149. is( $status, 400, 'a form key that additionalProperties refuses is a 400' );
  150. like( \@errors, [qr/Properties[ ]not[ ]allowed:[ ]junk/], 'for that reason' );
  151. ( $status, @errors ) = $router->validate( $route, body => { count => 3 }, content_type => 'text/plain' );
  152. is( $status, 415, 'a body of a media type the operation does not take is a 415' );
  153. is( [ $router->validate( $route, body => { count => 3 }, content_type => "$form_type; charset=UTF-8" ) ], [], 'and parameters of the media type do not count' );
  154. };
  155. subtest 'precedence' => sub {
  156. my @order = map { $_->{template} } grep { index( $_->{template}, '/sitemap/' ) == 0 || index( $_->{template}, '/lib/' ) == 0 } $router->routes();
  157. is( \@order, [qw{/lib/index /sitemap/static.xml /sitemap/{map}.xml.gz /sitemap/{map}.xml /lib/{module+}}], 'literal routes first, then by segment, then by literal length' );
  158. };
  159. done_testing();