File: test__server_http.py

package info (click to toggle)
python-gevent 0.13.6-1%2Bnmu3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,324 kB
  • sloc: python: 13,296; makefile: 95; ansic: 37
file content (63 lines) | stat: -rw-r--r-- 1,748 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import unittest
import gevent
from gevent import http
import test__server
from test__server import *

internal_error_start = 'HTTP/1.0 500 Internal Server Error\n'.replace('\n', '\r\n')
internal_error_end = '\n\nInternal Server Error'.replace('\n', '\r\n')

internal_error503 = '''HTTP/1.0 503 Service Unavailable
Connection: close
Content-type: text/plain
Content-length: 31

Service Temporarily Unavailable'''.replace('\n', '\r\n')


class SimpleHTTPServer(http.HTTPServer):

    def handle(self, request):
        if request.uri == '/ping':
            request.send_reply(200, "OK", "PONG")
        elif request.uri == '/short':
            gevent.sleep(0.1)
            request.send_reply(200, "OK", 'hello')
        elif request.uri == '/long':
            gevent.sleep(10)
            request.send_reply(200, "OK", 'hello')
        else:
            request.send_reply(404, "gevent.http", "")


class Settings:
    ServerClass = http.HTTPServer
    ServerSubClass = SimpleHTTPServer
    restartable = False
    close_socket_detected = False

    @staticmethod
    def assert500(self):
        conn = self.makefile()
        conn.write('GET / HTTP/1.0\r\n\r\n')
        result = conn.read()
        assert result.startswith(internal_error_start), (result, internal_error_start)
        assert result.endswith(internal_error_end), (result, internal_error_end)

    assertAcceptedConnectionError = assert500

    @staticmethod
    def assert503(self):
        conn = self.makefile()
        conn.write('GET / HTTP/1.0\r\n\r\n')
        result = conn.read()
        assert result == internal_error503, (result, internal_error503)

    assertPoolFull = assert503


test__server.Settings = Settings


if __name__ == '__main__':
    unittest.main()