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

This commit is contained in:
neiljp (Neil Pilgrim) 2017-09-18 21:12:56 -07:00 committed by Tim Abbott
parent 8adf51890a
commit f862cf2222
5 changed files with 36 additions and 3 deletions

View file

@ -0,0 +1,22 @@
{
"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"
}
}

View file

@ -1,6 +1,6 @@
{
"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": {

View file

@ -1,6 +1,6 @@
{
"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": {

View file

@ -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'):

View file

@ -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: