/restkit-4.1.2/tests/004-test-client.py

# · Python · 257 lines · 182 code · 50 blank · 25 comment · 2 complexity · 62a950c97dde5cf1a3059763a197c436 MD5 · raw file

  1. # -*- coding: utf-8 -
  2. #
  3. # This file is part of restkit released under the MIT license.
  4. # See the NOTICE for more information.
  5. from __future__ import with_statement
  6. import cgi
  7. import imghdr
  8. import os
  9. import socket
  10. import threading
  11. import Queue
  12. import urlparse
  13. import sys
  14. import tempfile
  15. import time
  16. import t
  17. from restkit.filters import BasicAuth
  18. LONG_BODY_PART = """This is a relatively long body, that we send to the client...
  19. This is a relatively long body, that we send to the client...
  20. This is a relatively long body, that we send to the client...
  21. This is a relatively long body, that we send to the client...
  22. This is a relatively long body, that we send to the client...
  23. This is a relatively long body, that we send to the client...
  24. This is a relatively long body, that we send to the client...
  25. This is a relatively long body, that we send to the client...
  26. This is a relatively long body, that we send to the client...
  27. This is a relatively long body, that we send to the client...
  28. This is a relatively long body, that we send to the client...
  29. This is a relatively long body, that we send to the client...
  30. This is a relatively long body, that we send to the client...
  31. This is a relatively long body, that we send to the client...
  32. This is a relatively long body, that we send to the client...
  33. This is a relatively long body, that we send to the client...
  34. This is a relatively long body, that we send to the client...
  35. This is a relatively long body, that we send to the client...
  36. This is a relatively long body, that we send to the client...
  37. This is a relatively long body, that we send to the client...
  38. This is a relatively long body, that we send to the client...
  39. This is a relatively long body, that we send to the client...
  40. This is a relatively long body, that we send to the client...
  41. This is a relatively long body, that we send to the client...
  42. This is a relatively long body, that we send to the client...
  43. This is a relatively long body, that we send to the client...
  44. This is a relatively long body, that we send to the client...
  45. This is a relatively long body, that we send to the client...
  46. This is a relatively long body, that we send to the client...
  47. This is a relatively long body, that we send to the client...
  48. This is a relatively long body, that we send to the client...
  49. This is a relatively long body, that we send to the client...
  50. This is a relatively long body, that we send to the client...
  51. This is a relatively long body, that we send to the client...
  52. This is a relatively long body, that we send to the client...
  53. This is a relatively long body, that we send to the client...
  54. This is a relatively long body, that we send to the client...
  55. This is a relatively long body, that we send to the client...
  56. This is a relatively long body, that we send to the client...
  57. This is a relatively long body, that we send to the client...
  58. This is a relatively long body, that we send to the client...
  59. This is a relatively long body, that we send to the client...
  60. This is a relatively long body, that we send to the client...
  61. This is a relatively long body, that we send to the client...
  62. This is a relatively long body, that we send to the client...
  63. This is a relatively long body, that we send to the client...
  64. This is a relatively long body, that we send to the client...
  65. This is a relatively long body, that we send to the client...
  66. This is a relatively long body, that we send to the client...
  67. This is a relatively long body, that we send to the client...
  68. This is a relatively long body, that we send to the client...
  69. This is a relatively long body, that we send to the client...
  70. This is a relatively long body, that we send to the client...
  71. This is a relatively long body, that we send to the client...
  72. This is a relatively long body, that we send to the client..."""
  73. @t.client_request("/")
  74. def test_001(u, c):
  75. r = c.request(u)
  76. t.eq(r.body_string(), "welcome")
  77. @t.client_request("/unicode")
  78. def test_002(u, c):
  79. r = c.request(u)
  80. t.eq(r.body_string(charset="utf-8"), u"éàù@")
  81. @t.client_request("/éàù")
  82. def test_003(u, c):
  83. r = c.request(u)
  84. t.eq(r.body_string(), "ok")
  85. t.eq(r.status_int, 200)
  86. @t.client_request("/json")
  87. def test_004(u, c):
  88. r = c.request(u, headers={'Content-Type': 'application/json'})
  89. t.eq(r.status_int, 200)
  90. r = c.request(u, headers={'Content-Type': 'text/plain'})
  91. t.eq(r.status_int, 400)
  92. @t.client_request('/unkown')
  93. def test_005(u, c):
  94. r = c.request(u, headers={'Content-Type': 'application/json'})
  95. t.eq(r.status_int, 404)
  96. @t.client_request('/query?test=testing')
  97. def test_006(u, c):
  98. r = c.request(u)
  99. t.eq(r.status_int, 200)
  100. t.eq(r.body_string(), "ok")
  101. #@t.client_request('http://e-engura.com/images/logo.gif')
  102. #def test_007(u, c):
  103. # r = c.request(u)
  104. # print r.status
  105. # t.eq(r.status_int, 200)
  106. # fd, fname = tempfile.mkstemp(suffix='.gif')
  107. # f = os.fdopen(fd, "wb")
  108. # f.write(r.body_string())
  109. # f.close()
  110. # t.eq(imghdr.what(fname), 'gif')
  111. #@t.client_request('http://e-engura.com/images/logo.gif')
  112. #def test_008(u, c):
  113. # r = c.request(u)
  114. # t.eq(r.status_int, 200)
  115. # fd, fname = tempfile.mkstemp(suffix='.gif')
  116. # f = os.fdopen(fd, "wb")
  117. # with r.body_stream() as body:
  118. # for block in body:
  119. # f.write(block)
  120. # f.close()
  121. # t.eq(imghdr.what(fname), 'gif')
  122. @t.client_request('/redirect')
  123. def test_009(u, c):
  124. c.follow_redirect = True
  125. r = c.request(u)
  126. complete_url = "%s/complete_redirect" % u.rsplit("/", 1)[0]
  127. t.eq(r.status_int, 200)
  128. t.eq(r.body_string(), "ok")
  129. t.eq(r.final_url, complete_url)
  130. @t.client_request('/')
  131. def test_010(u, c):
  132. r = c.request(u, 'POST', body="test")
  133. t.eq(r.body_string(), "test")
  134. @t.client_request('/bytestring')
  135. def test_011(u, c):
  136. r = c.request(u, 'POST', body="éàù@")
  137. t.eq(r.body_string(), "éàù@")
  138. @t.client_request('/unicode')
  139. def test_012(u, c):
  140. r = c.request(u, 'POST', body=u"éàù@")
  141. t.eq(r.body_string(), "éàù@")
  142. @t.client_request('/json')
  143. def test_013(u, c):
  144. r = c.request(u, 'POST', body="test",
  145. headers={'Content-Type': 'application/json'})
  146. t.eq(r.status_int, 200)
  147. r = c.request(u, 'POST', body="test",
  148. headers={'Content-Type': 'text/plain'})
  149. t.eq(r.status_int, 400)
  150. @t.client_request('/empty')
  151. def test_014(u, c):
  152. r = c.request(u, 'POST', body="",
  153. headers={'Content-Type': 'application/json'})
  154. t.eq(r.status_int, 200)
  155. r = c.request(u, 'POST', body="",
  156. headers={'Content-Type': 'application/json'})
  157. t.eq(r.status_int, 200)
  158. @t.client_request('/query?test=testing')
  159. def test_015(u, c):
  160. r = c.request(u, 'POST', body="",
  161. headers={'Content-Type': 'application/json'})
  162. t.eq(r.status_int, 200)
  163. @t.client_request('/1M')
  164. def test_016(u, c):
  165. fn = os.path.join(os.path.dirname(__file__), "1M")
  166. with open(fn, "rb") as f:
  167. l = int(os.fstat(f.fileno())[6])
  168. r = c.request(u, 'POST', body=f)
  169. t.eq(r.status_int, 200)
  170. t.eq(int(r.body_string()), l)
  171. @t.client_request('/large')
  172. def test_017(u, c):
  173. r = c.request(u, 'POST', body=LONG_BODY_PART)
  174. t.eq(r.status_int, 200)
  175. t.eq(int(r['content-length']), len(LONG_BODY_PART))
  176. t.eq(r.body_string(), LONG_BODY_PART)
  177. def test_0018():
  178. for i in range(10):
  179. t.client_request('/large')(test_017)
  180. @t.client_request('/')
  181. def test_019(u, c):
  182. r = c.request(u, 'PUT', body="test")
  183. t.eq(r.body_string(), "test")
  184. @t.client_request('/auth')
  185. def test_020(u, c):
  186. c.filters = [BasicAuth("test", "test")]
  187. c.load_filters()
  188. r = c.request(u)
  189. t.eq(r.status_int, 200)
  190. c.filters = [BasicAuth("test", "test2")]
  191. c.load_filters()
  192. r = c.request(u)
  193. t.eq(r.status_int, 403)
  194. @t.client_request('/list')
  195. def test_021(u, c):
  196. lines = ["line 1\n", " line2\n"]
  197. r = c.request(u, 'POST', body=lines,
  198. headers=[("Content-Length", "14")])
  199. t.eq(r.status_int, 200)
  200. t.eq(r.body_string(), 'line 1\n line2\n')
  201. @t.client_request('/chunked')
  202. def test_022(u, c):
  203. lines = ["line 1\n", " line2\n"]
  204. r = c.request(u, 'POST', body=lines,
  205. headers=[("Transfer-Encoding", "chunked")])
  206. t.eq(r.status_int, 200)
  207. t.eq(r.body_string(), '7\r\nline 1\n\r\n7\r\n line2\n\r\n0\r\n\r\n')