TPSGI-Router.t 11 KB

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