def testRead(self): f = File('CHANGELOG.md', 'r') handle = open('CHANGELOG.md') self.assertEqual(handle.read(), f.read()) f.close() handle.close()
def testFromHandle(self): handle = open('CHANGELOG.md', 'rb') f = File.fromhandle(handle) self.assertEqual('rb', f.mode) self.assertEqual('CHANGELOG.md', f.name) f.close() handle.close()
def test_HttpClient_execute_filesArePreservedInCopy(self): client = HttpClient(self.environment()) request = GenericRequest() request.path = "/" request.verb = "GET" request.headers["Content-Type"] = "multipart/related" license = File("LICENSE") request.body = {"license": license} self.stub_request_with_empty_reponse(request) client.execute(request) self.assertTrue(str(license.read()) in responses.calls[0].request.body) license.close()
def testRead_throwsIfClosed(self): f = File('CHANGELOG.md', 'r') f.read() f.close() try: f.read() self.fail( 'File.read() should have thrown an IOError after closing') except IOError as error: self.assertEqual('Open of closed file', str(error))
def testClose(self): f = File('CHANGELOG.md', 'r') f.open() self.assertFalse(f.closed) f.close() self.assertTrue(f.closed)
def test_Encoder_serialize_request_withMultipartContentType_stringifysData( self): request = GenericRequest() request.path = "/" request.verb = "POST" request.headers = {"Content-Type": "multipart/form-data; charset=utf8"} f = File(abspath('tests/resources/fileupload_test_binary.jpg')) data = f.read() request.body = { "some_key": "some_value", "some_nested[key]": "some_nested_value", "file": f } serialized = Encoder([Json(), Text(), Multipart(), FormEncoded()]).serialize_request(request) self.assertTrue("multipart/form-data; boundary=" in request.headers["Content-Type"]) self.assertTrue( "Content-Disposition: form-data; name=\"some_key\"" in serialized) self.assertTrue("some_value" in serialized) self.assertTrue( "Content-Disposition: form-data; name=\"some_nested[key]\"" in serialized) self.assertTrue("some_nested_value" in serialized) self.assertTrue( "Content-Disposition: form-data; name=\"file\"; filename=\"fileupload_test_binary.jpg\"" in serialized) self.assertTrue("Content-Type: image/jpeg" in serialized) self.assertTrue(str(data) in serialized) f.close()
def test_Encoder_serialize_request_withMultipartContentType_encodesFormPart( self): request = GenericRequest() request.path = "/" request.verb = "POST" request.headers = {"Content-Type": "multipart/form-data; charset=utf8"} f = File(abspath('tests/resources/fileupload_test_binary.jpg')) data = f.read() request.body = { "file": f, "input": FormPart({"key": "val"}, {"Content-Type": "application/json"}) } serialized = Encoder([Json(), Text(), Multipart(), FormEncoded()]).serialize_request(request) self.assertTrue("multipart/form-data; boundary=" in request.headers["Content-Type"]) self.assertTrue( "Content-Disposition: form-data; name=\"input\"; filename=\"input.json\"" in serialized) self.assertTrue("{\"key\": \"val\"}" in serialized) self.assertTrue( "Content-Disposition: form-data; name=\"file\"; filename=\"fileupload_test_binary.jpg\"" in serialized) self.assertTrue("Content-Type: image/jpeg" in serialized) self.assertTrue(str(data) in serialized) self.assertIsNotNone( re.search('.*input.json.*fileupload_test_binary.jpg.*', serialized, re.MULTILINE | re.DOTALL)) f.close()
def testRead_allowsMultipleReads(self): f = File('CHANGELOG.md', 'r') contents = f.read() self.assertEqual(contents, f.read())