def http_handler(self, request, resp): headers = resp.getheaders() for h, v in headers: if h.lower() == 'content-type': content_type = v.split(';')[0].strip() break if content_type == 'application/json': outfile = self.get_outfile(request['url']) body = resp.read() outfile = '%s.json' % outfile with open(outfile, 'wb') as f: f.write(body) else: # expects a file download url like: /{iid}/files/{uuid}/download/... download_url = request['url'] download_url = download_url.split('/download')[0] outfile = self.get_outfile(download_url) outfile = '%s.data' % outfile body = None with open(outfile, 'wb') as f: van_api.write_body_to_file(resp, f) return dict( status=resp.status, headers=headers, body=body, reason=resp.reason)
def test_it(self): from van_api import write_body_to_file resp = mock.Mock() data = ['abc', 'def', ''] read_called = [] def read(size=None): read_called.append(size) return data.pop(0) resp.read.side_effect = read outfile = mock.Mock() write_body_to_file(resp, outfile) self.assertEqual(read_called, [8192, 8192, 8192]) outfile.seek.assert_called_once_with(0) outfile.truncate.assert_called_once_with(0) outfile.write.assert_has_calls([ mock.call('abc'), mock.call('def')])