def test_encode_multipart_without_files(): mocker = Mox() file_path = '/path/to/file.pdf' old_guess_type = multipart.guess_type multipart.guess_type = mocker.CreateMockAnything() multipart.guess_type(file_path).AndReturn(('some-mimetype', None)) file_mock = mocker.CreateMockAnything() file_mock.name = file_path file_mock.read().AndReturn('MY_FILE_CONTENT') mocker.ReplayAll() try: my_data = { 'foo': u'bar', 'age': 21, 'my_file': file_mock, } got = multipart.encode_multipart('fakeboundary', my_data) assert_equals(got, '--fakeboundary\r\nContent-Disposition: form-data; ' \ 'name="age"\r\n\r\n21\r\n--fakeboundary\r\nContent-' \ 'Disposition: form-data; name="foo"\r\n\r\nbar\r\n-' \ '-fakeboundary\r\nContent-Disposition: form-data; ' \ 'name="my_file"; filename="file.pdf"\r\nContent-Type: ' \ 'some-mimetype\r\n\r\nMY_FILE_CONTENT\r\n--' \ 'fakeboundary--\r\n') mocker.VerifyAll() finally: multipart.guess_type = old_guess_type
def test_guess_mime_guessed(): mocker = Mox() old_guess_type = multipart.guess_type multipart.guess_type = mocker.CreateMockAnything() multipart.guess_type('should-be-a-path').AndReturn(('some-mimetype', None)) mocker.ReplayAll() try: assert_equals(multipart.guess_mime('should-be-a-path'), 'some-mimetype') mocker.VerifyAll() finally: multipart.guess_type = old_guess_type
def test_guess_mime_fallsback_to_octet_stream(): mocker = Mox() old_guess_type = multipart.guess_type multipart.guess_type = mocker.CreateMockAnything() multipart.guess_type('should-be-a-path').AndReturn((None, None)) mocker.ReplayAll() try: assert_equals(multipart.guess_mime('should-be-a-path'), 'application/octet-stream') mocker.VerifyAll() finally: multipart.guess_type = old_guess_type
def test_encode_file(): mocker = Mox() expected_list = ['--my-boundary', 'Content-Disposition: form-data; name="my_file"; ' \ 'filename="file.pdf"', 'Content-Type: application/octet-stream', '', 'Gabriel Falc\xc3\x83\xc2\xa3o'] old_guess_type = multipart.guess_type multipart.guess_type = mocker.CreateMockAnything() multipart.guess_type('/path/to/file.pdf').AndReturn((None, None)) file_mock = mocker.CreateMockAnything() file_mock.name = '/path/to/file.pdf' file_mock.read().AndReturn(u'Gabriel Falcão'.decode('latin1')) mocker.ReplayAll() try: got = multipart.encode_file('my-boundary', 'my_file', file_mock) assert_equals(got, expected_list) mocker.VerifyAll() finally: multipart.guess_type = old_guess_type