cubicweb/wsgi/test/unittest_wsgi.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 09 Nov 2016 16:14:17 +0100
changeset 11888 0849a5eb57b8
parent 11057 0b59724cb3f2
child 11913 4516c3956d46
permissions -rw-r--r--
[rtags] Allow to 'derive' rtags Since some releases, rtags (structure underlying uicfg) have selector and may be copied using something like: new_rtags = deepcopy(original_rtags) new_rtags.__module__ = __name__ new_rtags.__select__ = custom_selector The problem is that starting from that, both rtags wil diverge and changes in original_rtags won't be considered, while we usually want to set a few specific rules only in new_rtags. To fix this problem, this cset introduces the notion of "derivated/parent" rtag, eg: new_rtags = original_rtags.derive(__name__, custom_selector) Beside easier copying, when using the above method changes in original_rtags which are not overriden by new_rtags will be considered since it only hold its specific rules but look among its parent chain for non-found keys. Along the way, flake8 unittest_rtags. Closes #16164880

# encoding=utf-8

import webtest.app
from io import BytesIO

from cubicweb.devtools.webtest import CubicWebTestTC

from cubicweb.wsgi.request import CubicWebWsgiRequest
from cubicweb.multipart import MultipartError


class WSGIAppTC(CubicWebTestTC):
    def test_content_type(self):
        r = webtest.app.TestRequest.blank('/', {'CONTENT_TYPE': 'text/plain'})

        req = CubicWebWsgiRequest(r.environ, self.vreg)

        self.assertEqual('text/plain', req.get_header('Content-Type'))

    def test_content_body(self):
        r = webtest.app.TestRequest.blank('/', {
            'CONTENT_LENGTH': 12,
            'CONTENT_TYPE': 'text/plain',
            'wsgi.input': BytesIO(b'some content')})

        req = CubicWebWsgiRequest(r.environ, self.vreg)

        self.assertEqual(b'some content', req.content.read())

    def test_http_scheme(self):
        r = webtest.app.TestRequest.blank('/', {
            'wsgi.url_scheme': 'http'})

        req = CubicWebWsgiRequest(r.environ, self.vreg)

        self.assertFalse(req.https)

    def test_https_scheme(self):
        r = webtest.app.TestRequest.blank('/', {
            'wsgi.url_scheme': 'https'})

        req = CubicWebWsgiRequest(r.environ, self.vreg)

        self.assertTrue(req.https)

    def test_https_prefix(self):
        r = webtest.app.TestRequest.blank('/https/', {
            'wsgi.url_scheme': 'http'})

        req = CubicWebWsgiRequest(r.environ, self.vreg)

        self.assertTrue(req.https)

    def test_big_content(self):
        content = b'x'*100001
        r = webtest.app.TestRequest.blank('/', {
            'CONTENT_LENGTH': len(content),
            'CONTENT_TYPE': 'text/plain',
            'wsgi.input': BytesIO(content)})

        req = CubicWebWsgiRequest(r.environ, self.vreg)

        self.assertEqual(content, req.content.read())

    def test_post(self):
        self.webapp.post(
            '/',
            params={'__login': self.admlogin, '__password': self.admpassword})

    def test_post_bad_form(self):
        with self.assertRaises(MultipartError):
            self.webapp.post(
                '/',
                params='badcontent',
                headers={'Content-Type': 'multipart/form-data'})

    def test_post_non_form(self):
        self.webapp.post(
            '/',
            params='{}',
            headers={'Content-Type': 'application/json'})

    def test_get_multiple_variables(self):
        r = webtest.app.TestRequest.blank('/?arg=1&arg=2')
        req = CubicWebWsgiRequest(r.environ, self.vreg)

        self.assertEqual([u'1', u'2'], req.form['arg'])

    def test_post_multiple_variables(self):
        r = webtest.app.TestRequest.blank('/', POST='arg=1&arg=2')
        req = CubicWebWsgiRequest(r.environ, self.vreg)

        self.assertEqual([u'1', u'2'], req.form['arg'])

    def test_post_files(self):
        content_type, params = self.webapp.encode_multipart(
            (), (('filefield', 'aname', b'acontent'),))
        r = webtest.app.TestRequest.blank(
            '/', POST=params, content_type=content_type)
        req = CubicWebWsgiRequest(r.environ, self.vreg)
        self.assertIn('filefield', req.form)
        fieldvalue = req.form['filefield']
        self.assertEqual(u'aname', fieldvalue[0])
        self.assertEqual(b'acontent', fieldvalue[1].read())

    def test_post_unicode_urlencoded(self):
        params = 'arg=%C3%A9'
        r = webtest.app.TestRequest.blank(
            '/', POST=params, content_type='application/x-www-form-urlencoded')
        req = CubicWebWsgiRequest(r.environ, self.vreg)
        self.assertEqual(u"é", req.form['arg'])


if __name__ == '__main__':
    import unittest
    unittest.main()