Skip to content

Commit

Permalink
BREAKING CHANGE feat: add custom HTTP header support (#501)
Browse files Browse the repository at this point in the history
* add custom header support

* add header testing to holodeck
  • Loading branch information
eshanholtz authored Dec 4, 2019
1 parent 04452d4 commit 8b0fb5c
Show file tree
Hide file tree
Showing 53 changed files with 874 additions and 295 deletions.
21 changes: 20 additions & 1 deletion tests/holodeck.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from twilio.base.exceptions import TwilioRestException
from twilio.http import HttpClient
from twilio.http.request import Request
import platform
from twilio import __version__


class Hologram(object):
Expand All @@ -24,9 +26,26 @@ def mock(self, response, request=None):
def requests(self):
return self._requests

def add_standard_headers(self, request):
standard_headers = {
'User-Agent': 'twilio-python/{} (Python {})'.format(
__version__,
platform.python_version()),
'X-Twilio-Client': 'python-{}'.format(__version__),
'Accept': 'application/json',
'Accept-Charset': 'utf-8'
}

if request.method == 'POST' and 'Content-Type' not in standard_headers:
standard_headers['Content-Type'] = 'application/x-www-form-urlencoded'

standard_headers.update(request.headers)
request.headers = standard_headers
return request

def assert_has_request(self, request):
for req in self.requests:
if req == request:
if req == request or req == self.add_standard_headers(request):
return

message = '\nHolodeck never received a request matching: \n + {}\n'.format(request)
Expand Down
16 changes: 12 additions & 4 deletions tests/integration/authy/v1/service/entity/factor/test_challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def test_create_request(self):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities(identity="identity") \
.factors(sid="YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.challenges.create()
.challenges.create(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'post',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities/identity/Factors/YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Challenges',
headers=headers,
))

def test_create_response(self):
Expand Down Expand Up @@ -68,11 +70,13 @@ def test_delete_request(self):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities(identity="identity") \
.factors(sid="YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.challenges(sid="sid").delete()
.challenges(sid="sid").delete(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'delete',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities/identity/Factors/YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Challenges/sid',
headers=headers,
))

def test_delete_response(self):
Expand All @@ -95,11 +99,13 @@ def test_fetch_request(self):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities(identity="identity") \
.factors(sid="YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.challenges(sid="sid").fetch()
.challenges(sid="sid").fetch(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'get',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities/identity/Factors/YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Challenges/sid',
headers=headers,
))

def test_fetch_sid_response(self):
Expand Down Expand Up @@ -175,11 +181,13 @@ def test_update_request(self):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities(identity="identity") \
.factors(sid="YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.challenges(sid="sid").update()
.challenges(sid="sid").update(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'post',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities/identity/Factors/YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Challenges/sid',
headers=headers,
))

def test_verify_sid_response(self):
Expand Down
24 changes: 19 additions & 5 deletions tests/integration/authy/v1/service/entity/test_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ def test_create_request(self):
with self.assertRaises(TwilioException):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities(identity="identity") \
.factors.create(binding="binding", friendly_name="friendly_name", factor_type="app-push")
.factors.create(binding="binding", friendly_name="friendly_name", factor_type="app-push", twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

values = {'Binding': "binding", 'FriendlyName': "friendly_name", 'FactorType': "app-push", }

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'post',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities/identity/Factors',
headers=headers,
))
self.holodeck.assert_has_request(Request(
'post',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities/identity/Factors',
Expand Down Expand Up @@ -66,11 +72,13 @@ def test_delete_request(self):
with self.assertRaises(TwilioException):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities(identity="identity") \
.factors(sid="YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").delete()
.factors(sid="YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").delete(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'delete',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities/identity/Factors/YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
headers=headers,
))

def test_delete_response(self):
Expand All @@ -91,11 +99,13 @@ def test_fetch_request(self):
with self.assertRaises(TwilioException):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities(identity="identity") \
.factors(sid="YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").fetch()
.factors(sid="YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").fetch(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'get',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities/identity/Factors/YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
headers=headers,
))

def test_fetch_response(self):
Expand Down Expand Up @@ -134,11 +144,13 @@ def test_list_request(self):
with self.assertRaises(TwilioException):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities(identity="identity") \
.factors.list()
.factors.list(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'get',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities/identity/Factors',
headers=headers,
))

def test_read_empty_response(self):
Expand Down Expand Up @@ -215,11 +227,13 @@ def test_update_request(self):
with self.assertRaises(TwilioException):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities(identity="identity") \
.factors(sid="YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").update()
.factors(sid="YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").update(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'post',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities/identity/Factors/YFXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
headers=headers,
))

def test_verify_response(self):
Expand Down
20 changes: 16 additions & 4 deletions tests/integration/authy/v1/service/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ def test_create_request(self):

