testuri.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2003 by the Free Pascal development team
  4. Test uriparser unit
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program TestUri;
  12. {$IFDEF FPC}
  13. {$MODE OBJFPC}{$H+}
  14. {$ENDIF}
  15. uses
  16. uriparser;
  17. var
  18. URI: TURI;
  19. s: String;
  20. procedure TestParse;
  21. begin
  22. with URI do
  23. begin
  24. Protocol := 'http';
  25. Username := 'user';
  26. Password := 'pass';
  27. Host := 'localhost';
  28. Port := 8080;
  29. Path := '/test/dir';
  30. Document := 'some index.html';
  31. Params := 'param1=value1&param2=value2';
  32. Bookmark := 'bookmark';
  33. HasAuthority := True;
  34. end;
  35. s := EncodeURI(URI);
  36. WriteLn(s);
  37. Finalize(URI);
  38. FillChar(URI, SizeOf(URI), #0);
  39. Writeln;
  40. // URI := ParseURI(s, 'defaultprotocol', 1234);
  41. URI:=ParseURI('http://www.lazarus.freepascal.org/main.php');
  42. with URI do
  43. begin
  44. WriteLn('Protocol: ', Protocol);
  45. WriteLn('Username: ', Username);
  46. WriteLn('Password: ', Password);
  47. WriteLn('Host: ', Host);
  48. WriteLn('Port: ', Port);
  49. WriteLn('Path: ', Path);
  50. WriteLn('Document: ', Document);
  51. WriteLn('Params: ', Params);
  52. WriteLn('Bookmark: ', Bookmark);
  53. end;
  54. end;
  55. type
  56. urirec = record
  57. a, b: string
  58. end;
  59. const
  60. Base = 'http://a/b/c/d;p?q';
  61. tests: array[0..22] of urirec = (
  62. (a: 'g:h'; b: 'g:h'),
  63. (a: 'g'; b: 'http://a/b/c/g'),
  64. (a: './g'; b: 'http://a/b/c/g'),
  65. (a: 'g/'; b: 'http://a/b/c/g/'),
  66. (a: '/g'; b: 'http://a/g'),
  67. (a: '//g'; b: 'http://g'),
  68. (a: '?y'; b: 'http://a/b/c/d;p?y'),
  69. (a: 'g?y'; b: 'http://a/b/c/g?y'),
  70. (a: '#s'; b: 'http://a/b/c/d;p?q#s'),
  71. (a: 'g#s'; b: 'http://a/b/c/g#s'),
  72. (a: 'g?y#s'; b: 'http://a/b/c/g?y#s'),
  73. (a: ';x'; b: 'http://a/b/c/;x'),
  74. (a: 'g;x'; b: 'http://a/b/c/g;x'),
  75. (a: 'g;x?y#s'; b: 'http://a/b/c/g;x?y#s'),
  76. (a: ''; b: 'http://a/b/c/d;p?q'),
  77. (a: '.'; b: 'http://a/b/c/'),
  78. (a: './'; b: 'http://a/b/c/'),
  79. (a: '..'; b: 'http://a/b/'),
  80. (a: '../'; b: 'http://a/b/'),
  81. (a: '../g'; b: 'http://a/b/g'),
  82. (a: '../..'; b: 'http://a/'),
  83. (a: '../../'; b: 'http://a/'),
  84. (a: '../../g'; b: 'http://a/g')
  85. );
  86. tests1: array[0..1] of urirec = (
  87. (a: '../../../g'; b: 'http://a/g'),
  88. (a: '../../../../g'; b: 'http://a/g')
  89. );
  90. tests2: array[0..5] of urirec = (
  91. (a: '/./g'; b: 'http://a/g'),
  92. (a: '/../g'; b: 'http://a/g'),
  93. (a: 'g.'; b: 'http://a/b/c/g.'),
  94. (a: '.g'; b: 'http://a/b/c/.g'),
  95. (a: 'g..'; b: 'http://a/b/c/g..'),
  96. (a: '..g'; b: 'http://a/b/c/..g')
  97. );
  98. tests3: array[0..5] of urirec = (
  99. (a: './../g'; b: 'http://a/b/g'),
  100. (a: './g/.'; b: 'http://a/b/c/g/'),
  101. (a: 'g/./h'; b: 'http://a/b/c/g/h'),
  102. (a: 'g/../h'; b: 'http://a/b/c/h'),
  103. (a: 'g;x=1/./y'; b: 'http://a/b/c/g;x=1/y'),
  104. (a: 'g;x=1/../y'; b: 'http://a/b/c/y')
  105. );
  106. tests4: array[0..3] of urirec = (
  107. (a: 'g?y/./x'; b: 'http://a/b/c/g?y/./x'),
  108. (a: 'g?y/../x'; b: 'http://a/b/c/g?y/../x'),
  109. (a: 'g#s/./x'; b: 'http://a/b/c/g#s/./x'),
  110. (a: 'g#s/../x'; b: 'http://a/b/c/g#s/../x')
  111. );
  112. procedure Test(const Caption: string; const t: array of urirec);
  113. var
  114. rslt: UTF8String;
  115. i: Integer;
  116. Failed: Boolean;
  117. begin
  118. write(Caption, '...');
  119. Failed := False;
  120. for i := low(t) to high(t) do
  121. begin
  122. ResolveRelativeUri(Base, t[i].a, rslt);
  123. if rslt <> t[i].b then
  124. begin
  125. if not Failed then writeln;
  126. Failed := True;
  127. writeln('Test ', i, ' mismatch, expected: ''', t[i].b, '''; got: ''', rslt, '''');
  128. end;
  129. end;
  130. if not Failed then
  131. writeln(' OK');
  132. end;
  133. begin
  134. TestParse;
  135. Writeln;
  136. Writeln('Now testing relative URI resolving:');
  137. Test('Normal tests', tests);
  138. Test('URI authority is not changed by using dot segments', tests1);
  139. Test('Dot segments are removed only if they are complete path components', tests2);
  140. Test('Testing some nonsensical forms of URI', tests3);
  141. Test('Testing dot segments present in query or fragments', tests4);
  142. end.