-
Notifications
You must be signed in to change notification settings - Fork 30
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 #54 from smartystreets/eric/x-forwarded-for
Eric/x forwarded for
- Loading branch information
Showing
3 changed files
with
72 additions
and
4 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
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,54 @@ | ||
import smartystreets_python_sdk as smarty | ||
import unittest | ||
from mock import patch | ||
|
||
|
||
def mocked_session_send(request, **kwargs): | ||
class MockResponse: | ||
def __init__(self, payload, status_code): | ||
self.text = payload | ||
self.status_code = status_code | ||
self.headers = None | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, type, value, traceback): | ||
pass | ||
|
||
def json(self): | ||
return self.text | ||
|
||
mockresponse = MockResponse("This is the test payload.", 200) | ||
|
||
return mockresponse | ||
|
||
|
||
class TestCustomHeaderSender(unittest.TestCase): | ||
|
||
def test_populates_header(self): | ||
sender = smarty.RequestsSender(ip = '0.0.0.0') | ||
|
||
self.assertEqual(sender.ip, '0.0.0.0') | ||
|
||
@patch('requests.Session.send', side_effect=mocked_session_send) | ||
def test_x_forwarded_for_header_set(self, mock_send): | ||
sender = smarty.RequestsSender(ip = '0.0.0.0') | ||
smartyrequest = smarty.Request() | ||
smartyrequest.url_prefix = "http://localhost" | ||
smartyrequest.payload = "This is the test content." | ||
|
||
request = smarty.requests_sender.build_request(smartyrequest, '0.0.0.0') | ||
|
||
self.assertEqual('0.0.0.0', request.headers['X-Forwarded-For']) | ||
|
||
@patch('requests.Session.send', side_effect=mocked_session_send) | ||
def test_custom_headers_used(self, mock_send): | ||
sender = smarty.RequestsSender(ip = '0.0.0.0') | ||
smartyrequest = smarty.Request() | ||
smartyrequest.url_prefix = "http://localhost" | ||
smartyrequest.payload = "This is the test content." | ||
|
||
request = smarty.requests_sender.build_request(smartyrequest, '0.0.0.0') | ||
|
||
self.assertEqual('0.0.0.0', request.headers['X-Forwarded-For']) |