TPSGI-Router.t 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 {
  24. openapi => '3.0.3',
  25. info => { title => 'test', version => '1' },
  26. components => { schemas => { Count => { type => 'object', properties => { count => { type => 'integer' } } } } },
  27. paths => \%paths,
  28. };
  29. }
  30. my $router = TPSGI::Router->new(
  31. 'Test.pm' => document(
  32. '/sitemap/static.xml' => { get => operation() },
  33. '/sitemap/{map}.xml' => { get => operation( parameters => [ path_parameter('map') ] ) },
  34. '/sitemap/{map}.xml.gz' => { get => operation( parameters => [ path_parameter('map') ] ) },
  35. '/lib/{module+}' => { get => operation( parameters => [ path_parameter('module+') ] ) },
  36. '/lib/index' => { get => operation() },
  37. '/{year}-{month}/{slug}' => { get => operation( parameters => [ map { path_parameter($_) } qw{year month slug} ] ) },
  38. '/humans.txt' => { get => operation( 'x-tpsgi-callback' => 'main::named' ) },
  39. '/post/{slug}' => { get => operation( parameters => [ path_parameter('slug') ] ) },
  40. '/form' => {
  41. post => operation(
  42. requestBody => {
  43. content => {
  44. 'application/x-www-form-urlencoded' => {
  45. schema => {
  46. type => 'object',
  47. additionalProperties => false,
  48. properties => { count => { type => 'integer' } },
  49. },
  50. },
  51. },
  52. },
  53. ),
  54. },
  55. '/upload' => {
  56. post => operation(
  57. requestBody => {
  58. content => {
  59. 'multipart/form-data' => {
  60. schema => {
  61. type => 'object',
  62. properties => { count => { type => 'integer' }, file => { type => 'string', format => 'binary' } },
  63. },
  64. },
  65. 'application/x-www-form-urlencoded' => { schema => { '$ref' => '#/components/schemas/Count' } },
  66. },
  67. },
  68. ),
  69. },
  70. '/open/{slug}' => {
  71. post => operation(
  72. parameters => [ path_parameter('slug') ],
  73. requestBody => { content => { 'application/x-www-form-urlencoded' => { schema => { type => 'object', additionalProperties => true } } } },
  74. ),
  75. },
  76. '/open' => {
  77. post => operation(
  78. requestBody => {
  79. content => {
  80. 'application/x-www-form-urlencoded' => {
  81. schema => { type => 'object', additionalProperties => true, properties => { count => { type => 'integer' } } },
  82. },
  83. },
  84. },
  85. ),
  86. },
  87. '/search' => {
  88. get => operation(
  89. parameters => [
  90. { name => 'limit', in => 'query', schema => { type => 'integer' } },
  91. { name => 'exact', in => 'query', schema => { type => 'boolean' } },
  92. ],
  93. ),
  94. },
  95. '/loose' => { get => operation() },
  96. '/news/{slug}' => { get => operation( parameters => [ path_parameter('slug') ] ) },
  97. '/{section}/latest' => { get => operation( parameters => [ path_parameter('section') ] ) },
  98. ),
  99. );
  100. subtest 'a literal template wins over a variable' => sub {
  101. is( $router->match( 'GET', '/sitemap/static.xml' )->{template}, '/sitemap/static.xml', 'the static sitemap is the static route' );
  102. my $route = $router->match( 'GET', '/sitemap/1.xml' );
  103. is( $route->{template}, '/sitemap/{map}.xml', 'and any other reaches the template' );
  104. is( $route->{path}, { map => 1 }, 'with the variable' );
  105. is( $router->match( 'GET', '/sitemap/1.xml.gz' )->{template}, '/sitemap/{map}.xml.gz', 'and the compressed one its own template' );
  106. is( $router->match( 'GET', '/lib/index' )->{template}, '/lib/index', 'a literal path wins over {name+}' );
  107. is( $router->match( 'GET', '/news/latest' )->{template}, '/news/{slug}', 'and an earlier literal segment wins over more literal characters' );
  108. };
  109. subtest 'variables' => sub {
  110. my $route = $router->match( 'GET', '/2026-10/hello' );
  111. is( $route->{path}, { year => 2026, month => 10, slug => 'hello' }, 'two variables in one segment' );
  112. $route = $router->match( 'GET', '/lib/Trog/Auth.html' );
  113. is( $route->{path}, { module => 'Trog/Auth.html' }, '{module+} takes the rest of the path, slashes and all, as module' );
  114. my $cafe = "caf\N{LATIN SMALL LETTER E WITH ACUTE}";
  115. is( $router->match( 'GET', "/post/$cafe" )->{path}, { slug => $cafe }, 'a variable takes a letter outside ASCII' );
  116. is( $router->match( 'GET', "/post/a\nb" ), undef, 'and no newline' );
  117. is( $router->match( 'GET', '/post/a/b' ), undef, 'and no slash' );
  118. is( $router->match( 'GET', "/lib/a\nb" ), undef, 'nor does {name+} take a newline' );
  119. };
  120. subtest 'a literal character is only itself' => sub {
  121. is( $router->match( 'GET', '/humans.txt' )->{template}, '/humans.txt', 'the dot matches a dot' );
  122. is( $router->match( 'GET', '/humansXtxt' ), undef, 'and nothing else' );
  123. is( $router->match( 'GET', '/sitemap/1Xxml' ), undef, 'in a segment with a variable as well' );
  124. };
  125. subtest 'callbacks' => sub {
  126. is( $router->match( 'GET', '/loose' )->{callback}->(), 'coderef', 'a CODE reference is the callback' );
  127. is( $router->match( 'GET', '/humans.txt' )->{callback}->(), 'named', 'and a name is resolved to its sub' );
  128. like(
  129. dies { TPSGI::Router->new( 'Bad.pm' => document( '/x' => { get => { responses => { 200 => { description => 'ok' } } } } ) ) },
  130. qr/Bad[.]pm:[ ]GET[ ]\/x[ ]has[ ]no[ ]x-tpsgi-callback/,
  131. 'an operation without a callback dies'
  132. );
  133. like(
  134. dies { TPSGI::Router->new( 'Bad.pm' => document( '/x' => { get => operation( 'x-tpsgi-callback' => 'main::no_such_sub' ) } ) ) },
  135. qr/no_such_sub,[ ]which[ ]is[ ]not[ ]a[ ]defined/,
  136. 'and so does a name that is not a sub'
  137. );
  138. };
  139. subtest 'booleans' => sub {
  140. my $json = { %{ path_parameter('x') }, required => Cpanel::JSON::XS::true };
  141. ok(
  142. lives { TPSGI::Router->new( 'Json.pm' => document( '/j/{x}' => { get => operation( parameters => [$json] ) } ) ) },
  143. 'a JSON boolean is a boolean'
  144. ) or note $@;
  145. # $router's own document marks its path parameters required with perl's own true.
  146. is( $router->match( 'GET', '/post/a' )->{template}, '/post/{slug}', "and so is perl's own, which every document above uses" );
  147. my $document = document( '/n/{x}' => { get => operation( parameters => [ path_parameter('x') ] ) } );
  148. TPSGI::Router->new( 'Native.pm' => $document );
  149. ok( !ref $document->{paths}{'/n/{x}'}{get}{parameters}[0]{required}, 'and the router leaves the document it was given as it was' );
  150. };
  151. subtest 'documents that are refused' => sub {
  152. like(
  153. dies { TPSGI::Router->new( 'Bad.pm' => { openapi => '3.0.3', paths => {} } ) },
  154. qr/is[ ]not[ ]a[ ]valid[ ]OpenAPI[ ]3[ ]document/,
  155. 'an invalid document dies with the reason'
  156. );
  157. like(
  158. dies { TPSGI::Router->new( 'Bad.pm' => document( '/a/{x}' => { get => operation() }, '/a/{y}' => { post => operation() } ) ) },
  159. qr/[}][ ]and[ ]\/a\/[{].[}][ ]match[ ]the[ ]same/,
  160. 'two templates that differ only in their variable names die'
  161. );
  162. like(
  163. dies { TPSGI::Router->new( 'One.pm' => document( '/a' => { get => operation() } ), 'Two.pm' => document( '/a' => { get => operation() } ) ) },
  164. qr/Two[.]pm:[ ]GET[ ]\/a[ ]is[ ]also[ ]a[ ]route/,
  165. 'one template and method in two routers dies'
  166. );
  167. ok(
  168. lives { TPSGI::Router->new( 'One.pm' => document( '/a' => { get => operation() } ), 'Two.pm' => document( '/a' => { post => operation() } ) ) },
  169. 'but two methods of one template in two routers do not'
  170. ) or note $@;
  171. like( dies { TPSGI::Router::compile_template('/a/{x+}/b') }, qr/not[ ]the[ ]whole[ ]last[ ]segment/, '{name+} before the last segment dies' );
  172. like( dies { TPSGI::Router::compile_template('/a/b{x+}') }, qr/not[ ]the[ ]whole[ ]last[ ]segment/, 'and so does {name+} with text beside it' );
  173. like( dies { TPSGI::Router::compile_template('/a/{x') }, qr/a[ ]brace[ ]that[ ]is[ ]not/, 'and a brace that is not a variable' );
  174. };
  175. subtest 'methods' => sub {
  176. is( $router->match( 'HEAD', '/loose' )->{method}, 'get', 'HEAD falls back to get' );
  177. my $route = $router->match( 'POST', '/loose' );
  178. is( $route->{status}, 405, 'a path whose template has no operation for the method is a 405' );
  179. is( $route->{allow}, [qw{GET HEAD}], 'which names the methods it does have' );
  180. is( $router->match( 'GET', '/nowhere' ), undef, 'and a path that no template matches is nothing at all' );
  181. };
  182. subtest 'validation' => sub {
  183. my $route = $router->match( 'GET', '/search' );
  184. my %query = ( limit => '5', exact => 'true' );
  185. is( [ $router->validate( $route, query => \%query ) ], [], 'a valid query passes' );
  186. is( Mojo::JSON::encode_json( \%query ), '{"exact":true,"limit":5}', 'and arrives coerced: a number and a boolean, not two strings' );
  187. my ( $status, @errors ) = $router->validate( $route, query => { limit => 'many' } );
  188. is( $status, 400, 'a value that its schema refuses is a 400' );
  189. like( \@errors, [qr/\/limit:[ ]Expected[ ]integer/], 'for that reason' );
  190. ( $status, @errors ) = $router->validate( $route, query => { limit => 1, junk => 1 } );
  191. is( $status, 400, 'an undeclared query key is a 400' );
  192. like( \@errors, [qr/\/junk:/], 'and named' );
  193. ( $status, @errors ) = $router->validate( $route, query => { limit => 1, route => '/x' } );
  194. is( $status, 400, 'even one that tPSGI sets in the query itself, since the input is apart from it' );
  195. ( $status, @errors ) = $router->validate( $router->match( 'GET', '/loose' ), query => { anything => 1 } );
  196. is( $status, 400, 'and so is any key of the query, for an operation that declares nothing' );
  197. like( \@errors, [qr/\/anything:/], 'named too' );
  198. is( [ $router->validate( $router->match( 'GET', '/loose' ), query => {} ) ], [], 'which takes an empty query' );
  199. $route = $router->match( 'POST', '/form' );
  200. my $form_type = 'application/x-www-form-urlencoded';
  201. is( [ $router->validate( $route, body => { count => '3' }, content_type => $form_type ) ], [], 'a form that its schema takes passes' );
  202. ( $status, @errors ) = $router->validate( $route, body => { count => 3, junk => 1 }, content_type => $form_type );
  203. is( $status, 400, 'a form key that additionalProperties refuses is a 400' );
  204. like( \@errors, [qr/Properties[ ]not[ ]allowed:[ ]junk/], 'for that reason' );
  205. ( $status, @errors ) = $router->validate( $route, body => { count => 3 }, content_type => 'text/plain' );
  206. is( $status, 415, 'a body of a media type the operation does not take is a 415' );
  207. is( [ $router->validate( $route, body => { count => 3 }, content_type => "$form_type; charset=UTF-8" ) ], [], 'and parameters of the media type do not count' );
  208. };
  209. subtest 'the names of a body' => sub {
  210. my $form_type = 'application/x-www-form-urlencoded';
  211. my $multipart = 'multipart/form-data; boundary=x';
  212. my ( $status, @errors ) = $router->validate( $router->match( 'GET', '/loose' ), body => { count => 3 }, content_type => $form_type );
  213. is( $status, 400, 'a field, to an operation with no requestBody, is a 400' );
  214. like( \@errors, [qr/\/body\/count:[ ]the[ ]operation[ ]takes[ ]no[ ]body/], 'for that reason' );
  215. ( $status, @errors ) = $router->validate( $router->match( 'GET', '/loose' ), body => {}, uploads => ['file'], content_type => $multipart );
  216. is( $status, 400, 'and so is a file' );
  217. like( \@errors, [qr/\/body\/file:/], 'named' );
  218. is( [ $router->validate( $router->match( 'GET', '/loose' ), body => {}, content_type => $form_type ) ], [], 'but an empty body is not' );
  219. my $route = $router->match( 'POST', '/upload' );
  220. is( [ $router->validate( $route, body => { count => 3 }, uploads => ['file'], content_type => $multipart ) ], [], 'a field and a file that the schema names pass' );
  221. ( $status, @errors ) = $router->validate( $route, body => { count => 3, junk => 1 }, content_type => $multipart );
  222. is( $status, 400, 'a field that a schema without additionalProperties does not name is a 400' );
  223. like( \@errors, [qr/\/body\/junk:[ ]the[ ]schema[ ]of[ ]multipart\/form-data/], 'which names the media type' );
  224. ( $status, @errors ) = $router->validate( $route, body => { count => 3 }, uploads => ['other'], content_type => $multipart );
  225. is( $status, 400, 'and so is a file that it does not name' );
  226. like( \@errors, [qr/\/body\/other:/], 'named' );
  227. ( $status, @errors ) = $router->validate( $route, body => { count => 3, file => 'x' }, content_type => $form_type );
  228. is( $status, 400, 'the names are those of the media type that was sent, here a $ref' );
  229. like( \@errors, [qr/\/body\/file:[ ]the[ ]schema[ ]of[ ]\Q$form_type\E/], 'which does not name the file' );
  230. is( [ $router->validate( $route, body => { count => 3 }, content_type => $form_type ) ], [], 'and does name the count' );
  231. is( [ $router->validate( $router->match( 'POST', '/open' ), body => { count => 3, junk => 1 }, content_type => $form_type ) ], [], 'a schema with additionalProperties takes what it says it takes' );
  232. ( $status, @errors ) = $router->validate( $router->match( 'POST', '/open/x' ), body => { slug => 'y' }, content_type => $form_type );
  233. is( $status, 400, 'but not a name that the path or the query declares' );
  234. like( \@errors, [qr/\/body\/slug:[ ]the[ ]path[ ]or[ ]the[ ]query/], 'for that reason' );
  235. };
  236. subtest 'one name in two places' => sub {
  237. my $form = { content => { 'application/x-www-form-urlencoded' => { schema => { type => 'object', properties => { limit => { type => 'integer' } } } } } };
  238. my $query = { name => 'limit', in => 'query', schema => { type => 'integer' } };
  239. my $limit = qr/limit[ ]is[ ]a[ ]name[ ]of/;
  240. like(
  241. dies { TPSGI::Router->new( 'Test.pm' => document( '/a' => { post => operation( parameters => [$query], requestBody => $form ) } ) ) },
  242. qr/POST[ ]\/a:[ ]$limit[ ]the[ ]query[ ]and[ ]of[ ]the[ ]body/,
  243. 'the query and the body die'
  244. );
  245. like(
  246. dies { TPSGI::Router->new( 'Test.pm' => document( '/a/{limit}' => { get => operation( parameters => [ $query, path_parameter('limit') ] ) } ) ) },
  247. qr/$limit[ ]the[ ]path[ ]and[ ]of[ ]the[ ]query/,
  248. 'and so do the path and the query'
  249. );
  250. like(
  251. dies { TPSGI::Router->new( 'Test.pm' => document( '/a' => { get => operation( parameters => [$query], 'x-tpsgi-data' => { limit => 1 } ) } ) ) },
  252. qr/x-tpsgi-data[ ]sets[ ]limit,[ ]which[ ]is[ ]a[ ]name/,
  253. 'and x-tpsgi-data that sets a name the request can give'
  254. );
  255. ok( lives { TPSGI::Router->new( 'Test.pm' => document( '/a' => { get => operation( parameters => [$query], 'x-tpsgi-data' => { other => 1 } ) } ) ) }, 'but not x-tpsgi-data of another name' ) or note $@;
  256. };
  257. subtest 'precedence' => sub {
  258. my @order = map { $_->{template} } grep { index( $_->{template}, '/sitemap/' ) == 0 || index( $_->{template}, '/lib/' ) == 0 } $router->routes();
  259. 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' );
  260. };
  261. done_testing();