Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
webssh
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
郑天保
webssh
Commits
50ab045c
Commit
50ab045c
authored
Apr 24, 2018
by
Sheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use HTTPServerRequest instead
parent
42ac3daf
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
19 deletions
+8
-19
test_handler.py
tests/test_handler.py
+8
-19
No files found.
tests/test_handler.py
View file @
50ab045c
import
unittest
import
sys
from
tornado.httputil
import
HTTPServerRequest
from
handler
import
MixinHandler
class
RequestMock
(
object
):
def
__init__
(
self
):
self
.
headers
=
{}
def
set_ip
(
self
,
ip
):
self
.
headers
[
'X-Real-Ip'
]
=
ip
def
set_port
(
self
,
port
):
self
.
headers
[
'X-Real-Port'
]
=
port
class
TestMixinHandler
(
unittest
.
TestCase
):
def
test_get_real_client_addr_without_nginx_config
(
self
):
handler
=
MixinHandler
()
handler
.
request
=
RequestMock
(
)
handler
.
request
=
HTTPServerRequest
(
uri
=
'/'
)
self
.
assertIsNone
(
handler
.
get_real_client_addr
())
def
test_get_real_client_addr_with_correct_nginx_config
(
self
):
handler
=
MixinHandler
()
handler
.
request
=
RequestMock
(
)
handler
.
request
=
HTTPServerRequest
(
uri
=
'/'
)
ip
=
'127.0.0.1'
handler
.
request
.
set_ip
(
ip
)
handler
.
request
.
set_port
(
'12345'
)
handler
.
request
.
headers
.
add
(
'X-Real-Ip'
,
ip
)
handler
.
request
.
headers
.
add
(
'X-Real-Port'
,
'12345'
)
self
.
assertEqual
(
handler
.
get_real_client_addr
(),
(
ip
,
12345
))
@unittest.skipIf
(
sys
.
version_info
<
(
3
,),
reason
=
'assertLogs not supported in Python 2'
)
def
test_get_real_client_addr_with_bad_nginx_config
(
self
):
handler
=
MixinHandler
()
handler
.
request
=
RequestMock
(
)
handler
.
request
=
HTTPServerRequest
(
uri
=
'/'
)
ip
=
'127.0.0.1'
handler
.
request
.
set_ip
(
ip
)
handler
.
request
.
headers
.
add
(
'X-Real-Ip'
,
ip
)
with
self
.
assertLogs
()
as
cm
:
handler
.
get_real_client_addr
()
self
.
assertEqual
(
cm
.
output
,
[
'WARNING:root:Bad nginx configuration.'
])
handler
.
request
.
set_port
(
'12345x'
)
handler
.
request
.
headers
.
add
(
'X-Real-Port'
,
'12345x'
)
with
self
.
assertLogs
()
as
cm
:
handler
.
get_real_client_addr
()
self
.
assertEqual
(
cm
.
output
,
[
'WARNING:root:Bad nginx configuration.'
])
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment