IdTestHttpServer.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. unit IdTestHttpServer;
  2. interface
  3. uses
  4. IdGlobal,
  5. IdContext,
  6. IdHttp,
  7. IdIOHandlerStack,
  8. IdCustomHttpServer,
  9. IdHttpServer,
  10. IdLogDebug,
  11. IdServerInterceptLogFile,
  12. IdHttpHeaderInfo,
  13. IdObjs,
  14. IdSys,
  15. IdTcpClient,
  16. IdTest;
  17. type
  18. TIdTestHttpServer = class(TIdTest)
  19. private
  20. FException: Exception;
  21. procedure HandleSimpleGet(AContext:TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  22. procedure HandleException(AContext:TIdContext; AException: Exception);
  23. published
  24. procedure TestRange;
  25. procedure TestSimpleGet;
  26. end;
  27. implementation
  28. const
  29. cSmallRangeStart=5000;
  30. //arbitrary number larger than cardinal
  31. cLargeRangeStart=5000000000;
  32. cSimpleContent='hello';
  33. procedure TIdTestHttpServer.HandleException(AContext:TIdContext; AException: Exception);
  34. begin
  35. if FException = nil then
  36. begin
  37. FException := AException;
  38. end;
  39. end;
  40. procedure TIdTestHttpServer.HandleSimpleGet(AContext:TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  41. begin
  42. if ARequestInfo.Document='/smallrange' then
  43. begin
  44. Assert(ARequestInfo.ContentRangeStart=cSmallRangeStart);
  45. Assert(ARequestInfo.ContentRangeEnd=cSmallRangeStart+1);
  46. AResponseInfo.ContentText:='OK';
  47. end
  48. else if ARequestInfo.Document='/largerange' then
  49. begin
  50. Assert(ARequestInfo.ContentRangeStart=cLargeRangeStart);
  51. Assert(ARequestInfo.ContentRangeEnd=cLargeRangeStart+1);
  52. AResponseInfo.ContentText:='OK';
  53. end
  54. else
  55. begin
  56. AResponseInfo.ContentText := cSimpleContent;
  57. end;
  58. end;
  59. procedure TIdTestHttpServer.TestRange;
  60. var
  61. aServer:TIdHttpServer;
  62. aClient:TIdHttp;
  63. s:string;
  64. begin
  65. aServer:=TIdHTTPServer.Create;
  66. aClient:=TIdHTTP.Create;
  67. try
  68. aServer.DefaultPort:=22280;
  69. aServer.OnCommandGet:=Self.HandleSimpleGet;
  70. aServer.Active:=True;
  71. aClient.Request.ContentRangeStart:=cSmallRangeStart;
  72. aClient.Request.ContentRangeEnd:=cSmallRangeStart+1;
  73. s:=aClient.Get('http://127.0.0.1:22280/smallrange');
  74. Assert(s='OK',s);
  75. aClient.Request.ContentRangeStart:=cLargeRangeStart;
  76. aClient.Request.ContentRangeEnd:=cLargeRangeStart+1;
  77. s:=aClient.Get('http://127.0.0.1:22280/largerange');
  78. Assert(s='OK',s);
  79. finally
  80. Sys.FreeAndNil(aClient);
  81. Sys.FreeAndNil(aServer);
  82. end;
  83. end;
  84. procedure TIdTestHttpServer.TestSimpleGet;
  85. var
  86. HttpSrv: TIdHttpServer;
  87. HttpCli: TIdHttp;
  88. TcpCli: TIdTcpClient;
  89. TempString: string;
  90. aDebug:TIdLogDebug;
  91. aIntercept:TIdServerInterceptLogFile;
  92. begin
  93. HttpSrv := TIdHttpServer.Create;
  94. aIntercept := TIdServerInterceptLogFile.Create;
  95. try
  96. HttpSrv.OnCommandGet := HandleSimpleGet;
  97. HttpSrv.DefaultPort := 1234;
  98. HttpSrv.OnException := HandleException;
  99. HttpSrv.Intercept := aIntercept;
  100. aIntercept.FileName := '\indytest_httpserver.log';
  101. HttpSrv.Active := True;
  102. try
  103. aDebug := TIdLogDebug.Create;
  104. TcpCli := TIdTcpClient.Create;
  105. try
  106. TcpCli.Host := '127.0.0.1';
  107. TcpCli.Port := 1234;
  108. // TcpCli.ReadTimeout := 250;
  109. TcpCli.CreateIOHandler;
  110. TcpCli.IOHandler.Intercept := aDebug;
  111. TIdLogDebug(TcpCli.IOHandler.Intercept).Active := True;
  112. TcpCli.Connect;
  113. TcpCli.IOHandler.WriteLn('GET /index.html HTTP/1.1');
  114. TcpCli.IOHandler.WriteLn('Accept: */*');
  115. TcpCli.IOHandler.WriteLn('Accept-Language: en,nl;q=0.5');
  116. TcpCli.IOHandler.WriteLn('Accept-Encoding: gzip, deflate');
  117. TcpCli.IOHandler.WriteLn('User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 1.0.3705)');
  118. TcpCli.IOHandler.WriteLn('Host: 127.0.0.1');
  119. TcpCli.IOHandler.WriteLn('Connection: close');
  120. TcpCli.IOHandler.WriteLn('');
  121. if FException <> nil then
  122. raise FException;
  123. TempString := TcpCli.IOHandler.ReadLn('', -1);
  124. Assert(not TcpCli.IOHandler.ReadLnTimedOut, 'Timed out (1)');
  125. Assert(TempString = 'HTTP/1.1 200 OK', TempString);
  126. TcpCli.IOHandler.AllData;
  127. TcpCli.Disconnect;
  128. finally
  129. Sys.FreeAndNil(TcpCli);
  130. end;
  131. HttpCli := TIdHttp.Create;
  132. try
  133. HttpCli.ReadTimeout := 2500;
  134. TempString:=HttpCli.Get('http://127.0.0.1:1234/index.html');
  135. Assert(TempString = cSimpleContent);
  136. finally
  137. Sys.FreeAndNil(HttpCli);
  138. Sys.FreeAndNil(aDebug);
  139. end;
  140. finally
  141. HttpSrv.Active := False;
  142. end;
  143. finally
  144. Sys.FreeAndNil(HttpSrv);
  145. Sys.FreeAndNil(aIntercept);
  146. end;
  147. end;
  148. initialization
  149. TIdTest.RegisterTest(TIdTestHttpServer);
  150. end.