wikipedia bot: encode urls correctly in http request & adjust tests.
This commit is contained in:
parent
8adf51890a
commit
f862cf2222
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"request": {
|
"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": {
|
"response": {
|
||||||
"data": {
|
"data": {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"request": {
|
"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": {
|
"response": {
|
||||||
"data": {
|
"data": {
|
||||||
|
|
|
@ -37,6 +37,15 @@ class TestWikipediaBot(BotTestCase):
|
||||||
expected_method='send_reply'
|
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
|
# Incorrect word
|
||||||
bot_response = "I am sorry. The search term you provided is not found :slightly_frowning_face:"
|
bot_response = "I am sorry. The search term you provided is not found :slightly_frowning_face:"
|
||||||
with self.mock_http_conversation('test_incorrect_query'):
|
with self.mock_http_conversation('test_incorrect_query'):
|
||||||
|
|
|
@ -3,6 +3,7 @@ from __future__ import print_function
|
||||||
import requests
|
import requests
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
from six.moves import urllib
|
||||||
|
|
||||||
# See readme.md for instructions on running this code.
|
# See readme.md for instructions on running this code.
|
||||||
|
|
||||||
|
@ -41,7 +42,8 @@ class WikipediaHandler(object):
|
||||||
if query == '':
|
if query == '':
|
||||||
return help_text
|
return help_text
|
||||||
query_wiki_link = ('https://en.wikipedia.org/w/api.php?action=query&'
|
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:
|
try:
|
||||||
data = requests.get(query_wiki_link)
|
data = requests.get(query_wiki_link)
|
||||||
except requests.exceptions.RequestException:
|
except requests.exceptions.RequestException:
|
||||||
|
|
Loading…
Reference in a new issue