Test_HTTPUploads.dpr 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. program Test_HTTPUploads;
  26. {$I Tests.inc}
  27. uses
  28. SysUtils,
  29. StrUtils,
  30. Platform,
  31. Marshalling,
  32. libsagui,
  33. BrookLibraryLoader,
  34. BrookUtility,
  35. BrookHTTPUploads,
  36. Test;
  37. type
  38. TFakeUploads = class(TBrookHTTPUploads)
  39. public
  40. Done: Boolean;
  41. end;
  42. TFakeUpload = class
  43. public
  44. ErrorCode: Integer;
  45. Path: string;
  46. end;
  47. var
  48. FakeHandle: Pointer;
  49. FakeUploads: TFakeUploads;
  50. FakeUpload: TFakeUpload;
  51. HTTPUploadsEnumeratorDone: Boolean = False;
  52. function fake_httpupld_handle(uplds: Psg_httpupld): Pcvoid; cdecl;
  53. begin
  54. Result := uplds;
  55. end;
  56. function fake_httpupld_dir(uplds: Psg_httpupld): Pcchar; cdecl;
  57. begin
  58. Assert(uplds = FakeHandle);
  59. Result := 'fake_dir';
  60. end;
  61. function fake_httpupld_field(uplds: Psg_httpupld): Pcchar; cdecl;
  62. begin
  63. Assert(uplds = FakeHandle);
  64. Result := 'fake_field';
  65. end;
  66. function fake_httpupld_name(uplds: Psg_httpupld): Pcchar; cdecl;
  67. begin
  68. Assert(uplds = FakeHandle);
  69. Result := 'fake_name';
  70. end;
  71. function fake_httpupld_mime(uplds: Psg_httpupld): Pcchar; cdecl;
  72. begin
  73. Assert(uplds = FakeHandle);
  74. Result := 'fake_mime';
  75. end;
  76. function fake_httpupld_encoding(uplds: Psg_httpupld): Pcchar; cdecl;
  77. begin
  78. Assert(uplds = FakeHandle);
  79. Result := 'fake_encoding';
  80. end;
  81. function fake_httpupld_size(uplds: Psg_httpupld): cuint64_t; cdecl;
  82. begin
  83. Assert(uplds = FakeHandle);
  84. Result := 123;
  85. end;
  86. function fake_httpupld_save(upld: Psg_httpupld; overwritten: cbool): cint; cdecl;
  87. begin
  88. Assert(upld = FakeHandle);
  89. Assert(overwritten);
  90. Result := TFakeUpload(upld).ErrorCode;
  91. end;
  92. function fake_httpupld_save_as(upld: Psg_httpupld; const path: Pcchar;
  93. overwritten: cbool): cint; cdecl;
  94. var
  95. U: TFakeUpload;
  96. begin
  97. U := TFakeUpload(upld);
  98. Assert(upld = FakeHandle);
  99. U.Path := TMarshal.ToString(path);
  100. Assert(overwritten);
  101. Result := U.ErrorCode;
  102. end;
  103. function fake_httpuplds_count(uplds: Psg_httpupld): cuint; cdecl;
  104. begin
  105. Assert(uplds = FakeHandle);
  106. Result := 3;
  107. end;
  108. function fake_httpuplds_next(upld: PPsg_httpupld): cint; cdecl;
  109. begin
  110. Assert(Assigned(upld));
  111. if HTTPUploadsEnumeratorDone then
  112. upld^ := nil;
  113. Result := 0;
  114. end;
  115. procedure AssignFakeAPI; {$IFNDEF DEBUG}inline;{$ENDIF}
  116. begin
  117. sg_httpupld_handle := fake_httpupld_handle;
  118. sg_httpupld_dir := fake_httpupld_dir;
  119. sg_httpupld_field := fake_httpupld_field;
  120. sg_httpupld_name := fake_httpupld_name;
  121. sg_httpupld_mime := fake_httpupld_mime;
  122. sg_httpupld_encoding := fake_httpupld_encoding;
  123. sg_httpupld_size := fake_httpupld_size;
  124. sg_httpupld_save := fake_httpupld_save;
  125. sg_httpupld_save_as := fake_httpupld_save_as;
  126. sg_httpuplds_count := fake_httpuplds_count;
  127. sg_httpuplds_next := fake_httpuplds_next;
  128. end;
  129. procedure DoHTTPCreateLibraryNotLoaded;
  130. begin
  131. TBrookHTTPUpload.Create(FakeUpload);
  132. end;
  133. procedure Test_HTTPUploadCreate;
  134. var
  135. U: TBrookHTTPUpload;
  136. begin
  137. FakeHandle := FakeUploads;
  138. U := TBrookHTTPUpload.Create(FakeUploads);
  139. Assert(U.Handle = FakeUploads);
  140. Assert(U.Directory = 'fake_dir');
  141. Assert(U.Field = 'fake_field');
  142. Assert(U.Name = 'fake_name');
  143. Assert(U.Mime = 'fake_mime');
  144. Assert(U.Encoding = 'fake_encoding');
  145. Assert(U.Size = 123);
  146. TBrookLibraryLoader.Unload;
  147. try
  148. AssertExcept(DoHTTPCreateLibraryNotLoaded, ESgLibNotLoaded,
  149. Format(SSgLibNotLoaded, [IfThen(SgLib.GetLastName = '',
  150. SG_LIB_NAME, SgLib.GetLastName)]));
  151. finally
  152. TBrookLibraryLoader.Load;
  153. AssignFakeAPI;
  154. end;
  155. end;
  156. procedure DoHTTPSaveInvalidArgument1(const AArgs: array of const);
  157. begin
  158. TBrookHTTPUpload(AArgs[0].VPointer^).Save(True);
  159. end;
  160. procedure DoHTTPSaveInvalidArgument2(const AArgs: array of const);
  161. begin
  162. TBrookHTTPUpload(AArgs[0].VPointer^).Save;
  163. end;
  164. procedure Test_HTTPUploadSave;
  165. var
  166. U: TBrookHTTPUpload;
  167. E: string;
  168. begin
  169. FakeHandle := FakeUpload;
  170. U := TBrookHTTPUpload.Create(FakeUpload);
  171. FakeUpload.ErrorCode := 0;
  172. E := '';
  173. Assert(U.Save(True, E));
  174. Assert(E = '');
  175. FakeUpload.ErrorCode := EINVAL;
  176. E := '';
  177. Assert(not U.Save(True, E));
  178. Assert(E = Sagui.StrError(EINVAL));
  179. FakeUpload.ErrorCode := 0;
  180. E := '';
  181. Assert(U.Save(E));
  182. Assert(E = '');
  183. FakeUpload.ErrorCode := EINVAL;
  184. Assert(not U.Save(E));
  185. Assert(E = Sagui.StrError(EINVAL));
  186. FakeUpload.ErrorCode := 0;
  187. E := '';
  188. U.Save(True);
  189. Assert(E = '');
  190. FakeUpload.ErrorCode := EINVAL;
  191. E := '';
  192. AssertOSExcept(DoHTTPSaveInvalidArgument1, EINVAL, [@U]);
  193. FakeUpload.ErrorCode := 0;
  194. E := '';
  195. U.Save;
  196. Assert(E = '');
  197. FakeUpload.ErrorCode := EINVAL;
  198. E := '';
  199. AssertOSExcept(DoHTTPSaveInvalidArgument2, EINVAL, [@U]);
  200. end;
  201. procedure DoHTTPSaveAsInvalidArgument1(const AArgs: array of const);
  202. begin
  203. TBrookHTTPUpload(AArgs[0].VPointer^).SaveAs('fake_path', True);
  204. end;
  205. procedure DoHTTPSaveAsInvalidArgument2(const AArgs: array of const);
  206. begin
  207. TBrookHTTPUpload(AArgs[0].VPointer^).SaveAs('fake_path');
  208. end;
  209. procedure Test_HTTPUploadSaveAs;
  210. var
  211. U: TBrookHTTPUpload;
  212. E: string;
  213. begin
  214. FakeHandle := FakeUpload;
  215. U := TBrookHTTPUpload.Create(FakeUpload);
  216. FakeUpload.ErrorCode := 0;
  217. FakeUpload.Path := '';
  218. E := '';
  219. Assert(U.SaveAs('fake_path', True, E));
  220. Assert(FakeUpload.Path = 'fake_path');
  221. Assert(E = '');
  222. FakeUpload.ErrorCode := EINVAL;
  223. FakeUpload.Path := '';
  224. E := '';
  225. Assert(not U.SaveAs('fake_path', True, E));
  226. Assert(FakeUpload.Path = 'fake_path');
  227. Assert(E = Sagui.StrError(EINVAL));
  228. FakeUpload.ErrorCode := 0;
  229. FakeUpload.Path := '';
  230. E := '';
  231. Assert(U.SaveAs('fake_path', E));
  232. Assert(FakeUpload.Path = 'fake_path');
  233. Assert(E = '');
  234. FakeUpload.ErrorCode := EINVAL;
  235. FakeUpload.Path := '';
  236. E := '';
  237. Assert(not U.SaveAs('fake_path', E));
  238. Assert(FakeUpload.Path = 'fake_path');
  239. Assert(E = Sagui.StrError(EINVAL));
  240. FakeUpload.ErrorCode := 0;
  241. FakeUpload.Path := '';
  242. E := '';
  243. U.SaveAs('fake_path', True);
  244. Assert(FakeUpload.Path = 'fake_path');
  245. Assert(E = '');
  246. FakeUpload.ErrorCode := EINVAL;
  247. FakeUpload.Path := '';
  248. E := '';
  249. AssertOSExcept(DoHTTPSaveAsInvalidArgument1, EINVAL, [@U]);
  250. FakeUpload.ErrorCode := 0;
  251. FakeUpload.Path := '';
  252. E := '';
  253. U.SaveAs('fake_path');
  254. Assert(FakeUpload.Path = 'fake_path');
  255. Assert(E = '');
  256. FakeUpload.ErrorCode := EINVAL;
  257. FakeUpload.Path := '';
  258. E := '';
  259. AssertOSExcept(DoHTTPSaveAsInvalidArgument2, EINVAL, [@U]);
  260. end;
  261. procedure Test_HTTPUploadHandle;
  262. begin
  263. Assert(TBrookHTTPUpload.Create(FakeUpload).Handle = FakeUpload);
  264. end;
  265. procedure Test_HTTPUploadStreamHandle;
  266. begin
  267. Assert(TBrookHTTPUpload.Create(FakeUpload).StreamHandle = FakeUpload);
  268. end;
  269. procedure Test_HTTPUploadDirectory;
  270. begin
  271. Assert(TBrookHTTPUpload.Create(FakeUpload).Directory = 'fake_dir');
  272. end;
  273. procedure Test_HTTPUploadField;
  274. begin
  275. Assert(TBrookHTTPUpload.Create(FakeUpload).Field = 'fake_field');
  276. end;
  277. procedure Test_HTTPUploadName;
  278. begin
  279. Assert(TBrookHTTPUpload.Create(FakeUpload).Name = 'fake_name');
  280. end;
  281. procedure Test_HTTPUploadMime;
  282. begin
  283. Assert(TBrookHTTPUpload.Create(FakeUpload).Mime = 'fake_mime');
  284. end;
  285. procedure Test_HTTPUploadEncoding;
  286. begin
  287. Assert(TBrookHTTPUpload.Create(FakeUpload).Encoding = 'fake_encoding');
  288. end;
  289. procedure Test_HTTPUploadSize;
  290. begin
  291. Assert(TBrookHTTPUpload.Create(FakeUpload).Size = 123);
  292. end;
  293. procedure Test_HTTPUploadsEnumerator;
  294. var
  295. UL: TFakeUploads;
  296. U: TBrookHTTPUpload;
  297. I: Integer;
  298. begin
  299. FakeHandle := FakeUploads;
  300. UL := TFakeUploads.Create(FakeUploads);
  301. try
  302. I := 0;
  303. for U in UL do
  304. begin
  305. if I = 3 then
  306. HTTPUploadsEnumeratorDone := True
  307. else
  308. Inc(I);
  309. end;
  310. Assert(I = 3);
  311. finally
  312. UL.Free;
  313. end;
  314. end;
  315. procedure Test_HTTPUploadsCreate;
  316. var
  317. UL: TBrookHTTPUploads;
  318. begin
  319. UL := TBrookHTTPUploads.Create(FakeUploads);
  320. try
  321. Assert(UL.Handle = FakeUploads);
  322. finally
  323. UL.Free;
  324. end;
  325. end;
  326. procedure Test_HTTPUploadsGetEnumerator;
  327. var
  328. UL: TBrookHTTPUploads;
  329. E: TBrookHTTPUploadsEnumerator;
  330. begin
  331. UL := TBrookHTTPUploads.Create(FakeUploads);
  332. try
  333. E := UL.GetEnumerator;
  334. Assert(Assigned(E) and (E is TBrookHTTPUploadsEnumerator));
  335. E.Destroy;
  336. finally
  337. UL.Free;
  338. end;
  339. end;
  340. procedure Test_HTTPUploadsFirst;
  341. var
  342. UL: TBrookHTTPUploads;
  343. U: TBrookHTTPUpload;
  344. begin
  345. UL := TBrookHTTPUploads.Create(FakeUploads);
  346. try
  347. U := UL.First;
  348. Assert(U.Handle = FakeUploads);
  349. finally
  350. UL.Free;
  351. end;
  352. end;
  353. procedure Test_HTTPUploadsNext;
  354. var
  355. UL: TBrookHTTPUploads;
  356. U: TBrookHTTPUpload;
  357. begin
  358. UL := TBrookHTTPUploads.Create(nil);
  359. try
  360. U := UL.Next;
  361. Assert(not Assigned(U.Handle));
  362. finally
  363. UL.Free;
  364. end;
  365. end;
  366. procedure Test_HTTPUploadsEOF;
  367. var
  368. UL: TBrookHTTPUploads;
  369. begin
  370. UL := TBrookHTTPUploads.Create(FakeUploads);
  371. try
  372. Assert(UL.EOF);
  373. finally
  374. UL.Free;
  375. end;
  376. end;
  377. procedure Test_HTTPUploadsCount;
  378. var
  379. UL: TBrookHTTPUploads;
  380. begin
  381. UL := TBrookHTTPUploads.Create(FakeUploads);
  382. try
  383. Assert(UL.Count = 3);
  384. finally
  385. UL.Free;
  386. end;
  387. end;
  388. begin
  389. {$IF (NOT DEFINED(FPC)) AND DEFINED(DEBUG)}
  390. ReportMemoryLeaksOnShutdown := True;
  391. {$ENDIF}
  392. TBrookLibraryLoader.Load;
  393. FakeUploads := TFakeUploads.Create(nil);
  394. FakeUpload := TFakeUpload.Create;
  395. try
  396. AssignFakeAPI;
  397. Test_HTTPUploadCreate;
  398. Test_HTTPUploadSave;
  399. Test_HTTPUploadSaveAs;
  400. Test_HTTPUploadHandle;
  401. Test_HTTPUploadStreamHandle;
  402. Test_HTTPUploadDirectory;
  403. Test_HTTPUploadField;
  404. Test_HTTPUploadName;
  405. Test_HTTPUploadMime;
  406. Test_HTTPUploadEncoding;
  407. Test_HTTPUploadSize;
  408. Test_HTTPUploadsEnumerator;
  409. Test_HTTPUploadsCreate;
  410. Test_HTTPUploadsGetEnumerator;
  411. Test_HTTPUploadsFirst;
  412. Test_HTTPUploadsNext;
  413. Test_HTTPUploadsEOF;
  414. Test_HTTPUploadsCount;
  415. finally
  416. FakeUploads.Free;
  417. FakeUpload.Free;
  418. TBrookLibraryLoader.Unload;
  419. end;
  420. end.