class TestTxClient(TestCase): @inlineCallbacks def setUp(self): self.root = DummyServerResource() site_factory = Site(self.root) self.webserver = yield reactor.listenTCP(0, site_factory) addr = self.webserver.getHost() self.txclient = TxClient("http://%s:%s/" % (addr.host, addr.port)) @inlineCallbacks def tearDown(self): yield self.webserver.loseConnection() @inlineCallbacks def test_send(self): samples = [ ["Line 1", 101], ["Line 2", 102], ] now = datetime.now() def check_request(request): self.assertEqual(request.uri, '/api/store/') self.assertEqual(request.method, 'POST') self.assertEqual(request.headers['content-type'], 'text/plain') raw_data = request.content.read() data = self.txclient.decode(raw_data) self.assertEqual(data['timestamp'], datetime.strftime(now, '%Y-%m-%d %H:%M:%S')) self.assertEqual(data['api_key'], '1') self.assertEqual(data['samples'], samples) return "ok" self.root.set_request_handler(check_request) response = yield self.txclient.send(api_key="1", samples=samples, timestamp=now) self.assertEqual(response, "ok")
def setUp(self): self.root = DummyServerResource() site_factory = Site(self.root) self.webserver = yield reactor.listenTCP(0, site_factory) addr = self.webserver.getHost() self.txclient = TxClient("http://%s:%s/" % (addr.host, addr.port))