Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pactman/mock/pact_request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import os.path
import urllib.parse
import re

from ..verifier.parse_header import get_header_param
from ..verifier.result import Result
Expand Down Expand Up @@ -89,10 +90,12 @@ def respond_for_interaction(self, reason):
def handle_response_encoding(self, response, headers):
# default to content-type to json
# rfc4627 states JSON is Unicode and defaults to UTF-8
json_type = re.compile(r"application/(json|.*\+json|json\-.*)(;|$)")

content_type = [headers[h] for h in headers if h.lower() == "content-type"]
if content_type:
content_type = content_type[0]
if "application/json" not in content_type:
if not json_type.match(content_type):
return response["body"]
charset = get_header_param(content_type, "charset")
if not charset:
Expand Down
16 changes: 16 additions & 0 deletions pactman/test/test_mock_urlopen.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ def test_urlopen_responder_handles_json_body():
assert r.headers["Content-Type"] == "application/json; charset=UTF-8"


def test_urlopen_responder_handles_json_like_body():
h = MockURLOpenHandler(Mock())

interaction = dict(
response=dict(
body={"message": "hello world"},
status=200,
headers={"Content-Type":"application/geo+json"}
)
)
r = h.respond_for_interaction(interaction)

assert r.data == b'{"message": "hello world"}'
assert r.headers["Content-Type"] == "application/geo+json"


def test_urlopen_responder_handles_json_string_body():
h = MockURLOpenHandler(Mock())

Expand Down