ringcentral.http.api_response_test

  1import unittest
  2
  3from ..test import TestCase
  4from ..core import is_third
  5from .api_response import ApiResponse
  6from requests import Response
  7
  8multipart_headers = {'Content-Type': 'multipart/mixed; boundary=Boundary_1245_945802293_1394135045248'}
  9
 10
 11class TestApiResponse(TestCase):
 12    def test_multipart(self):
 13        response = "--Boundary_1245_945802293_1394135045248\n" + \
 14                   "Content-Type: application/json\r\n" + \
 15                   "\r\n" + \
 16                   "{\n" + \
 17                   "  \"response\" : [ {\n" + \
 18                   "    \"status\" : 200\n" + \
 19                   "  }, {\n" + \
 20                   "    \"status\" : 200\n" + \
 21                   "  } ]\n" + \
 22                   "}\n" + \
 23                   "--Boundary_1245_945802293_1394135045248\n" + \
 24                   "Content-Type: application/json\n" + \
 25                   "\n" + \
 26                   "{\n" + \
 27                   "  \"foo\" : \"bar\"\n" + \
 28                   "}\n" + \
 29                   "--Boundary_1245_945802293_1394135045248\n" + \
 30                   "Content-Type: application/json\n" + \
 31                   "\n" + \
 32                   "{\n" + \
 33                   "  \"baz\" : \"qux\"\n" + \
 34                   "}\n" + \
 35                   "--Boundary_1245_945802293_1394135045248--\n"
 36
 37        r = ApiResponse(response=create_response(response, 207, multipart_headers))
 38
 39        self.assertEqual(2, len(r.multipart()))
 40        self.assertEqual('bar', r.multipart()[0].json().foo)
 41        self.assertEqual('qux', r.multipart()[1].json().baz)
 42
 43    def test_multipart_with_error(self):
 44        response = "--Boundary_1245_945802293_1394135045248\n" + \
 45                   "Content-Type: application/json\n" + \
 46                   "\n" + \
 47                   "{\n" + \
 48                   "  \"response\" : [ {\n" + \
 49                   "    \"status\" : 200\n" + \
 50                   "  }, {\n" + \
 51                   "    \"status\" : 404\n" + \
 52                   "  }, {\n" + \
 53                   "    \"status\" : 200\n" + \
 54                   "  } ]\n" + \
 55                   "}\n" + \
 56                   "--Boundary_1245_945802293_1394135045248\n" + \
 57                   "Content-Type: application/json\n" + \
 58                   "\n" + \
 59                   "{\n" + \
 60                   "  \"foo\" : \"bar\"\n" + \
 61                   "}\n" + \
 62                   "--Boundary_1245_945802293_1394135045248\n" + \
 63                   "Content-Type: application/json\n" + \
 64                   "\n" + \
 65                   "{\n" + \
 66                   "  \"error_description\" : \"object not found\"\n" + \
 67                   "}\n" + \
 68                   "--Boundary_1245_945802293_1394135045248\n" + \
 69                   "Content-Type: application/json\n" + \
 70                   "\n" + \
 71                   "{\n" + \
 72                   "  \"baz\" : \"qux\"\n" + \
 73                   "}\n" + \
 74                   "--Boundary_1245_945802293_1394135045248--\n"
 75
 76        r = ApiResponse(response=create_response(response, 207, multipart_headers))
 77
 78        self.assertEqual('bar', r.multipart()[0].json().foo)
 79        self.assertEqual('object not found', r.multipart()[1].error())
 80        self.assertEqual('qux', r.multipart()[2].json().baz)
 81
 82    def test_multipart_bad_response(self):
 83        response = "--Boundary_1245_945802293_1394135045248\n" + \
 84                   "Content-Type: application/json\n" + \
 85                   "\n" + \
 86                   "THIS IS JUNK AND CANNOT BE PARSED AS JSON\n" + \
 87                   "--Boundary_1245_945802293_1394135045248\n" + \
 88                   "Content-Type: application/json\n" + \
 89                   "\n" + \
 90                   "{\n" + \
 91                   "  \"foo\" : \"bar\"\n" + \
 92                   "}\n" + \
 93                   "--Boundary_1245_945802293_1394135045248\n" + \
 94                   "Content-Type: application/json\n" + \
 95                   "\n" + \
 96                   "{\n" + \
 97                   "  \"baz\" : \"qux\"\n" + \
 98                   "}\n" + \
 99                   "--Boundary_1245_945802293_1394135045248--\n"
