Skip to content

Commit fe7c3e0

Browse files
committed
django driver now handles redirections
1 parent 0d62f96 commit fe7c3e0

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

splinter/driver/djangoclient.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,17 @@ def _post_load(self):
8888
pass
8989
self.status_code = StatusCode(self._response.status_code, '')
9090

91+
def _handle_redirect_chain(self):
92+
if self._response.redirect_chain:
93+
for redirect_url, redirect_code in self._response.redirect_chain:
94+
self._last_urls.append(redirect_url)
95+
self._url = self._last_urls[-1]
96+
9197
def visit(self, url):
9298
self._url = url
9399
self._response = self._browser.get(url, follow=True)
94100
self._last_urls.append(url)
101+
self._handle_redirect_chain()
95102
self._post_load()
96103

97104
def submit(self, form):
@@ -109,6 +116,7 @@ def submit(self, form):
109116
if getattr(input, 'type', '') == 'file' and key in data:
110117
data[key] = open(data[key], 'rb')
111118
self._response = func_method(url, data, follow=True)
119+
self._handle_redirect_chain()
112120
self._post_load()
113121
return self._response
114122

tests/fake_django/urls.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import sys
22

33
from django.conf.urls import patterns, include, url
4+
from django.core.urlresolvers import reverse
45
from django.http import HttpResponse
56

7+
from django.shortcuts import redirect
68
from django.contrib import admin
79
from django.contrib.auth.decorators import login_required
810
admin.autodiscover()
911

1012
sys.path.append('tests')
11-
from fake_webapp import EXAMPLE_HTML, EXAMPLE_IFRAME_HTML, EXAMPLE_ALERT_HTML, EXAMPLE_TYPE_HTML, EXAMPLE_NO_BODY_HTML, EXAMPLE_POPUP_HTML
13+
from fake_webapp import (
14+
EXAMPLE_HTML,
15+
EXAMPLE_IFRAME_HTML,
16+
EXAMPLE_ALERT_HTML,
17+
EXAMPLE_TYPE_HTML,
18+
EXAMPLE_NO_BODY_HTML,
19+
EXAMPLE_POPUP_HTML,
20+
EXAMPLE_REDIRECT_LOCATION_HTML
21+
)
1222

1323

1424
def index(request):
@@ -69,6 +79,15 @@ def auth_required(request):
6979
return HttpResponse("Success!")
7080

7181

82+
def redirected(request):
83+
location = '{}?{}'.format(reverse('redirect_location'), 'come=get&some=true')
84+
return redirect(location)
85+
86+
87+
def redirect_location(request):
88+
return HttpResponse(EXAMPLE_REDIRECT_LOCATION_HTML)
89+
90+
7291
urlpatterns = patterns(
7392
'',
7493
url(r'^$', index),
@@ -83,5 +102,7 @@ def auth_required(request):
83102
url(r'^query$', query_string),
84103
url(r'^popup$', popup),
85104
url(r'^authenticate$', auth_required),
105+
url(r'^redirected', redirected),
106+
url(r'^redirect-location', redirect_location, name='redirect_location'),
86107
url(r'^admin/', include(admin.site.urls)),
87108
)

tests/fake_webapp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def read_static(static_name):
2222
EXAMPLE_TYPE_HTML = read_static('type.html')
2323
EXAMPLE_POPUP_HTML = read_static('popup.html')
2424
EXAMPLE_NO_BODY_HTML = read_static('no-body.html')
25+
EXAMPLE_REDIRECT_LOCATION_HTML = read_static('redirect-location.html')
2526

2627
# Functions for http basic auth.
2728
# Taken verbatim from http://flask.pocoo.org/snippets/8/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE HTML>
2+
3+
<html>
4+
<head>
5+
<title>Redirect Location</title>
6+
</head>
7+
<body>
8+
<h1>I just been redirected to this location.</h1>
9+
</body>
10+
</html>

tests/test_djangoclient.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import sys
99
import unittest
10+
import urlparse
1011

1112
sys.path.append('tests/fake_django')
1213
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
@@ -20,7 +21,9 @@ class DjangoClientDriverTest(BaseBrowserTests, unittest.TestCase):
2021

2122
@classmethod
2223
def setUpClass(cls):
23-
cls.browser = Browser('django', wait_time=0.1)
24+
components = urlparse.urlparse(EXAMPLE_APP)
25+
cls.browser = Browser('django', wait_time=0.1, client_SERVER_NAME=components.hostname,
26+
client_SERVER_PORT=components.port)
2427

2528
def setUp(self):
2629
self.browser.visit(EXAMPLE_APP)
@@ -124,3 +127,12 @@ def test_finding_all_links_by_non_ascii_text(self):
124127
for key, text in non_ascii_encodings.items():
125128
link = self.browser.find_link_by_text(text)
126129
self.assertEqual(key, link['id'])
130+
131+
def test_redirection(self):
132+
"""
133+
when visiting /redirected, browser should be redirected to /redirected-location?come=get&some=true
134+
browser.url should be updated
135+
"""
136+
self.browser.visit('{}redirected'.format(EXAMPLE_APP))
137+
assert 'I just been redirected to this location.' in self.browser.html
138+
self.assertEqual('{}redirect-location?come=get&some=true'.format(EXAMPLE_APP), self.browser.url)

0 commit comments

Comments
 (0)