File: test_contextlocals.py

package info (click to toggle)
python-bottle 0.10.11-1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,332 kB
  • sloc: python: 4,605; makefile: 191
file content (45 lines) | stat: -rw-r--r-- 1,311 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
# -*- coding: utf-8 -*-
'''
Some objects are context-local, meaning that they have different values depending on the context they are accessed from. A context is currently defined as a thread.
'''

import unittest
import bottle
import threading


def run_thread(func):
    t = threading.Thread(target=func)
    t.start()
    t.join()

class TestThreadLocals(unittest.TestCase):
    def test_request(self):
        e1 = {'PATH_INFO': '/t1'}
        e2 = {'PATH_INFO': '/t2'}

        def run():
            bottle.request.bind(e2)
            self.assertEquals(bottle.request.path, '/t2')

        bottle.request.bind(e1)
        self.assertEquals(bottle.request.path, '/t1')
        run_thread(run)
        self.assertEquals(bottle.request.path, '/t1')

    def test_response(self):

        def run():
            bottle.response.bind()
            bottle.response.content_type='test/thread'
            self.assertEquals(bottle.response.headers['Content-Type'], 'test/thread')

        bottle.response.bind()
        bottle.response.content_type='test/main'
        self.assertEquals(bottle.response.headers['Content-Type'], 'test/main')
        run_thread(run)
        self.assertEquals(bottle.response.headers['Content-Type'], 'test/main')


if __name__ == '__main__': #pragma: no cover
    unittest.main()