Skip to content

Commit

Permalink
Merge pull request #106 from sigmavirus24/bug/43
Browse files Browse the repository at this point in the history
Fix query matcher handling unicode
  • Loading branch information
sigmavirus24 committed Apr 24, 2016
2 parents 68da87d + 8dfc33c commit 08bd50f
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions betamax/matchers/query.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# -*- coding: utf-8 -*-
import sys

from .base import BaseMatcher
from requests.compat import urlparse

try:
from urlparse import parse_qs
from urlparse import parse_qs, urlparse
except ImportError:
from urllib.parse import parse_qs
from urllib.parse import parse_qs, urlparse


isPY2 = (2, 6) <= sys.version_info < (3, 0)


class QueryMatcher(BaseMatcher):
Expand All @@ -17,8 +21,14 @@ def to_dict(self, query):
return parse_qs(query or '') # Protect against None

def match(self, request, recorded_request):
request_query = self.to_dict(urlparse(request.url).query)
recorded_query = self.to_dict(
urlparse(recorded_request['uri']).query
)
return request_query == recorded_query
request_query_dict = self.to_dict(urlparse(request.url).query)
recorded_query = urlparse(recorded_request['uri']).query
if recorded_query and isPY2:
# NOTE(sigmavirus24): If we're on Python 2, the request.url will
# be str/bytes and the recorded_request['uri'] will be unicode.
# For the comparison to work for high unicode characters, we need
# to encode the recorded query string before parsing it. See also
# GitHub bug #43.
recorded_query = recorded_query.encode('utf-8')
recorded_query_dict = self.to_dict(recorded_query)
return request_query_dict == recorded_query_dict

0 comments on commit 08bd50f

Please sign in to comment.