1from misc.colour_terminal import print_green
2from server.http.http_server import HTTPd, HTTPSd
3from test.base_test import BaseTest, HTTP, HTTPS
4
5
6class HTTPTest(BaseTest):
7
8    """ Class for HTTP Tests. """
9
10    # Temp Notes: It is expected that when pre-hook functions are executed,
11    # only an empty test-dir exists. pre-hook functions are executed just prior
12    # to the call to Wget is made. post-hook functions will be executed
13    # immediately after the call to Wget returns.
14
15    def __init__(self,
16                 pre_hook=None,
17                 test_params=None,
18                 post_hook=None,
19                 protocols=(HTTP,),
20                 req_protocols=None):
21        super(HTTPTest, self).__init__(pre_hook,
22                                       test_params,
23                                       post_hook,
24                                       protocols,
25                                       req_protocols)
26
27    def setup(self):
28        self.server_setup()
29        self.ready = True
30
31    def begin(self):
32        if not self.ready:
33            # this is to maintain compatibility with scripts that
34            # don't call setup()
35            self.setup()
36        with self:
37            # If any exception occurs, self.__exit__ will be immediately called.
38            # We must call the parent method in the end in order to verify
39            # whether the tests succeeded or not.
40            if self.ready:
41                self.do_test()
42                print_green("Test Passed.")
43            else:
44                self.tests_passed = False
45            return super(HTTPTest, self).begin()
46
47    def instantiate_server_by(self, protocol):
48        server = {HTTP: HTTPd,
49                  HTTPS: HTTPSd}[protocol]()
50        server.start()
51
52        return server
53
54    def request_remaining(self):
55        return [s.server_inst.get_req_headers()
56                for s in self.servers]
57
58    def stop_server(self):
59        for server in self.servers:
60            server.server_inst.shutdown()
61# vim: set ts=4 sts=4 sw=4 tw=80 et :
62