from unittest import TestCase
from pycurlbrowser import Browser, CannedResponse
from datetime import timedelta

class TestBrowser(TestCase):

    """
    Some rather brittle tests based upon existing websites
    """

    def setUp(self):
        self.browser = Browser()

    def test_visit_duckduckgo(self):
        """Visit a website, check that we get there"""
        self.assertEqual(self.browser.go('duckduckgo.com'), 200)
        self.assertEqual(self.browser.url.lower(), 'http://duckduckgo.com')

    def test_search_duckduckgo(self):
        """Hopefully a search results page will include the search string"""
        search = "a crumble layer that you swirl"
        self.assertEqual(self.browser.go('duckduckgo.com/html'), 200)
        self.browser.form_select(0)
        self.assertTrue('q' in self.browser.form_fields)
        self.browser.form_data_update(q=search)
        self.assertEqual(self.browser.form_submit(), 200)
        print self.browser.src
        self.assertTrue(search in self.browser.src)

    def test_canned_response_duckduckgo(self):
        """Let's pretend that duckduckgo.com's frontpage 404s"""
        # Arrange
        can = CannedResponse()
        can.code = 404
        self.browser.canned_response['duckduckgo.com/html'] = can
        # Act, Assert
        self.assertEqual(self.browser.go('duckduckgo.com/html'), 404)

    def test_canned_content_duckduckgo(self):
        """Let's pretend that duckduckgo.com's frontpage has a silly message"""
        # Arrange
        can = CannedResponse()
        can.src = "Try Google"
        self.browser.canned_response['duckduckgo.com/html'] = can
        # Act
        self.browser.go('duckduckgo.com/html')
        # Assert
        self.assertEqual(self.browser.src, can.src)

    def test_canned_roundtrip_duckduckgo(self):
        """Let's pretend that duckduckgo.com's really slow"""
        # Arrange
        can = CannedResponse()
        can.roundtrip = timedelta(5)
        self.browser.canned_response['duckduckgo.com/html'] = can
        # Act
        self.browser.go('duckduckgo.com/html')
        # Assert
        self.assertEqual(self.browser.roundtrip, can.roundtrip)

    def test_canned_exception_duckduckgo(self):
        """Let's pretend that duckduckgo.com's really slow"""
        # Arrange
        can = CannedResponse()
        self.browser.canned_response['duckduckgo.com/html'] = can
        # Act
        self.browser.go('duckduckgo.com/html')
        # Assert
        self.assertRaises(Exception, self.browser.go('duckduckgo.com/html'))

@@ -2549,8 +2549,1702 @@
 html'))%0A
+%0A    # TODO: should have a way to get canned response per POST of specific data%0A    def test_canned_code_verbs(self):%0A        %22%22%22Different canned response codes for different verbs%22%22%22%0A        # Arrange%0A        url = 'duckduckgo.com/html'%0A%0A        can_post = CannedResponse()%0A        can_post.code = 1%0A        self.browser.add_canned_response(can_post, url, 'POST')%0A%0A        can_get = CannedResponse()%0A        can_get.code = 2%0A        self.browser.add_canned_response(can_get, url, 'GET')%0A%0A        can_put = CannedResponse()%0A        can_put.code = 3%0A        self.browser.add_canned_response(can_put, url, 'PUT')%0A%0A        can_delete = CannedResponse()%0A        can_delete.code = 4%0A        self.browser.add_canned_response(can_delete, url, 'DELETE')%0A%0A        # Act, Assert%0A        self.assertEqual(self.browser.go(url, 'POST'),   1)%0A        self.assertEqual(self.browser.go(url, 'GET'),    2)%0A        self.assertEqual(self.browser.go(url, 'PUT'),    3)%0A        self.assertEqual(self.browser.go(url, 'DELETE'), 4)%0A%0A    def test_canned_code_data(self):%0A        %22%22%22Different canned response codes for different data passed%22%22%22%0A        # Arrange%0A        url = 'duckduckgo.com/html'%0A        method = 'POST'%0A%0A        can_one = CannedResponse()%0A        can_one.code = 1%0A        data_one = dict(foo='bar')%0A        self.browser.add_canned_response(can_one, url, method, data_one)%0A%0A        can_two = CannedResponse()%0A        can_two.code = 2%0A        data_two = dict(bar='foo')%0A        self.browser.add_canned_response(can_two, url, method, data_two)%0A%0A        # Act, Assert%0A        self.assertEqual(self.browser.go(url, method, data_one), 1)%0A        self.assertEqual(self.browser.go(url, method, data_two), 2)%0A

Add some failing tests for new API changes.