def __test_doctype(self,type): ''' Same test for different DOCTYPEs ''' url = self.junit_url+'/doctype_'+type portlet = Marionet.objects.create(url=url, session=True) client = WebClient() self.assert_(client) response = client.get(url) self.assertEqual(200, response.status) out = PageProcessor.process(response,portlet.session) self.assert_(out) soup = BeautifulSoup(str(out)) self.assert_(soup) # only body remains self.assertEqual('div', soup.find().name) self.assertEqual(None, soup.find('head')) # namespace is correct portlet_div = soup.find('div', {'class': '%s_body' % portlet.session.get('namespace')}) self.assert_(portlet_div) # base + title + content are correct self.assertEqual('http://127.0.0.10:3000/',portlet.session.get('baseURL')) self.assertEqual('Portlet title',portlet.session.get('title')) self.assertEqual('Portlet content',portlet_div.text)
def test_images(self): ''' Image url rewrite ''' url = self.junit_url+'/basic_tags' portlet = Marionet(url=url, session=True) client = WebClient() self.assert_(client) response = client.get(url) self.assertEqual(200, response.status) portlet.session.set('location', 'http://example.com:8000/some-page') out = PageProcessor.process(response,portlet.session) self.assert_(out) soup = BeautifulSoup(str(out)) self.assert_(soup) #print soup # absolute url img_url = 'http://localhost:3000/images/portlet_test_bench/rails.png' self.assertEqual( soup.find(id='image_absolute_url').findNext('img')['src'], img_url ) # relative url self.assertEqual( soup.find(id='image_absolute_path').findNext('img')['src'], img_url ) # explicit base url self.assertEqual( soup.find(id='image_relative_path').findNext('img')['src'], img_url )
def test_xhr(self): """ Simple XHR test. """ client = WebClient() self.assert_(client) response = client.post(self.junit_url+'/check_xhr',headers={'xhr': True}) self.assertEqual(200, response.status) self.assertEqual(response.read(), 'true')
def test_get(self): """ Simple GET. """ client = WebClient() self.assert_(client) self.assertEqual(0, len(client.cookies)) response = client.get(self.junit_url) self.assertEqual(200, response.status) self.assertEqual(0, len(client.cookies))
def test_post_redirect(self): """ Test POST + redirect. """ client = WebClient() self.assert_(client) self.assertEqual(0, len(client.cookies)) url = self.junit_url+'/post_redirect_get' response = client.post(url) self.assertEqual(200, response.status) self.assertEqual(0, len(client.cookies)) self.assertEqual( '/caterpillar/test_bench/junit/redirect_target', response.read())
def test_xhr_post(self): """ XHR POST. """ client = WebClient() self.assert_(client) params = 'foo=bar&msg=test+message' response = client.post(self.junit_url+'/xhr_post',params=params,headers={'xhr': True}) self.assertEqual(200, response.status) xml = response.read() doc = libxml2.parseDoc(xml) for (k,v) in QueryDict(params).items(): n = doc.xpathEval('//'+k) self.assert_(n) self.assertEqual(v,n[0].content)
def test_server_cookies(self): client = WebClient() self.assert_(client) self.assertEqual(0, len(client.cookies)) url = self.junit_url+'/foobarcookies' response = client.post(url) self.assertEqual(200, response.status) # server sends three cookies self.assertEqual(3, len(client.cookies)) # then head to a new url url = self.junit_url+'/foobarcookiestxt' response = client.post(url) self.assertEqual(200, response.status) self.assertEqual('__g00d____yrcl____3ver__',response.read())
def test_post_params(self): """ Test POST + params. """ client = WebClient() self.assert_(client) self.assertEqual(0, len(client.cookies)) url = self.junit_url+'/post_params' params = 'foo=bar&baz=xyz' response = client.post(url,params=params) self.assertEqual(200, response.status) self.assertEqual(0, len(client.cookies)) xml = response.read() doc = libxml2.parseDoc(xml) for (k,v) in QueryDict(params).items(): n = doc.xpathEval('//'+k) self.assert_(n) self.assertEqual(v,n[0].content)
def test_parse_empty_tree(self): ''' Empty tree parse ''' url = self.junit_url+'/empty' portlet = Marionet.objects.create(url=url, session=True) client = WebClient() self.assert_(client) response = client.get(url) self.assertEqual(200, response.status) tree = PageProcessor.parse_tree(response) self.assertEqual(etree._ElementTree, tree.__class__) # test meta data # trigger side effects! tree = PageProcessor.append_metadata(tree,portlet.session) # portlet_tag = tree.find('head/portlet-session') self.assertEqual(portlet_tag.__class__, etree._Element) self.assertEqual(portlet_tag.get('namespace'), portlet.session.get('namespace')) self.assertEqual(portlet_tag.get('baseURL'), self.junit_base)
def test_bench_tree(self): ''' Portlet test bench index ''' url = self.junit_base + '/caterpillar/test_bench' portlet = Marionet.objects.create(url=url, session=True) client = WebClient() self.assert_(client) response = client.get(url) self.assertEqual(200, response.status) tree = PageProcessor.parse_tree(response) self.assertEqual(tree.__class__, etree._ElementTree) # test meta data # trigger side effects! tree = PageProcessor.append_metadata(tree,portlet.session) # portlet_tag = tree.find('//head/portlet-session') self.assertEqual(portlet_tag.__class__, etree._Element) self.assertEqual(portlet_tag.get('namespace'), portlet.session.get('namespace')) self.assertEqual(portlet_tag.get('baseURL'), self.junit_base)
def test_links(self): ''' Link rewrite ''' url = self.junit_url+'/links' portlet = Marionet.objects.create(url=url, session=True) # get test data client = WebClient() self.assert_(client) response = client.get(url) self.assertEqual(200, response.status) portlet.session.set('location', 'http://example.com:8000/some-page') out = PageProcessor.process(response,portlet.session) soup = BeautifulSoup(out) link = soup.find(id='anchor_absolute_url').find('a') self.assertEqual( 'http://example.com:8000/some-page?'+str(portlet.session.get('namespace'))+'.href=http%3A//localhost%3A3000/caterpillar/test_bench', link.get('href') )
def test_process(self): ''' Portlet processing chain ''' url = self.junit_url+'/xslt_simple' portlet = Marionet.objects.create(url=url, session=True) client = WebClient() self.assert_(client) response = client.get(url) self.assertEqual(200, response.status) out = PageProcessor.process(response,portlet.session) self.assert_(out) soup = BeautifulSoup(str(out)) self.assert_(soup) # only body remains self.assertEqual('div', soup.find().name) self.assertEqual(None, soup.find('head')) # namespace is correct portlet_div = soup.find('div', {'class': '%s_body' % portlet.session.get('namespace')}) self.assert_(portlet_div)
def test_post_cookies(self): """ Test POST redirect + preset cookies. """ client = WebClient() self.assert_(client) self.assertEqual(0, len(client.cookies)) url = self.junit_url+'/post_cookies' cookies = {'foo': ['foo=bar'], 'baz': ['baz=xyz']} client.add_cookies(cookies) response = client.post(url) self.assertEqual(200, response.status) # server adds a third cookie at redirect self.assertEqual(3, len(client.cookies)) xml = response.read() doc = libxml2.parseDoc(xml) for _c in cookies.values(): (k,v) = _c[0].split('=') n = doc.xpathEval('//'+k) self.assert_(n) self.assertEqual(v,n[0].content)
def test_sanity(self): """ """ client_1 = WebClient() self.assert_(client_1) self.assertEqual(0, len(client_1.cookies)) cookies = {'foo': ['foo=bar'], 'baz': ['baz=xyz']} client_1.cookies = copy(cookies) self.assertEqual(2, len(client_1.cookies)) client_2 = WebClient() self.assert_(client_2) self.assertEqual(0, len(client_2.cookies)) cookies = { 'foo': ['foo=abc'], 'bar': ['bar=def'], 'baz': ['baz=ghi'], } client_2.cookies = copy(cookies) self.assertEqual(3, len(client_2.cookies)) self.assertEqual(2, len(client_1.cookies))
def test_transform(self): ''' Basic body transformation ''' url = self.junit_url+'/xslt_simple' portlet = Marionet.objects.create(url=url, session=True) client = WebClient() self.assert_(client) response = client.get(url) self.assertEqual(200, response.status) tree = PageProcessor.parse_tree(response) self.assert_(tree) # trigger side effects! tree = PageProcessor.append_metadata(tree,portlet.session) # out = PageProcessor.transform(tree,portlet.session) soup = BeautifulSoup(str(out)) self.assert_(soup) # only body remains self.assertEqual('div', soup.find().name) self.assertEqual(None, soup.find('head')) # namespace is correct portlet_div = soup.find('div', {'class': '%s_body' % portlet.session.get('namespace')}) self.assert_(portlet_div)
def test_css(self): ''' CSS rewrite ''' url = self.junit_url+'/css' portlet = Marionet(url=url, session=True) client = WebClient() self.assert_(client) response = client.get(url) self.assertEqual(200, response.status) out = PageProcessor.process(response,portlet.session) self.assert_(out) #print out soup = BeautifulSoup(str(out)) self.assert_(soup) # absolute url css_url = 'http://localhost:3000/stylesheets/portlet_test_bench/main.css' # search for inline @import url url = re.search('@import "([^"]*)', soup.find(id='absolute_style_url').text).group(1) self.assert_(url) self.assertEqual( css_url, url ) # absolute path url = re.search('@import "([^"]*)', soup.find(id='absolute_style_path').text).group(1) self.assert_(url) self.assert_( re.match( r'^%s?(.*)' % (css_url), url ), '%s ~= %s' % (url,css_url) ) # style_in_body self.assert_(soup.find(id='absolute_style_path').text) # style in attribute self.assert_(soup.find(id='style_attribute')['style'])
def test_get_cookie(self): """ Cookie and session test. """ client = WebClient() self.assert_(client) self.assertEqual(0, len(client.cookies)) session_id = None url = self.junit_url+'/session_cookie' response = client.get(url) self.assertEqual(200, response.status) cookies = copy(client.cookies) self.assertEqual(1, len(cookies)) xml = response.read() doc = libxml2.parseDoc(xml) self.assert_(doc) nodes = doc.xpathEval('//id/text()') self.assertEqual(1, len(nodes)) session_id = nodes[0].content self.assert_(session_id) # GET again and assert that the session remains the same xml = doc = nodes = response = client = None client = WebClient() self.assert_(client) self.assertEqual(0, len(client.cookies)) client.add_cookies(cookies) self.assertEqual(1, len(client.cookies)) response = client.get(url) self.assertEqual(200, response.status) self.assertEqual(1, len(client.cookies)) xml = response.read() doc = libxml2.parseDoc(xml) self.assert_(doc) nodes = doc.xpathEval('//id/text()') self.assertEqual(1, len(nodes)) _session_id = nodes[0].content self.assert_(_session_id) self.assertEqual(session_id,_session_id)