Merge pull request #87 from michele-comitini/master
fix oauth issues if redirecting in a form | http custom status error message
This commit is contained in:
@@ -176,7 +176,7 @@ class OAuthAccount(object):
|
|||||||
|
|
||||||
HTTP = self.globals['HTTP']
|
HTTP = self.globals['HTTP']
|
||||||
|
|
||||||
raise HTTP(307,
|
raise HTTP(302,
|
||||||
"You are not authenticated: you are being redirected to the <a href='" + auth_request_url + "'> authentication server</a>",
|
"You are not authenticated: you are being redirected to the <a href='" + auth_request_url + "'> authentication server</a>",
|
||||||
Location=auth_request_url)
|
Location=auth_request_url)
|
||||||
|
|
||||||
|
|||||||
@@ -285,7 +285,7 @@ server for requests. It can be used for the optional"scope" parameters for Face
|
|||||||
if self.args:
|
if self.args:
|
||||||
data.update(self.args)
|
data.update(self.args)
|
||||||
auth_request_url = self.auth_url + "?" + urlencode(data)
|
auth_request_url = self.auth_url + "?" + urlencode(data)
|
||||||
raise HTTP(307,
|
raise HTTP(302,
|
||||||
"You are not authenticated: you are being redirected to the <a href='" + auth_request_url + "'> authentication server</a>",
|
"You are not authenticated: you are being redirected to the <a href='" + auth_request_url + "'> authentication server</a>",
|
||||||
Location=auth_request_url)
|
Location=auth_request_url)
|
||||||
return
|
return
|
||||||
|
|||||||
+15
-4
@@ -69,12 +69,14 @@ class HTTP(BaseException):
|
|||||||
status,
|
status,
|
||||||
body='',
|
body='',
|
||||||
cookies=None,
|
cookies=None,
|
||||||
|
status_message='',
|
||||||
**headers
|
**headers
|
||||||
):
|
):
|
||||||
self.status = status
|
self.status = status
|
||||||
self.body = body
|
self.body = body
|
||||||
self.headers = headers
|
self.headers = headers
|
||||||
self.cookies2headers(cookies)
|
self.cookies2headers(cookies)
|
||||||
|
self.status_message = status_message
|
||||||
|
|
||||||
def cookies2headers(self, cookies):
|
def cookies2headers(self, cookies):
|
||||||
if cookies and len(cookies) > 0:
|
if cookies and len(cookies) > 0:
|
||||||
@@ -85,10 +87,14 @@ class HTTP(BaseException):
|
|||||||
env = env or {}
|
env = env or {}
|
||||||
status = self.status
|
status = self.status
|
||||||
headers = self.headers
|
headers = self.headers
|
||||||
|
status_message = status
|
||||||
if status in defined_status:
|
if status in defined_status:
|
||||||
status = '%d %s' % (status, defined_status[status])
|
if status_message:
|
||||||
|
status = str(status) + ' ' + str(status_message)
|
||||||
|
else:
|
||||||
|
status = '%d %s' % (status, defined_status[status])
|
||||||
else:
|
else:
|
||||||
status = str(status)
|
status = str(status) + ' ' + status_message
|
||||||
if not regex_status.match(status):
|
if not regex_status.match(status):
|
||||||
status = '500 %s' % (defined_status[500])
|
status = '500 %s' % (defined_status[500])
|
||||||
headers.setdefault('Content-Type', 'text/html; charset=UTF-8')
|
headers.setdefault('Content-Type', 'text/html; charset=UTF-8')
|
||||||
@@ -124,12 +130,17 @@ class HTTP(BaseException):
|
|||||||
message elements that are not defined are omitted
|
message elements that are not defined are omitted
|
||||||
"""
|
"""
|
||||||
msg = '%(status)d'
|
msg = '%(status)d'
|
||||||
if self.status in defined_status:
|
status_message = ''
|
||||||
|
if self.status_message:
|
||||||
|
status_message = self.status_message
|
||||||
|
elif self.status in defined_status:
|
||||||
|
status_message = defined_status.get(self.status)
|
||||||
|
if status_message:
|
||||||
msg = '%(status)d %(defined_status)s'
|
msg = '%(status)d %(defined_status)s'
|
||||||
if 'web2py_error' in self.headers:
|
if 'web2py_error' in self.headers:
|
||||||
msg += ' [%(web2py_error)s]'
|
msg += ' [%(web2py_error)s]'
|
||||||
return msg % dict(status=self.status,
|
return msg % dict(status=self.status,
|
||||||
defined_status=defined_status.get(self.status),
|
defined_status=status_message,
|
||||||
web2py_error=self.headers.get('web2py_error'))
|
web2py_error=self.headers.get('web2py_error'))
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from test_http import *
|
||||||
from test_cache import *
|
from test_cache import *
|
||||||
from test_dal import *
|
from test_dal import *
|
||||||
from test_html import *
|
from test_html import *
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""Unit tests for http.py """
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
if os.path.isdir('gluon'):
|
||||||
|
sys.path.append(os.path.realpath('gluon'))
|
||||||
|
else:
|
||||||
|
sys.path.append(os.path.realpath('../'))
|
||||||
|
|
||||||
|
from http import HTTP, defined_status
|
||||||
|
|
||||||
|
|
||||||
|
class TestHTTP(unittest.TestCase):
|
||||||
|
""" Tests http.HTTP """
|
||||||
|
|
||||||
|
def test_status_message(self):
|
||||||
|
""" Tests http status code message """
|
||||||
|
|
||||||
|
h = HTTP
|
||||||
|
|
||||||
|
def gen_status_str(code, message):
|
||||||
|
return str(code) + ' ' + str(message)
|
||||||
|
message = 'This is a custom message'
|
||||||
|
code = 1423
|
||||||
|
self.assertEqual(str(h(code, status_message=message)), gen_status_str(code, message))
|
||||||
|
|
||||||
|
# test predefined codes
|
||||||
|
for code in defined_status.keys():
|
||||||
|
self.assertEqual(str(h(code)), gen_status_str(code, defined_status[code]))
|
||||||
|
|
||||||
|
# test correct use of status_message
|
||||||
|
for code in defined_status.keys():
|
||||||
|
self.assertEqual(str(h(code, status_message=message)), gen_status_str(code, message))
|
||||||
|
|
||||||
|
# test wrong call detection
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user