Skip to content

Commit 79dce16

Browse files
author
Mike Kistler
committed
Test updates for path variable encoding
1 parent 163668e commit 79dce16

2 files changed

Lines changed: 65 additions & 3 deletions

File tree

test/test_conversation_v1.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def test_get_counterexample():
140140
service = watson_developer_cloud.ConversationV1(
141141
username='username', password='password', version='2017-02-03')
142142
counterexample = service.get_counterexample(
143-
workspace_id='boguswid', text='What are you wearing%3F')
143+
workspace_id='boguswid', text='What are you wearing?')
144144
assert len(responses.calls) == 1
145145
assert responses.calls[0].request.url.startswith(url)
146146
assert counterexample == response
@@ -203,8 +203,8 @@ def test_update_counterexample():
203203
username='username', password='password', version='2017-02-03')
204204
counterexample = service.update_counterexample(
205205
workspace_id='boguswid',
206-
text='What are you wearing%3F',
207-
new_text='What are you wearing%3F')
206+
text='What are you wearing?',
207+
new_text='What are you wearing?')
208208
assert len(responses.calls) == 1
209209
assert responses.calls[0].request.url.startswith(url)
210210
assert counterexample == response

test/test_watson_service.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# coding=utf-8
2+
import json
3+
4+
import watson_developer_cloud
5+
from watson_developer_cloud import WatsonService
6+
7+
import responses
8+
9+
##############################################################################
10+
# Service
11+
##############################################################################
12+
13+
class AnyServiceV1(WatsonService):
14+
default_url = 'https://gateway.watsonplatform.net/test/api'
15+
16+
def __init__(self, version, url=default_url, username=None, password=None):
17+
WatsonService.__init__(
18+
self,
19+
vcap_services_name='test',
20+
url=url,
21+
username=username,
22+
password=password,
23+
use_vcap_services=True)
24+
self.version = version
25+
26+
def op_with_path_params(self, path0, path1):
27+
if path0 is None:
28+
raise ValueError('path0 must be provided')
29+
if path1 is None:
30+
raise ValueError('path1 must be provided')
31+
params = {'version': self.version}
32+
url = '/v1/foo/{0}/bar/{1}/baz'.format(
33+
*self._encode_path_vars(path0, path1))
34+
self.request(method='GET', url=url, params=params, accept_json=True)
35+
return None
36+
37+
38+
@responses.activate
39+
def test_url_encoding():
40+
service = AnyServiceV1('2017-07-07', username='username', password='password')
41+
42+
# All characters in path0 _must_ be encoded in path segments
43+
path0 = ' \"<>^`{}|/\\?#%[]'
44+
path0_encoded = '%20%22%3C%3E%5E%60%7B%7D%7C%2F%5C%3F%23%25%5B%5D'
45+
# All non-ASCII chars _must_ be encoded in path segments
46+
path1 = u'比萨浇头'.encode('utf8') # "pizza toppings"
47+
path1_encoded = '%E6%AF%94%E8%90%A8%E6%B5%87%E5%A4%B4'
48+
49+
path_encoded = '/v1/foo/' + path0_encoded + '/bar/' + path1_encoded + '/baz'
50+
test_url = service.default_url + path_encoded
51+
52+
responses.add(responses.GET,
53+
test_url,
54+
status = 200,
55+
body = json.dumps({"foobar": "baz"}),
56+
content_type = 'application/json')
57+
58+
response = service.op_with_path_params(path0, path1)
59+
60+
assert len(responses.calls) == 1
61+
assert path_encoded in responses.calls[0].request.url
62+
assert 'version=2017-07-07' in responses.calls[0].request.url

0 commit comments

Comments
 (0)