wikipedia bot: encode urls correctly in http request & adjust tests.
ADD
zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_hash_query.json
+{
+ "request": {
+ "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=%23&format=json"
+ },
+ "response": {
+ "data": {
+ "status_code":200
+ },
+ "query": {
+ "search": [
+ {
+ "title":"Number sign"
+ }
+ ]
+ }
+ },
+ "response-headers":{
+ "status":200,
+ "ok":true,
+ "content-type":"application/json; charset=utf-8"
+ }
+}

MODIFY
zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json
zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json
{
"request": {
- "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=sssssss kkkkk&format=json"
+ "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=sssssss%20kkkkk&format=json"
},
"response": {
"data": {

MODIFY
zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json
zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json
{
"request": {
- "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=The sky is blue&format=json"
+ "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=The%20sky%20is%20blue&format=json"
},
"response": {
"data": {

MODIFY
zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py
zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py
@@ -37,6 +37,15 @@ class TestWikipediaBot(BotTestCase):
expected_method='send_reply'
)
+ # Hash query
+ bot_response = "For search term \"#\", https://en.wikipedia.org/wiki/Number_sign"
+ with self.mock_http_conversation('test_hash_query'):
+ self.assert_bot_response(
+ message = {'content': '#'},
+ response = {'content': bot_response},
+ expected_method='send_reply'
+ )
+
# Incorrect word
bot_response = "I am sorry. The search term you provided is not found :slightly_frowning_face:"
with self.mock_http_conversation('test_incorrect_query'):

MODIFY
zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py
zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py
@@ -3,6 +3,7 @@ from __future__ import print_function
import requests
import logging
import re
+from six.moves import urllib
# See readme.md for instructions on running this code.
@@ -41,7 +42,8 @@ class WikipediaHandler(object):
if query == '':
return help_text
query_wiki_link = ('https://en.wikipedia.org/w/api.php?action=query&'
- 'list=search&srsearch=%s&format=json' % (query,))
+ 'list=search&srsearch=%s&format=json'
+ % (urllib.parse.quote(query),))
try:
data = requests.get(query_wiki_link)
except requests.exceptions.RequestException:

wikipedia bot: encode urls correctly in http request & adjust tests.