<commit_before>"""
Test WSGI basics and provide some helper functions for other WSGI tests.
"""

import falcon

from cal.tests import base
from cal import base as wsgi_base
from cal import wsgi


def _first_hook(req, resp, resource):
    if not req.client_accepts_json:
        raise falcon.HTTPNotAcceptable(
            'This API only supports responses encoded as JSON.',
            href='http://docs.examples.com/api/json')

    if req.method in ('POST', 'PUT'):
        if 'application/json' not in req.content_type:
            raise falcon.HTTPUnsupportedMediaType(
                'This API only supports requests encoded as JSON.',
                href='http://docs.examples.com/api/json')


def _second_hook(req, resp, resource):
    headers = req.headers
    methods = headers.get('URL-METHODS', '').split(',')

    if req.method not in methods:
        raise falcon.HTTPNotFound()


class TestController(object):

    def get(self, msg):
        return msg


class TestResource(wsgi_base.BaseSource):

    def __init__(self, controller):
        self.controller = controller

    def on_get(self, req, resp):
        resp.status = falcon.HTTP_200
        resp.body = self.controller.get('Success')


class TestWSGIDriver(wsgi.WSGIDriver):

    def __init__(self):
        super(TestWSGIDriver, self).__init__()

    def before_hooks(self):
        return [
            _first_hook,
            _second_hook,
        ]

    def _init_routes_and_middlewares(self):
        middleware = \
                [wsgi.FuncMiddleware(hook) for hook in self.before_hooks()]
        self.app = falcon.API(middleware=middleware)

        controller = TestController()
        self.app.add_route('/', TestResource(controller))


class Test(base.TestCase):

    def setUp(self):
        super(Test, self).setUp()
        self.wsgi_driver = TestWSGIDriver()
        self.api = self.wsgi_driver.app

    def test_first_hook_raise_HTTPNotAcceptable(self):<commit_msg>Fix pep8<commit_after>"""
Test WSGI basics and provide some helper functions for other WSGI tests.
"""

import falcon

from cal.tests import base
from cal import base as wsgi_base
from cal import wsgi


def _first_hook(req, resp, resource):
    if not req.client_accepts_json:
        raise falcon.HTTPNotAcceptable(
            'This API only supports responses encoded as JSON.',
            href='http://docs.examples.com/api/json')

    if req.method in ('POST', 'PUT'):
        if 'application/json' not in req.content_type:
            raise falcon.HTTPUnsupportedMediaType(
                'This API only supports requests encoded as JSON.',
                href='http://docs.examples.com/api/json')


def _second_hook(req, resp, resource):
    headers = req.headers
    methods = headers.get('URL-METHODS', '').split(',')

    if req.method not in methods:
        raise falcon.HTTPNotFound()


class TestController(object):

    def get(self, msg):
        return msg


class TestResource(wsgi_base.BaseSource):

    def __init__(self, controller):
        self.controller = controller

    def on_get(self, req, resp):
        resp.status = falcon.HTTP_200
        resp.body = self.controller.get('Success')


class TestWSGIDriver(wsgi.WSGIDriver):

    def __init__(self):
        super(TestWSGIDriver, self).__init__()

    def before_hooks(self):
        return [
            _first_hook,
            _second_hook,
        ]

    def _init_routes_and_middlewares(self):
        middleware = \
            [wsgi.FuncMiddleware(hook) for hook in self.before_hooks()]
        self.app = falcon.API(middleware=middleware)

        controller = TestController()
        self.app.add_route('/', TestResource(controller))


class Test(base.TestCase):

    def setUp(self):
        super(Test, self).setUp()
        self.wsgi_driver = TestWSGIDriver()
        self.api = self.wsgi_driver.app

    def test_first_hook_raise_HTTPNotAcceptable(self):
{'fraction_non_alphanumeric': 0.1037567084078712, 'fraction_numerical': 0.0017889087656529517, 'mean_word_length': 2.713472485768501, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2615373', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1157, 'n_words': 286}