test_http.py 3.88 KB
Newer Older
1 2 3 4 5 6 7
"""
Unit tests for stub HTTP server base class.
"""

import unittest
import requests
import json
8
from terrain.stubs.http import StubHttpService, StubHttpRequestHandler, require_params
9 10 11 12 13


class StubHttpServiceTest(unittest.TestCase):

    def setUp(self):
14
        super(StubHttpServiceTest, self).setUp()
15 16
        self.server = StubHttpService()
        self.addCleanup(self.server.shutdown)
17
        self.url = "http://127.0.0.1:{0}/set_config".format(self.server.port)
18 19 20 21 22 23 24 25

    def test_configure(self):
        """
        All HTTP stub servers have an end-point that allows
        clients to configure how the server responds.
        """
        params = {
            'test_str': 'This is only a test',
26
            'test_empty': '',
27 28
            'test_int': 12345,
            'test_float': 123.45,
29 30 31
            'test_dict': {
                'test_key': 'test_val',
            },
32
            'test_empty_dict': {},
33
            'test_unicode': u'\u2603 the snowman',
34 35
            'test_none': None,
            'test_boolean': False
36 37 38 39
        }

        for key, val in params.iteritems():

40 41 42
            # JSON-encode each parameter
            post_params = {key: json.dumps(val)}
            response = requests.put(self.url, data=post_params)
43 44 45 46
            self.assertEqual(response.status_code, 200)

        # Check that the expected values were set in the configuration
        for key, val in params.iteritems():
47
            self.assertEqual(self.server.config.get(key), val)
48 49

    def test_bad_json(self):
50 51 52 53 54 55 56 57 58 59
        response = requests.put(self.url, data="{,}")
        self.assertEqual(response.status_code, 400)

    def test_no_post_data(self):
        response = requests.put(self.url, data={})
        self.assertEqual(response.status_code, 200)

    def test_unicode_non_json(self):
        # Send unicode without json-encoding it
        response = requests.put(self.url, data={'test_unicode': u'\u2603 the snowman'})
60 61 62 63 64 65 66 67
        self.assertEqual(response.status_code, 400)

    def test_unknown_path(self):
        response = requests.put(
            "http://127.0.0.1:{0}/invalid_url".format(self.server.port),
            data="{}"
        )
        self.assertEqual(response.status_code, 404)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89


class RequireRequestHandler(StubHttpRequestHandler):
    @require_params('GET', 'test_param')
    def do_GET(self):
        self.send_response(200)

    @require_params('POST', 'test_param')
    def do_POST(self):
        self.send_response(200)


class RequireHttpService(StubHttpService):
    HANDLER_CLASS = RequireRequestHandler


class RequireParamTest(unittest.TestCase):
    """
    Test the decorator for requiring parameters.
    """

    def setUp(self):
90
        super(RequireParamTest, self).setUp()
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        self.server = RequireHttpService()
        self.addCleanup(self.server.shutdown)
        self.url = "http://127.0.0.1:{port}".format(port=self.server.port)

    def test_require_get_param(self):

        # Expect success when we provide the required param
        response = requests.get(self.url, params={"test_param": 2})
        self.assertEqual(response.status_code, 200)

        # Expect failure when we do not proivde the param
        response = requests.get(self.url)
        self.assertEqual(response.status_code, 400)

        # Expect failure when we provide an empty param
        response = requests.get(self.url + "?test_param=")
        self.assertEqual(response.status_code, 400)

    def test_require_post_param(self):

        # Expect success when we provide the required param
        response = requests.post(self.url, data={"test_param": 2})
        self.assertEqual(response.status_code, 200)

        # Expect failure when we do not proivde the param
        response = requests.post(self.url)
        self.assertEqual(response.status_code, 400)

        # Expect failure when we provide an empty param
        response = requests.post(self.url, data={"test_param": None})
        self.assertEqual(response.status_code, 400)