linkchecker/tests/test_cgi.py

81 lines
2.8 KiB
Python
Raw Permalink Normal View History

2012-04-22 18:45:24 +00:00
# Copyright (C) 2004-2012 Bastian Kleineidam
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
2009-07-24 21:58:20 +00:00
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Test cgi form routines.
"""
2020-05-14 19:15:28 +00:00
import urllib.parse
from io import BytesIO
from wsgiref.util import setup_testing_defaults
from linkcheck.lc_cgi import checkform, checklink, LCFormError, application
2012-04-22 10:48:21 +00:00
from linkcheck.strformat import limit
from . import TestBase
2020-05-28 19:29:13 +00:00
class TestWsgi(TestBase):
"""Test wsgi application."""
def test_form_valid_url(self):
2011-03-12 08:02:29 +00:00
# Check url validity.
env = dict()
form = dict(url="http://www.example.com/", level="1")
checkform(form, env)
def test_form_empty_url(self):
2011-03-12 08:02:29 +00:00
# Check with empty url.
env = dict()
form = dict(url="", level="0")
self.assertRaises(LCFormError, checkform, form, env)
def test_form_default_url(self):
2011-03-12 08:02:29 +00:00
# Check with default url.
env = dict()
form = dict(url="http://", level="0")
self.assertRaises(LCFormError, checkform, form, env)
def test_form_invalid_url(self):
2011-03-12 08:02:29 +00:00
# Check url (in)validity.
env = dict()
form = dict(url="http://www.foo bar/", level="0")
self.assertRaises(LCFormError, checkform, form, env)
2011-03-12 08:02:29 +00:00
def test_checklink(self):
form = dict(url="http://www.example.com/", level="0")
checklink(form, {})
def test_application(self):
form = dict(url="http://www.example.com/", level="0")
2020-05-14 19:15:28 +00:00
formdata = urllib.parse.urlencode(form)
2020-05-28 19:29:13 +00:00
formdata = formdata.encode("ascii")
environ = {"wsgi.input": BytesIO(formdata)}
setup_testing_defaults(environ)
test_response = b""
test_headers = [None]
test_status = [None]
2020-05-28 19:29:13 +00:00
def start_response(status, headers):
test_status[0] = status
test_headers[0] = headers
2020-05-28 19:29:13 +00:00
2012-04-22 10:48:21 +00:00
for str_data in application(environ, start_response):
if not isinstance(str_data, bytes):
2012-04-22 10:48:21 +00:00
err = "answer is not a byte string: %r" % limit(str_data, 30)
self.assertTrue(False, err)
test_response += str_data
2020-05-28 19:29:13 +00:00
self.assertEqual(test_status[0], "200 OK")
self.assertTrue(b"Generated by LinkChecker" in test_response)
self.assertFalse(b"internal error" in test_response)