File: base_test.py

package info (click to toggle)
python-flask-cors 3.0.10-2%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 548 kB
  • sloc: python: 1,791; makefile: 138; sh: 17
file content (86 lines) | stat: -rw-r--r-- 2,905 bytes parent folder | download | duplicates (5)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
"""
    test
    ~~~~
    Flask-CORS is a simple extension to Flask allowing you to support cross
    origin resource sharing (CORS) using a simple decorator.

    :copyright: (c) 2016 by Cory Dolphin.
    :license: MIT, see LICENSE for more details.
"""
from flask import Flask
import unittest

from flask_cors import *
from flask_cors.core import *


class FlaskCorsTestCase(unittest.TestCase):
    def shortDescription(self):
        """
        Get's the one liner description to be displayed.
        Source:
        http://erikzaadi.com/2012/09/13/inheritance-within-python-unit-tests/
        """
        doc = self.id()[self.id().rfind('.')+1:]
        return "{}.{}".format(self.__class__.__name__, doc)

    def iter_verbs(self, c):
        ''' A simple helper method to iterate through a range of
            HTTP Verbs and return the test_client bound instance,
            keeping writing our tests as DRY as possible.
        '''
        for verb in ['get', 'head', 'options']:
            yield getattr(c, verb)

    def iter_responses(self, path, verbs=['get', 'head', 'options'], **kwargs):
        for verb in verbs:
            yield self._request(verb.lower(), path, **kwargs)

    def _request(self, verb, *args, **kwargs):
        _origin = kwargs.pop('origin', None)
        headers = kwargs.pop('headers', {})
        if _origin:
            headers.update(Origin=_origin)

        with self.app.test_client() as c:
            return getattr(c, verb)(*args, headers=headers, **kwargs)

    def get(self, *args, **kwargs):
        return self._request('get', *args, **kwargs)

    def head(self, *args, **kwargs):
        return self._request('head', *args, **kwargs)

    def post(self, *args, **kwargs):
        return self._request('post', *args, **kwargs)

    def options(self, *args, **kwargs):
        return self._request('options', *args, **kwargs)

    def put(self, *args, **kwargs):
        return self._request('put', *args, **kwargs)

    def patch(self, *args, **kwargs):
        return self._request('patch', *args, **kwargs)

    def delete(self, *args, **kwargs):
        return self._request('delete', *args, **kwargs)

    def preflight(self, path, method='GET', cors_request_headers=None, json=True, **kwargs):
        kwargs['headers'] = kwargs.get('headers', {})

        if cors_request_headers:
            kwargs['headers'].update({'Access-Control-Request-Headers': ', '.join(cors_request_headers)})
        if json:
            kwargs['headers'].update({'Content-Type':'application/json'})

        kwargs['headers'].update({'Access-Control-Request-Method': method})

        return self.options(path,**kwargs)

    def assertHasACLOrigin(self, resp, origin=None):
        if origin is None:
            self.assertTrue(ACL_ORIGIN in resp.headers)
        else:
            self.assertTrue(resp.headers.get(ACL_ORIGIN) == origin)