summaryrefslogtreecommitdiff
path: root/webtest
diff options
context:
space:
mode:
authorGael Pasgrimaud <gael@gawel.org>2016-07-21 10:26:21 +0200
committerGael Pasgrimaud <gael@gawel.org>2016-07-21 10:30:24 +0200
commit9ca03a04927744fbea1b6a3fd209623981204bf1 (patch)
treeee2d508dc7baf4ce8e301bb455439b86f2ffa4c3 /webtest
parent2358ed021a244c1a4400b6e353c15876e13b7660 (diff)
downloadwebtest-9ca03a04927744fbea1b6a3fd209623981204bf1.tar.gz
do not guess encoding if response's charset is set. See #160
Diffstat (limited to 'webtest')
-rw-r--r--webtest/forms.py6
-rw-r--r--webtest/response.py10
2 files changed, 12 insertions, 4 deletions
diff --git a/webtest/forms.py b/webtest/forms.py
index 9ff0a17..16f5637 100644
--- a/webtest/forms.py
+++ b/webtest/forms.py
@@ -400,7 +400,11 @@ class Form(object):
def __init__(self, response, text, parser_features='html.parser'):
self.response = response
self.text = text
- self.html = BeautifulSoup(self.text, parser_features)
+ if response and response.charset:
+ self.html = BeautifulSoup(self.text, parser_features,
+ from_encoding=response.charset)
+ else:
+ self.html = BeautifulSoup(self.text, parser_features)
attrs = self.html('form')[0].attrs
self.action = attrs.get('action', '')
diff --git a/webtest/response.py b/webtest/response.py
index 3952079..186ac47 100644
--- a/webtest/response.py
+++ b/webtest/response.py
@@ -359,7 +359,7 @@ class TestResponse(webob.Response):
raise TypeError(
"The only keyword argument allowed is 'no'")
for s in strings:
- if not s in self:
+ if s not in self:
print_stderr("Actual response (no %r):" % s)
print_stderr(str(self))
raise IndexError(
@@ -426,7 +426,11 @@ class TestResponse(webob.Response):
raise AttributeError(
"Not an HTML response body (content-type: %s)"
% self.content_type)
- soup = BeautifulSoup(self.testbody, self.parser_features)
+ if self.charset:
+ soup = BeautifulSoup(self.testbody, self.parser_features,
+ from_encoding=self.charset)
+ else:
+ soup = BeautifulSoup(self.testbody, self.parser_features)
return soup
@property
@@ -484,7 +488,7 @@ class TestResponse(webob.Response):
from lxml.html import fromstring
except ImportError: # pragma: no cover
fromstring = etree.HTML
- ## FIXME: would be nice to set xml:base, in some fashion
+ # FIXME: would be nice to set xml:base, in some fashion
if self.content_type == 'text/html':
return fromstring(self.testbody, base_url=self.request.url)
else: