example_test.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import urllib
  2. assert "Hello World!" == urllib.urlopen('http://localhost:18080').read()
  3. assert "About Crow example." == urllib.urlopen('http://localhost:18080/about').read()
  4. assert 404 == urllib.urlopen('http://localhost:18080/list').getcode()
  5. assert "3 bottles of beer!" == urllib.urlopen('http://localhost:18080/hello/3').read()
  6. assert "100 bottles of beer!" == urllib.urlopen('http://localhost:18080/hello/100').read()
  7. assert 400 == urllib.urlopen('http://localhost:18080/hello/500').getcode()
  8. assert "3" == urllib.urlopen('http://localhost:18080/add_json', data='{"a":1,"b":2}').read()
  9. assert "3" == urllib.urlopen('http://localhost:18080/add/1/2').read()
  10. # test persistent connection
  11. import socket
  12. import time
  13. s = socket.socket()
  14. s.connect(('localhost', 18080))
  15. for i in xrange(10):
  16. s.send('''GET / HTTP/1.1
  17. Host: localhost\r\n\r\n''');
  18. assert 'Hello World!' in s.recv(1024)
  19. # test large
  20. s = socket.socket()
  21. s.connect(('localhost', 18080))
  22. s.send('''GET /large HTTP/1.1
  23. Host: localhost\r\nConnection: close\r\n\r\n''')
  24. r = ''
  25. while True:
  26. d = s.recv(1024*1024)
  27. if not d:
  28. break;
  29. r += d
  30. print len(r), len(d)
  31. print len(r), r[:100]
  32. assert len(r) > 512*1024
  33. # test timeout
  34. s = socket.socket()
  35. s.connect(('localhost', 18080))
  36. # invalid request, connection will be closed after timeout
  37. s.send('''GET / HTTP/1.1
  38. hHhHHefhwjkefhklwejfklwejf
  39. ''')
  40. print s.recv(1024)