def test_iter(self): "Iterate through lines of body content" with patch('ffs.contrib.http.HTTPFilesystem.open') as popen: popen.return_value = StringIO("<html>\nHai\n<html>") expected = ['<html>\n', 'Hai\n', '<html>'] for i, line in enumerate(http.HTTPPath('example.com')): self.assertEqual(expected[i], line)
def test_open_headers(self): "Should have access to the headers" with patch('requests.get') as pget: pget.return_value.content = 'Hai\n' pget.return_value.headers = dict(haps='bar') p = http.HTTPPath('qwantz.com') with p as fh: self.assertEqual({'haps': 'bar'}, fh.headers)
def fh_for_url(url): """ Return a file-like-object for URL! """ return http.HTTPPath(url).open()
def test_radd_return(self): "Should be pathy" p = 'www.qwantz.com' p2 = p + http.HTTPPath('comix') self.assertIsInstance(p2, http.HTTPPath) self.assertEqual('www.qwantz.com/comix', p2)
def test_iadd_return(self): "Should be pathy" p = http.HTTPPath('www.qwantz.com') p += 'comix' self.assertIsInstance(p, http.HTTPPath) self.assertEqual('www.qwantz.com/comix', p)
def test_add(self): "Should be Pathy" p = http.HTTPPath('qwantz.com') p2 = p + 'comix' self.assertIsInstance(p2, http.HTTPPath) self.assertEqual('qwantz.com/comix', p2)
def test_setitem_raises(self): "Should be an immutable collection" p = http.HTTPPath('qwantz.com') with self.assertRaises(TypeError): p[-1] = 'dinosaurcomics.com'
def test_getitem_klass(self): "Should be a Path" p = http.HTTPPath('localhost:8000/foo/bar') self.assertIsInstance(p[:1], http.HTTPPath) self.assertIsInstance(p[0], http.HTTPPath)
def test_eq(self): "Should be equal to strings" p = http.HTTPPath('www.bbc.co.uk/weather') self.assertEqual('www.bbc.co.uk/weather', p)
def test_contains_questionmark(self): "Shouldn't blow up" cases = [('localhost:8000', False), ('localhost:8000/?haps=true', True)] for url, expected in cases: self.assertEqual(expected, http.HTTPPath(url).__contains__('?'))
def test_contextmanager(self): "Filelike object as a contextmanager" with patch('ffs.contrib.http.HTTPFilesystem.open') as popen: popen.return_value = StringIO("Hai\n") with http.HTTPPath('example.com') as hh: self.assertEqual('Hai\n', hh.read())
def test_init_httppath(self): "Deal with initializing with a HTTPPath" p1 = http.HTTPPath('www.bbc.co.uk') p2 = http.HTTPPath(p1) self.assertIsInstance(p2._value, str) self.assertEqual('www.bbc.co.uk', p2)
Start a server in another thread, then interact with it. """ import os import sys import multiprocessing import time from ffs.contrib import http def runserver(): from SimpleHTTPServer import test as goservergo goservergo() servit = multiprocessing.Process(target=runserver) servit.start() briefly = 0.54321 time.sleep(briefly) p = http.HTTPPath('localhost:8000') with p as page: for i, line in enumerate(page): print line if i > 5: break print page.headers print page.ls()