Skip to content

Commit

Permalink
add Context.reconstructed_url
Browse files Browse the repository at this point in the history
  • Loading branch information
craigahobbs committed Oct 21, 2016
1 parent e4d6aae commit 3f5e98d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
30 changes: 29 additions & 1 deletion chisel/app.py
Expand Up @@ -26,7 +26,7 @@
import logging
import os
import re
from urllib.parse import unquote
from urllib.parse import quote, unquote

from .app_defs import ENVIRON_CTX
from .request import Request
Expand Down Expand Up @@ -260,3 +260,31 @@ def response_json(self, status, response, content_type='application/json', encod
else:
content_list = [content.encode(encoding)]
return self.response(status, content_type, content_list, headers=headers)

@property
def reconstructed_url(self):
"""
Reconstructs the request URL using the algorithm provided by PEP3333
"""

environ = self.environ
url = environ['wsgi.url_scheme']+'://'

if environ.get('HTTP_HOST'):
url += environ['HTTP_HOST']
else:
url += environ['SERVER_NAME']

if environ['wsgi.url_scheme'] == 'https':
if environ['SERVER_PORT'] != '443':
url += ':' + environ['SERVER_PORT']
else:
if environ['SERVER_PORT'] != '80':
url += ':' + environ['SERVER_PORT']

url += quote(environ.get('SCRIPT_NAME', ''))
url += quote(environ.get('PATH_INFO', ''))
if environ.get('QUERY_STRING'):
url += '?' + environ['QUERY_STRING']

return url
59 changes: 57 additions & 2 deletions chisel/tests/test_app.py
Expand Up @@ -28,10 +28,10 @@
import types
import unittest

from chisel import action, Application, ENVIRON_CTX, Request
from chisel import action, Application, Context, ENVIRON_CTX, Request


class TestAppApplication(unittest.TestCase):
class TestApplication(unittest.TestCase):

def setUp(self):

Expand Down Expand Up @@ -392,3 +392,58 @@ def my_action(dummy_ctx, req):
status, dummy_headers, response = app.request('GET', '/my_action/3/4')
self.assertEqual(status, '200 OK')
self.assertEqual(response, b'{"sum":7}')


class TestContext(unittest.TestCase):

def test_context_reconstructed_url(self):
app = Application()

# Minimal HTTP_HOST
ctx = Context(app, environ={
'wsgi.url_scheme': 'http',
'HTTP_HOST': 'localhost'
})
self.assertEqual(ctx.reconstructed_url, 'http://localhost')

# Minimal SERVER_NAME/SERVER_PORT
ctx = Context(app, environ={
'wsgi.url_scheme': 'http',
'SERVER_NAME': 'localhost',
'SERVER_PORT': '80'
})
self.assertEqual(ctx.reconstructed_url, 'http://localhost')

# HTTP non-80 SERVER_PORT
ctx = Context(app, environ={
'wsgi.url_scheme': 'http',
'SERVER_NAME': 'localhost',
'SERVER_PORT': '8080'
})
self.assertEqual(ctx.reconstructed_url, 'http://localhost:8080')

# HTTPS
ctx = Context(app, environ={
'wsgi.url_scheme': 'https',
'SERVER_NAME': 'localhost',
'SERVER_PORT': '443'
})
self.assertEqual(ctx.reconstructed_url, 'https://localhost')

# HTTPS non-443 SERVER_PORT
ctx = Context(app, environ={
'wsgi.url_scheme': 'https',
'SERVER_NAME': 'localhost',
'SERVER_PORT': '8443'
})
self.assertEqual(ctx.reconstructed_url, 'https://localhost:8443')

# Complete
ctx = Context(app, environ={
'wsgi.url_scheme': 'http',
'HTTP_HOST': 'localhost',
'SCRIPT_NAME': '',
'PATH_INFO': '/request',
'QUERY_STRING': 'foo=bar'
})
self.assertEqual(ctx.reconstructed_url, 'http://localhost/request?foo=bar')

0 comments on commit 3f5e98d

Please sign in to comment.