2012-04-22 18:45:24 +00:00
|
|
|
# Copyright (C) 2004-2012 Bastian Kleineidam
|
2004-08-16 19:20:06 +00:00
|
|
|
#
|
|
|
|
|
# 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.
|
2005-01-19 15:08:02 +00:00
|
|
|
"""
|
|
|
|
|
Test cgi form routines.
|
|
|
|
|
"""
|
2020-05-14 19:15:28 +00:00
|
|
|
import urllib.parse
|
2018-01-05 16:31:07 +00:00
|
|
|
from io import BytesIO
|
2012-04-18 19:52:36 +00:00
|
|
|
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
|
2005-12-17 19:22:44 +00:00
|
|
|
|
2022-10-17 18:26:53 +00:00
|
|
|
from . import TestBase
|
2020-05-28 19:29:13 +00:00
|
|
|
|
2022-10-17 18:26:53 +00:00
|
|
|
|
|
|
|
|
class TestWsgi(TestBase):
|
2012-04-18 19:52:36 +00:00
|
|
|
"""Test wsgi application."""
|
2004-08-16 19:20:06 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def test_form_valid_url(self):
|
2011-03-12 08:02:29 +00:00
|
|
|
# Check url validity.
|
2012-04-22 15:45:47 +00:00
|
|
|
env = dict()
|
2012-04-18 19:52:36 +00:00
|
|
|
form = dict(url="http://www.example.com/", level="1")
|
2012-04-22 15:45:47 +00:00
|
|
|
checkform(form, env)
|
2004-08-16 19:20:06 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def test_form_empty_url(self):
|
2011-03-12 08:02:29 +00:00
|
|
|
# Check with empty url.
|
2012-04-22 15:45:47 +00:00
|
|
|
env = dict()
|
2012-04-18 19:52:36 +00:00
|
|
|
form = dict(url="", level="0")
|
2012-04-22 15:45:47 +00:00
|
|
|
self.assertRaises(LCFormError, checkform, form, env)
|
2004-08-16 19:20:06 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def test_form_default_url(self):
|
2011-03-12 08:02:29 +00:00
|
|
|
# Check with default url.
|
2012-04-22 15:45:47 +00:00
|
|
|
env = dict()
|
2012-04-18 19:52:36 +00:00
|
|
|
form = dict(url="http://", level="0")
|
2012-04-22 15:45:47 +00:00
|
|
|
self.assertRaises(LCFormError, checkform, form, env)
|
2004-08-16 19:20:06 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def test_form_invalid_url(self):
|
2011-03-12 08:02:29 +00:00
|
|
|
# Check url (in)validity.
|
2012-04-22 15:45:47 +00:00
|
|
|
env = dict()
|
2012-04-18 19:52:36 +00:00
|
|
|
form = dict(url="http://www.foo bar/", level="0")
|
2012-04-22 15:45:47 +00:00
|
|
|
self.assertRaises(LCFormError, checkform, form, env)
|
2011-03-12 08:02:29 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def test_checklink(self):
|
2012-04-18 19:52:36 +00:00
|
|
|
form = dict(url="http://www.example.com/", level="0")
|
2020-08-05 18:54:56 +00:00
|
|
|
checklink(form, {})
|
2012-04-18 19:52:36 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def test_application(self):
|
2012-04-18 19:52:36 +00:00
|
|
|
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)}
|
2012-04-18 19:52:36 +00:00
|
|
|
setup_testing_defaults(environ)
|
2016-12-04 00:49:23 +00:00
|
|
|
test_response = b""
|
2012-04-18 19:52:36 +00:00
|
|
|
test_headers = [None]
|
|
|
|
|
test_status = [None]
|
2020-05-28 19:29:13 +00:00
|
|
|
|
2012-04-18 19:52:36 +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):
|
2016-12-04 00:49:23 +00:00
|
|
|
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")
|
2016-12-04 00:49:23 +00:00
|
|
|
self.assertTrue(b"Generated by LinkChecker" in test_response)
|
2021-01-28 19:20:24 +00:00
|
|
|
self.assertFalse(b"internal error" in test_response)
|