Skip to content

Commit

Permalink
Make treq close the http connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudi Giesler committed Mar 23, 2015
1 parent c44cfd5 commit dd1ace7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
32 changes: 19 additions & 13 deletions vumi_twilio_api/tests/test_twilio_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import treq
from twilio.rest import TwilioRestClient
from twisted.internet.defer import inlineCallbacks
from twisted.trial.unittest import TestCase
from vumi.application.tests.helpers import ApplicationHelper
Expand All @@ -9,6 +8,7 @@

from vumi_twilio_api.twilio_api import TwilioAPIServer, TwilioAPIWorker


class TestTwilioAPIServer(VumiTestCase):

@inlineCallbacks
Expand All @@ -21,14 +21,17 @@ def setUp(self):
})
addr = self.worker.webserver.getHost()
self.url = 'http://%s:%s%s' % (addr.host, addr.port, '/api')
account = "TestAccount"
token = "test_account_token"
self.client = TwilioRestClient(account, token, base=self.url, version='v1')

def _server_request(self, path=''):
url = '%s/v1/%s' % (self.url, path)
return treq.get(url, persistent=False)

@inlineCallbacks
def test_root_default(self):
response = yield treq.get(self.url + '/v1/')
self.assertEqual(response.headers.getRawHeaders('content-type'), ['application/xml'])
response = yield self._server_request()
self.assertEqual(
response.headers.getRawHeaders('content-type'),
['application/xml'])
self.assertEqual(response.code, 200)
content = yield response.content()
root = ET.fromstring(content)
Expand All @@ -37,22 +40,26 @@ def test_root_default(self):

@inlineCallbacks
def test_root_xml(self):
response = yield treq.get(self.url + '/v1/.xml')
self.assertEqual(response.headers.getRawHeaders('content-type'), ['application/xml'])
response = yield self._server_request('.xml')
self.assertEqual(
response.headers.getRawHeaders('content-type'),
['application/xml'])
self.assertEqual(response.code, 200)
content = yield response.content()
root = ET.fromstring(content)
self.assertEqual(root.tag, "TwilioResponse")
self.assertEqual(list(root), [])

@inlineCallbacks
def test_root_json(self):
response = yield treq.get(self.url + '/v1/.json')
self.assertEqual(response.headers.getRawHeaders('content-type'), ['application/xml'])
response = yield self._server_request('.json')
self.assertEqual(
response.headers.getRawHeaders('content-type'),
['application/json'])
self.assertEqual(response.code, 200)
content = yield response.json()
self.assertEqual(content, {})


class TestServerFormatting(TestCase):

Expand Down Expand Up @@ -95,4 +102,3 @@ def test_format_json(self):
res = format_json(d)
root = json.loads(res)
self.assertEqual(root, d)

6 changes: 4 additions & 2 deletions vumi_twilio_api/twilio_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from vumi.config import ConfigInt, ConfigText
import xml.etree.ElementTree as ET


class TwilioAPIConfig(ApplicationWorker.CONFIG_CLASS):
"""Config for the Twilio API worker"""
web_path = ConfigText(
Expand Down Expand Up @@ -57,8 +58,9 @@ def format_json(dct):
return json.dumps(dct)

def _format_response(self, request, dct, format_):
format_ = format_.lstrip('.').lower()
func = getattr(TwilioAPIServer, 'format_' + format_, TwilioAPIServer.format_xml)
format_ = str(format_.lstrip('.').lower())
func = getattr(
TwilioAPIServer, 'format_' + format_, TwilioAPIServer.format_xml)
request.setHeader('Content-Type', 'application/%s' % format_)
return func(dct)

Expand Down

0 comments on commit dd1ace7

Please sign in to comment.