Skip to content

Commit

Permalink
encode URL path parts to avoid issues with funky titles
Browse files Browse the repository at this point in the history
See e.g. user '?renommé201306261613' on frwiki.
  • Loading branch information
bfontaine committed May 15, 2020
1 parent 30218b8 commit ac6a6db
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions tests/test_base.py
Expand Up @@ -27,6 +27,7 @@ def test_build_path(self):

("/x/2000-01-01/a", "/{a}/{start}/{b}", [("a", "", "x"), ("start", "", ""), ("b", "a", "x")]),
("/x/2050-12-31/a", "/{a}/{end}/{b}", [("a", "", "x"), ("end", "", ""), ("b", "a", "x")]),
("/x/%3F", "/{a}/{b}", [("a", "", "x"), ("b", "?", "")]),
):
self.assertEqual(expected, base.build_path(path_format, path_params), path_format)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_page.py
Expand Up @@ -20,7 +20,7 @@ def test_simple_info(self):
self.assertEqual(response,
fn("en.wikipedia.org", "Albert_Einstein"))

m.get(prefix + "/en.wikipedia.org/i don't exist lol",
m.get(prefix + "/en.wikipedia.org/i%20don%27t%20exist%20lol",
json={"error": "No matching results found for: i don't exist lol"},
status_code=404)
self.assertRaises(exceptions.NotFound,
Expand Down
10 changes: 9 additions & 1 deletion tests/test_user.py
Expand Up @@ -52,4 +52,12 @@ def test_pages_created_non_existing_user(self):
self.assertRaises(exceptions.NotFound,
lambda: user.pages_created("project1", "foo"))
self.assertRaises(exceptions.NotFound,
lambda: next(user.pages_created_iter("project1", "foo")))
lambda: next(user.pages_created_iter("project1", "foo")))

def test_pages_created_funky_username(self):
with requests_mock.Mocker() as m:
m.get("m://x/user/pages/project1/%3Fhello", json={"pages": {}})
self.assertEqual({"pages": []},
user.pages_created("project1", "?hello"))
self.assertEqual([],
list(user.pages_created_iter("project1", "?hello")))
3 changes: 2 additions & 1 deletion xtools/base.py
Expand Up @@ -4,6 +4,7 @@

from typing import Optional, Tuple, Any, Sequence
from json.decoder import JSONDecodeError
from urllib.parse import quote as urlquote

import time
import requests
Expand Down Expand Up @@ -122,7 +123,7 @@ def has_more(index):
elif name == "end":
param_value = END_TIME

params_dict[name] = param_value
params_dict[name] = urlquote(param_value)

path = path_format.format(**params_dict).rstrip("/")
return path
11 changes: 9 additions & 2 deletions xtools/page.py
Expand Up @@ -11,7 +11,11 @@


def _get_page_dict(what: str, project: str, article: str) -> dict:
return base.get("/page/%s/%s/%s" % (what, project, article))
return base.get(base.build_path("/page/{what}/{project}/{article}", (
("what", what, ""),
("project", project, ""),
("article", article, ""),
)))


def article_info(project: str, article: str) -> dict:
Expand Down Expand Up @@ -171,4 +175,7 @@ def assessments(project: str, articles: Sequence[str],
if class_only:
params["classonly"] = "1"

return base.get("/page/assessments/%s/%s" % (project, "|".join(articles)), params)
return base.get(base.build_path("/page/assessments/{project}/{articles}", (
("project", project, ""),
("articles", "|".join(articles), ""),
)), params)

0 comments on commit ac6a6db

Please sign in to comment.