100
101        r = ApiResponse(response=create_response(response, 207, multipart_headers))
102
103        with self.assertRaises(Exception) as e:
104            r.multipart()
105
106
107# FIXME Use create response from main file api_response
108def create_response(body, status, headers=None):
109    res = Response()
110    res.headers = headers
111    if is_third():
112        res._content = bytes(body, 'utf8')
113        res.encoding = 'utf8'
114    else:
115        res._content = body
116    res.status_code = status
117    return res
118
119
120if __name__ == '__main__':
121    unittest.main()
multipart_headers = {'Content-Type': 'multipart/mixed; boundary=Boundary_1245_945802293_1394135045248'}
class TestApiResponse(ringcentral.test.testcase.TestCase):
 12class TestApiResponse(TestCase):
 13    def test_multipart(self):
 14        response = "--Boundary_1245_945802293_1394135045248\n" + \
 15                   "Content-Type: application/json\r\n" + \
 16                   "\r\n" + \
 17                   "{\n" + \
 18                   "  \"response\" : [ {\n" + \
 19                   "    \"status\" : 200\n" + \
 20                   "  }, {\n" + \
 21                   "    \"status\" : 200\n" + \
 22                   "  } ]\n" + \
 23                   "}\n" + \
 24                   "--Boundary_1245_945802293_1394135045248\n" + \
 25                   "Content-Type: application/json\n" + \
 26                   "\n" + \
 27                   "{\n" + \
 28                   "  \"foo\" : \"bar\"\n" + \
 29                   "}\n" + \
 30                   "--Boundary_1245_945802293_1394135045248\n" + \
 31                   "Content-Type: application/json\n" + \
 32                   "\n" + \
 33                   "{\n" + \
 34                   "  \"baz\" : \"qux\"\n" + \
 35                   "}\n" + \
 36                   "--Boundary_1245_945802293_1394135045248--\n"
 37
 38        r = ApiResponse(response=create_response(response, 207, multipart_headers))
 39
 40        self.assertEqual(2, len(r.multipart()))
 41        self.assertEqual('bar', r.multipart()[0].json().foo)
 42        self.assertEqual('qux', r.multipart()[1].json().baz)
 43
 44    def test_multipart_with_error(self):
 45        response = "--Boundary_1245_945802293_1394135045248\n" + \
 46                   "Content-Type: application/json\n" + \
 47                   "\n" + \
 48                   "{\n" + \
 49                   "  \"response\" : [ {\n" + \
 50                   "    \"status\" : 200\n" + \
 51                   "  }, {\n" + \
 52                   "    \"status\" : 404\n" + \
 53                   "  }, {\n" + \
 54                   "    \"status\" : 200\n" + \
 55                   "  } ]\n" + \
 56                   "}\n" + \
 57                   "--Boundary_1245_945802293_1394135045248\n" + \
 58                   "Content-Type: application/json\n" + \
 59                   "\n" + \
 60                   "{\n" + \
 61                   "  \"foo\" : \"bar\"\n" + \
 62                   "}\n" + \
 63                   "--Boundary_1245_945802293_1394135045248\n" + \
 64                   "Content-Type: application/json\n" + \
 65                   "\n" + \
 66                   "{\n" + \
 67                   "  \"error_description\" : \"object not found\"\n" + \
 68                   "}\n" + \
 69                   "--Boundary_1245_945802293_1394135045248\n" + \
 70                   "Content-Type: application/json\n" + \
 71                   "\n" + \
 72                   "{\n" + \
 73                   "  \"baz\" : \"qux\"\n" + \
 74                   "}\n" + \
 75                   "--Boundary_1245_945802293_1394135045248--\n"
 76
 77        r = ApiResponse(response=create_response(response, 207, multipart_headers))
 78
 79        self.assertEqual('bar', r.multipart()[0].json().foo)
 80        self.assertEqual('object not found', r.multipart()[1].error())
 81        self.assertEqual('qux', r.multipart()[2].json().baz)
 82
 83    def test_multipart_bad_response(self):
 84        response = "--Boundary_1245_945802293_1394135045248\n" + \
 85                   "Content-Type: application/json\n" + \
 86                   "\n" + \
 87                   "THIS IS JUNK AND CANNOT BE PARSED AS JSON\n" + \
 88                   "--Boundary_1245_945802293_1394135045248\n" + \
 89                   "Content-Type: application/json\n" + \
 90                   "\n" + \
 91                   "{\n" + \
 92                   "  \"foo\" : \"bar\"\n" + \
 93                   "}\n" + \
 94                   "--Boundary_1245_945802293_1394135045248\n" + \
 95                   "Content-Type: application/json\n" + \
 96                   "\n" + \
 97                   "{\n" + \
 98                   "  \"baz\" : \"qux\"\n" + \
 99                   "}\n" + \
