Skip to content

Commit fba1df2

Browse files
surenderrajuKoleS46
authored andcommitted
Fixed get response body decode error for py2
1 parent 0e90d8d commit fba1df2

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

src/HttpLibrary/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import absolute_import
22
from future import standard_library
33
standard_library.install_aliases()
4-
from builtins import object
4+
from builtins import object, str
55
from robot.api import logger
66

77
from base64 import b64encode
@@ -486,8 +486,14 @@ def get_response_body(self):
486486
| Should Start With | ${body} | <?xml version="1.0" encoding="UTF-8" |
487487
"""
488488
response_body = self.response.body
489+
490+
# In python2 response comes as str, which satisfies below bytes condition too and fails at decode call.
491+
if isinstance(response_body, str):
492+
return response_body
493+
489494
if isinstance(response_body, bytes):
490-
response_body = response_body.decode()
495+
response_body = response_body.decode('utf-8', 'replace')
496+
491497
return response_body
492498

493499
def response_body_should_contain(self, should_contain):
@@ -499,12 +505,13 @@ def response_body_should_contain(self, should_contain):
499505
| Response Body Should Contain | version="1.0" |
500506
| Response Body Should Contain | encoding="UTF-8" |
501507
"""
502-
logger.debug('Testing whether "%s" contains "%s".' % (
503-
self.response.body, should_contain))
504508

505509
response = self.response.body
506510
if isinstance(response, bytes):
507-
response = response.decode("utf-8")
511+
response = response.decode("utf-8", "replace")
512+
513+
logger.debug('Testing whether "%s" contains "%s".' % (response, should_contain))
514+
508515
assert should_contain in response, \
509516
'"%s" should have contained "%s", but did not.' % (
510517
self.response.body, should_contain)

src/HttpLibrary/livetest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@
4141
__author__ = '[email protected]'
4242
__version__ = '0.5'
4343

44-
import sys, os
44+
import sys
4545
import webtest
4646
import ssl
4747
import http.client
4848
import urllib.parse
4949
from http.cookies import BaseCookie, CookieError
5050
from six.moves import http_cookiejar
51-
from robot.api import logger
5251

5352

5453
conn_classes = {'http': http.client.HTTPConnection,

tests/http/simple.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,24 @@ POST with one word request body
168168
POST /echo
169169
Response Body Should Contain Tooot
170170

171+
POST with extended ascii chars request body
172+
Set Request Body ToootÄß
173+
Set Request Header Content-Type text/plain
174+
POST /echo
175+
Response Body Should Contain ToootÄß
176+
171177
PUT with two word request body
172178
Set Request Body Tooot Tooooot
173179
Set Request Header Content-Type text/plain
174180
PUT /echo
175181
Response Body Should Contain Tooot Tooooot
176182

183+
PUT with two word extended ascii chars request body
184+
Set Request Body ToootÖÜ ToooootÄß
185+
Set Request Header Content-Type text/plain
186+
PUT /echo
187+
Response Body Should Contain ToootÖÜ ToooootÄß
188+
177189
Get Response Status
178190
GET /200
179191
${status}= Get Response Status

tox.ini

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22
envlist = py27, py36
33

44
[testenv]
5-
whitelist_externals = make
6-
7-
[testenv:py36]
8-
commands=
9-
make buildout-development-py3
10-
make run-robotframework
11-
12-
[testenv:py27]
13-
commands=
14-
make buildout-development-py2
15-
make run-robotframework
5+
commands =
6+
{envbindir}/python bootstrap.py
7+
{envbindir}/buildout
8+
{toxinidir}/bin/robotframework tests
9+
deps =
10+
zc.buildout
11+
skip_install = true

0 commit comments

Comments
 (0)