2
0

HttpClientBase.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Authors:
  2. // Rafael Mizrahi <[email protected]>
  3. // Erez Lotan <[email protected]>
  4. // Oren Gurfinkel <[email protected]>
  5. // Ofer Borstein
  6. //
  7. // Copyright (c) 2004 Mainsoft Co.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Net;
  31. using GHTUtils;
  32. using GHTUtils.Base;
  33. namespace CHttpClient
  34. {
  35. /// <summary>
  36. /// Summary description for HttpClientBase.
  37. /// </summary>
  38. public class HttpClientBase : GHTBase
  39. {
  40. protected string _testingUrl = "";
  41. protected string _proxyAddress = "";
  42. public HttpClientBase()
  43. {
  44. _testingUrl = "http://localhost/httpapp/";
  45. _proxyAddress = "localhost";
  46. }
  47. protected string TCUrl(string page)
  48. {
  49. return _testingUrl + page;
  50. }
  51. //===========================================================
  52. // HTTP request/response routines
  53. //===========================================================
  54. protected HttpStatusCode HttpRequestStatusCode(string url)
  55. {
  56. return HttpRequestStatusCode(CreateRequest(url));
  57. }
  58. protected HttpStatusCode HttpRequestStatusCode(HttpWebRequest _request)
  59. {
  60. System.Net.HttpWebResponse _response;
  61. _response = (HttpWebResponse)_request.GetResponse();
  62. HttpStatusCode c = _response.StatusCode;
  63. _response.Close();
  64. return c;
  65. }
  66. protected string HttpRequestString(string url)
  67. {
  68. return HttpRequestString(CreateRequest(url));
  69. }
  70. protected string HttpRequestString(HttpWebRequest _request)
  71. {
  72. System.Net.HttpWebResponse _response;
  73. _response = (HttpWebResponse)_request.GetResponse();
  74. Stream s = _response.GetResponseStream();
  75. TextReader r = new StreamReader(s);
  76. string str = r.ReadToEnd();
  77. _response.Close();
  78. return str;
  79. }
  80. protected HttpWebRequest CreateRequest(string url)
  81. {
  82. return (HttpWebRequest)System.Net.WebRequest.Create(url);
  83. }
  84. //===========================================================
  85. // Upload/Download Data utilities
  86. //===========================================================
  87. protected bool ValidateFile(string filename)
  88. {
  89. FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
  90. byte [] buffer = new byte[fs.Length];
  91. fs.Read(buffer, 0, buffer.Length);
  92. fs.Close();
  93. return ValidateData(buffer);
  94. }
  95. protected bool ValidateData(byte [] sdata)
  96. {
  97. for (int i=0; i < sdata.Length; i++)
  98. {
  99. if (sdata[i] != (byte)(i % 256))
  100. return false;
  101. }
  102. return true;
  103. }
  104. protected byte [] GenerateData(int size)
  105. {
  106. byte [] sdata = new byte[size];
  107. for (int i=0; i < sdata.Length; i++)
  108. {
  109. sdata[i] = (byte)(i % 256);
  110. }
  111. return sdata;
  112. }
  113. //===========================================================
  114. // Test case execution routines
  115. //===========================================================
  116. protected delegate bool TestCaseDelegate();
  117. protected void ExecuteTestCase(string name, TestCaseDelegate f)
  118. {
  119. Exception exp = null;
  120. //sub test start
  121. try
  122. {
  123. BeginCase(name);
  124. Compare( f(), true );
  125. }
  126. catch(Exception ex){exp = ex;}
  127. finally{EndCase(exp); exp = null;}
  128. //sub test end
  129. }
  130. }
  131. }