-
Notifications
You must be signed in to change notification settings - Fork 983
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #440 from dstufft/redirects
Redirect the old /pypi/<name>/<version>/ URLs to /project/
- Loading branch information
Showing
6 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pretend | ||
|
||
from pyramid.httpexceptions import HTTPMovedPermanently | ||
|
||
from warehouse import redirects | ||
|
||
|
||
def test_redirect_view(): | ||
target = "/{wat}/{_request.method}" | ||
view = redirects.redirect_view_factory(target) | ||
|
||
request = pretend.stub(method="GET", matchdict={"wat": "the-thing"}) | ||
resp = view(request) | ||
|
||
assert isinstance(resp, HTTPMovedPermanently) | ||
assert resp.headers["Location"] == "/the-thing/GET" | ||
|
||
|
||
def test_add_redirect(monkeypatch): | ||
rview = pretend.stub() | ||
rview_factory = pretend.call_recorder(lambda target, redirect: rview) | ||
monkeypatch.setattr(redirects, "redirect_view_factory", rview_factory) | ||
|
||
config = pretend.stub( | ||
add_route=pretend.call_recorder(lambda name, route: None), | ||
add_view=pretend.call_recorder(lambda view, route_name: None), | ||
) | ||
|
||
source = "/the/{thing}/" | ||
target = "/other/{thing}/" | ||
redirect = pretend.stub() | ||
|
||
redirects.add_redirect(config, source, target, redirect=redirect) | ||
|
||
assert config.add_route.calls == [ | ||
pretend.call("warehouse.redirects." + source, source), | ||
] | ||
assert config.add_view.calls == [ | ||
pretend.call(rview, route_name="warehouse.redirects." + source), | ||
] | ||
assert rview_factory.calls == [pretend.call(target, redirect=redirect)] | ||
|
||
|
||
def test_includeme(): | ||
config = pretend.stub( | ||
add_directive=pretend.call_recorder(lambda n, fn, action_wrap: None), | ||
) | ||
redirects.includeme(config) | ||
assert config.add_directive.calls == [ | ||
pretend.call( | ||
"add_redirect", | ||
redirects.add_redirect, | ||
action_wrap=False, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from pyramid.httpexceptions import HTTPMovedPermanently | ||
|
||
|
||
def redirect_view_factory(target, redirect=HTTPMovedPermanently): | ||
def redirect_view(request): | ||
return redirect(target.format(_request=request, **request.matchdict)) | ||
return redirect_view | ||
|
||
|
||
def add_redirect(config, source, target, **kw): | ||
route_name = "warehouse.redirects." + source | ||
|
||
config.add_route(route_name, source) | ||
config.add_view(redirect_view_factory(target, **kw), route_name=route_name) | ||
|
||
|
||
def includeme(config): | ||
config.add_directive("add_redirect", add_redirect, action_wrap=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters