ringcentral.http.multipart_builder_test
1import unittest 2 3from ..test import TestCase 4from .multipart_builder import MultipartBuilder 5 6class MockPlatform: 7 def create_url(self, url, add_server): 8 if add_server: 9 return 'http://example.com/' + url 10 return url 11 12class TestMultipartBuilder(TestCase): 13 def test_add(self): 14 mb = MultipartBuilder(MockPlatform()) 15 16 mb.set_body({'foo': 'bar'}) 17 mb.add(('report.csv', 'some,data,to,send')) 18 19 req = mb.request('/foo') 20 21 self.assertEqual(mb.body(), {'foo': 'bar'}) 22 self.assertEqual(mb.contents(), [('attachment', ('report.csv', 'some,data,to,send'))]) 23 self.assertEqual(req.files, [ 24 ('json', ('request.json', '{"foo": "bar"}', 'application/json')), 25 ('attachment', ('report.csv', 'some,data,to,send')) 26 ]) 27 28 def test_multipart_mixed(self): 29 mb = MultipartBuilder(MockPlatform()) 30 31 mb.set_body({'foo': 'bar'}) 32 mb.add(('report.csv', 'some,data,to,send')) 33 mb.set_multipart_mixed(True) 34 35 req = mb.request('/foo') 36 37 self.assertTrue('multipart/mixed' in req.headers['Content-Type'], True) 38 self.assertEqual(mb.body(), {'foo': 'bar'}) 39 self.assertEqual(mb.contents(), [('attachment', ('report.csv', 'some,data,to,send'))]) 40 41 42if __name__ == '__main__': 43 unittest.main()
13class TestMultipartBuilder(TestCase): 14 def test_add(self): 15 mb = MultipartBuilder(MockPlatform()) 16 17 mb.set_body({'foo': 'bar'}) 18 mb.add(('report.csv', 'some,data,to,send')) 19 20 req = mb.request('/foo') 21 22 self.assertEqual(mb.body(), {'foo': 'bar'}) 23 self.assertEqual(mb.contents(), [('attachment', ('report.csv', 'some,data,to,send'))]) 24 self.assertEqual(req.files, [ 25 ('json', ('request.json', '{"foo": "bar"}', 'application/json')), 26 ('attachment', ('report.csv', 'some,data,to,send')) 27 ]) 28 29 def test_multipart_mixed(self): 30 mb = MultipartBuilder(MockPlatform()) 31 32 mb.set_body({'foo': 'bar'}) 33 mb.add(('report.csv', 'some,data,to,send')) 34 mb.set_multipart_mixed(True) 35 36 req = mb.request('/foo') 37 38 self.assertTrue('multipart/mixed' in req.headers['Content-Type'], True) 39 self.assertEqual(mb.body(), {'foo': 'bar'}) 40 self.assertEqual(mb.contents(), [('attachment', ('report.csv', 'some,data,to,send'))])
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the __init__ method, the base class __init__ method must always be called. It is important that subclasses should not change the signature of their __init__ method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes:
- failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'.
- longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed.
- maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
14 def test_add(self): 15 mb = MultipartBuilder(MockPlatform()) 16 17 mb.set_body({'foo': 'bar'}) 18 mb.add(('report.csv', 'some,data,to,send')) 19 20 req = mb.request('/foo') 21 22 self.assertEqual(mb.body(), {'foo': 'bar'}) 23 self.assertEqual(mb.contents(), [('attachment', ('report.csv', 'some,data,to,send'))]) 24 self.assertEqual(req.files, [ 25 ('json', ('request.json', '{"foo": "bar"}', 'application/json')), 26 ('attachment', ('report.csv', 'some,data,to,send')) 27 ])
29 def test_multipart_mixed(self): 30 mb = MultipartBuilder(MockPlatform()) 31 32 mb.set_body({'foo': 'bar'}) 33 mb.add(('report.csv', 'some,data,to,send')) 34 mb.set_multipart_mixed(True) 35 36 req = mb.request('/foo') 37 38 self.assertTrue('multipart/mixed' in req.headers['Content-Type'], True) 39 self.assertEqual(mb.body(), {'foo': 'bar'}) 40 self.assertEqual(mb.contents(), [('attachment', ('report.csv', 'some,data,to,send'))])
Inherited Members
- ringcentral.test.testcase.TestCase
- TestCase
- get_sdk
- add
- authentication_mock
- logout_mock
- presence_subscription_mock
- refresh_mock
- subscription_mock
- delete_mock_with_body
- delete_mock_without_body
- unittest.case.TestCase
- failureException
- longMessage
- maxDiff
- addTypeEqualityFunc
- addCleanup
- enterContext
- addClassCleanup
- enterClassContext
- setUp
- tearDown
- setUpClass
- tearDownClass
- countTestCases
- defaultTestResult
- shortDescription
- id
- subTest
- run
- doCleanups
- doClassCleanups
- debug
- skipTest
- fail
- assertFalse
- assertTrue
- assertRaises
- assertWarns
- assertLogs
- assertNoLogs
- assertEqual
- assertNotEqual
- assertAlmostEqual
- assertNotAlmostEqual
- assertSequenceEqual
- assertListEqual
- assertTupleEqual
- assertSetEqual
- assertIn
- assertNotIn
- assertIs
- assertIsNot
- assertDictEqual
- assertDictContainsSubset
- assertCountEqual
- assertMultiLineEqual
- assertLess
- assertLessEqual
- assertGreater
- assertGreaterEqual
- assertIsNone
- assertIsNotNone
- assertIsInstance
- assertNotIsInstance
- assertRaisesRegex
- assertWarnsRegex
- assertRegex
- assertNotRegex
- failUnlessRaises
- failIf
- assertRaisesRegexp
- assertRegexpMatches
- assertNotRegexpMatches
- failUnlessEqual
- assertEquals
- failIfEqual
- assertNotEquals
- failUnlessAlmostEqual
- assertAlmostEquals
- failIfAlmostEqual
- assertNotAlmostEquals
- failUnless
- assert_