xref: /unit/test/python/iter_exception/wsgi.py (revision 1848:4bd548074e2c)
1class application:
2    def __init__(self, environ, start_response):
3        self.environ = environ
4        self.start = start_response
5
6        self.next = self.__next__
7
8    def __iter__(self):
9        self.__i = 0
10        self._skip_level = int(self.environ.get('HTTP_X_SKIP', 0))
11        self._not_skip_close = int(
12            self.environ.get('HTTP_X_NOT_SKIP_CLOSE', 0)
13        )
14        self._is_chunked = self.environ.get('HTTP_X_CHUNKED')
15
16        headers = [(('Content-Length', '10'))]
17        if self._is_chunked is not None:
18            headers = []
19
20        if self._skip_level < 1:
21            raise Exception('first exception')
22
23        write = self.start('200', headers)
24
25        if self._skip_level < 2:
26            raise Exception('second exception')
27
28        write(b'XXXXX')
29
30        if self._skip_level < 3:
31            raise Exception('third exception')
32
33        return self
34
35    def __next__(self):
36        if self._skip_level < 4:
37            raise Exception('next exception')
38
39        self.__i += 1
40        if self.__i > 2:
41            raise StopIteration
42
43        return b'X'
44
45    def close(self):
46        if self._not_skip_close == 1:
47            raise Exception('close exception')
48