with self.assertRaises(TwilioException):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities.create(identity="identity")
.entities.create(identity="identity", twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

values = {'Identity': "identity", }

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'post',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities',
headers=headers,
))
self.holodeck.assert_has_request(Request(
'post',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities',
Expand Down Expand Up @@ -58,11 +64,13 @@ def test_delete_request(self):

with self.assertRaises(TwilioException):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities(identity="identity").delete()
.entities(identity="identity").delete(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'delete',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities/identity',
headers=headers,
))

def test_delete_response(self):
Expand All @@ -81,11 +89,13 @@ def test_fetch_request(self):

with self.assertRaises(TwilioException):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities(identity="identity").fetch()
.entities(identity="identity").fetch(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'get',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities/identity',
headers=headers,
))

def test_fetch_response(self):
Expand Down Expand Up @@ -117,11 +127,13 @@ def test_list_request(self):

with self.assertRaises(TwilioException):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.entities.list()
.entities.list(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'get',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Entities',
headers=headers,
))

def test_read_empty_response(self):
Expand Down
24 changes: 19 additions & 5 deletions tests/integration/authy/v1/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ def test_create_request(self):
self.holodeck.mock(Response(500, ''))

with self.assertRaises(TwilioException):
self.client.authy.v1.services.create(friendly_name="friendly_name")
self.client.authy.v1.services.create(friendly_name="friendly_name", twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

values = {'FriendlyName': "friendly_name", }

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'post',
'https://authy.twilio.com/v1/Services',
headers=headers,
))
self.holodeck.assert_has_request(Request(
'post',
'https://authy.twilio.com/v1/Services',
Expand Down Expand Up @@ -54,11 +60,13 @@ def test_delete_request(self):
self.holodeck.mock(Response(500, ''))

with self.assertRaises(TwilioException):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").delete()
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").delete(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'delete',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
headers=headers,
))

def test_delete_response(self):
Expand All @@ -75,11 +83,13 @@ def test_fetch_request(self):
self.holodeck.mock(Response(500, ''))

with self.assertRaises(TwilioException):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").fetch()
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").fetch(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'get',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
headers=headers,
))

def test_fetch_response(self):
Expand Down Expand Up @@ -108,11 +118,13 @@ def test_list_request(self):
self.holodeck.mock(Response(500, ''))

with self.assertRaises(TwilioException):
self.client.authy.v1.services.list()
self.client.authy.v1.services.list(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'get',
'https://authy.twilio.com/v1/Services',
headers=headers,
))

def test_read_empty_response(self):
Expand Down Expand Up @@ -177,11 +189,13 @@ def test_update_request(self):
self.holodeck.mock(Response(500, ''))

with self.assertRaises(TwilioException):
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").update()
self.client.authy.v1.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").update(twilio_authy_sandbox_mode="twilio_authy_sandbox_mode")

headers = {'Twilio-Authy-Sandbox-Mode': "twilio_authy_sandbox_mode", }
self.holodeck.assert_has_request(Request(
'post',
'https://authy.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
headers=headers,
))

def test_update_response(self):
Expand Down
16 changes: 13 additions & 3 deletions tests/integration/chat/v2/service/channel/test_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ def test_create_request(self):
with self.assertRaises(TwilioException):
self.client.chat.v2.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.channels(sid="CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.members.create(identity="identity")
.members.create(identity="identity", x_twilio_webhook_enabled="true")

values = {'Identity': "identity", }

headers = {'X-Twilio-Webhook-Enabled': "true", }
self.holodeck.assert_has_request(Request(
'post',
'https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Members',
headers=headers,
))
self.holodeck.assert_has_request(Request(
'post',
'https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Members',
Expand Down Expand Up @@ -181,11 +187,13 @@ def test_delete_request(self):
with self.assertRaises(TwilioException):
self.client.chat.v2.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.channels(sid="CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.members(sid="MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").delete()
.members(sid="MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").delete(x_twilio_webhook_enabled="true")

headers = {'X-Twilio-Webhook-Enabled': "true", }
self.holodeck.assert_has_request(Request(
'delete',
'https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Members/MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
headers=headers,
))

def test_delete_response(self):
Expand All @@ -206,11 +214,13 @@ def test_update_request(self):
with self.assertRaises(TwilioException):
self.client.chat.v2.services(sid="ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.channels(sid="CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.members(sid="MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").update()
.members(sid="MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX").update(x_twilio_webhook_enabled="true")

headers = {'X-Twilio-Webhook-Enabled': "true", }
self.holodeck.assert_has_request(Request(
'post',
'https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Members/MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
headers=headers,
))

def test_update_role_sid_response(self):
Expand Down
Loading

0 comments on commit 8b0fb5c

Please sign in to comment.