100                   "--Boundary_1245_945802293_1394135045248--\n"
101
102        r = ApiResponse(response=create_response(response, 207, multipart_headers))
103
104        with self.assertRaises(Exception) as e:
105            r.multipart()

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.
def test_multipart(self):
13    def test_multipart(self):
14        response = "--Boundary_1245_945802293_1394135045248\n" + \
15                   "Content-Type: application/json\r\n" + \
16                   "\r\n" + \
17                   "{\n" + \
18                   "  \"response\" : [ {\n" + \
19                   "    \"status\" : 200\n" + \
20                   "  }, {\n" + \
21                   "    \"status\" : 200\n" + \
22                   "  } ]\n" + \
23                   "}\n" + \
24                   "--Boundary_1245_945802293_1394135045248\n" + \
25                   "Content-Type: application/json\n" + \
26                   "\n" + \
27                   "{\n" + \
28                   "  \"foo\" : \"bar\"\n" + \
29                   "}\n" + \
30                   "--Boundary_1245_945802293_1394135045248\n" + \
31                   "Content-Type: application/json\n" + \
32                   "\n" + \
33                   "{\n" + \
34                   "  \"baz\" : \"qux\"\n" + \
35                   "}\n" + \
36                   "--Boundary_1245_945802293_1394135045248--\n"
37
38        r = ApiResponse(response=create_response(response, 207, multipart_headers))
39
40        self.assertEqual(2, len(r.multipart()))
41        self.assertEqual('bar', r.multipart()[0].json().foo)
42        self.assertEqual('qux', r.multipart()[1].json().baz)
def test_multipart_with_error(self):
44    def test_multipart_with_error(self):
45        response = "--Boundary_1245_945802293_1394135045248\n" + \
46                   "Content-Type: application/json\n" + \
47                   "\n" + \
48                   "{\n" + \
49                   "  \"response\" : [ {\n" + \
50                   "    \"status\" : 200\n" + \
51                   "  }, {\n" + \
52                   "    \"status\" : 404\n" + \
53                   "  }, {\n" + \
54                   "    \"status\" : 200\n" + \
55                   "  } ]\n" + \
56                   "}\n" + \
57                   "--Boundary_1245_945802293_1394135045248\n" + \
58                   "Content-Type: application/json\n" + \
59                   "\n" + \
60                   "{\n" + \
61                   "  \"foo\" : \"bar\"\n" + \
62                   "}\n" + \
63                   "--Boundary_1245_945802293_1394135045248\n" + \
64                   "Content-Type: application/json\n" + \
65                   "\n" + \
66                   "{\n" + \
67                   "  \"error_description\" : \"object not found\"\n" + \
68                   "}\n" + \
69                   "--Boundary_1245_945802293_1394135045248\n" + \
70                   "Content-Type: application/json\n" + \
71                   "\n" + \
72                   "{\n" + \
73                   "  \"baz\" : \"qux\"\n" + \
74                   "}\n" + \
75                   "--Boundary_1245_945802293_1394135045248--\n"
76
77        r = ApiResponse(response=create_response(response, 207, multipart_headers))
78
79        self.assertEqual('bar', r.multipart()[0].json().foo)
80        self.assertEqual('object not found', r.multipart()[1].error())
81        self.assertEqual('qux', r.multipart()[2].json().baz)
def test_multipart_bad_response(self):
 83    def test_multipart_bad_response(self):
 84        response = "--Boundary_1245_945802293_1394135045248\n" + \
 85                   "Content-Type: application/json\n" + \
 86                   "\n" + \
 87                   "THIS IS JUNK AND CANNOT BE PARSED AS JSON\n" + \
 88                   "--Boundary_1245_945802293_1394135045248\n" + \
 89                   "Content-Type: application/json\n" + \
 90                   "\n" + \
 91                   "{\n" + \
 92                   "  \"foo\" : \"bar\"\n" + \
 93                   "}\n" + \
 94                   "--Boundary_1245_945802293_1394135045248\n" + \
 95                   "Content-Type: application/json\n" + \
 96                   "\n" + \
 97                   "{\n" + \
 98                   "  \"baz\" : \"qux\"\n" + \
 99                   "}\n" + \
100                   "--Boundary_1245_945802293_1394135045248--\n"
101
102        r = ApiResponse(response=create_response(response, 207, multipart_headers))
103
104        with self.assertRaises(Exception) as e:
105            r.multipart()
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_
def create_response(body, status, headers=None):
109def create_response(body, status, headers=None):
110    res = Response()
111    res.headers = headers
112    if is_third():
113        res._content = bytes(body, 'utf8')
114        res.encoding = 'utf8'
115    else:
116        res._content = body
117    res.status_code = status
118    return res