Example #1
0
    def test_flow_httplib(self):
        """Verify that cassette works when using httplib directly."""

        # First run
        with cassette.play(self.filename, file_format=self.file_format):
            conn = httplib.HTTPConnection("127.0.0.1", 5000)
            conn.request("GET", "/index")
            r = conn.getresponse()
            conn.close()

        self.assertEqual(r.status, 200)
        self.assertEqual(r.reason, "OK")
        self.assertEqual(r.read(), "hello world")
        self.assertEqual(self.had_response.called, False)

        self.had_response.reset_mock()

        # Second run
        with cassette.play(self.filename, file_format=self.file_format):
            conn = httplib.HTTPConnection("127.0.0.1", 5000)
            conn.request("GET", "/index")
            r = conn.getresponse()
            conn.close()

        self.assertEqual(r.status, 200)
        self.assertEqual(r.reason, "OK")
        self.assertEqual(r.read(), "hello world")
        self.assertEqual(self.had_response.called, True)
Example #2
0
    def test_flow_httplib(self):
        """Verify that cassette works when using httplib directly."""

        # First run
        with cassette.play(self.filename, file_format=self.file_format):
            conn = httplib.HTTPConnection("127.0.0.1", 5000)
            conn.request("GET", "/index")
            r = conn.getresponse()
            conn.close()

        self.assertEqual(r.status, 200)
        self.assertEqual(r.reason, "OK")
        self.assertEqual(r.read(), "hello world")
        self.assertEqual(self.had_response.called, False)

        self.had_response.reset_mock()

        # Second run
        with cassette.play(self.filename, file_format=self.file_format):
            conn = httplib.HTTPConnection("127.0.0.1", 5000)
            conn.request("GET", "/index")
            r = conn.getresponse()
            conn.close()

        self.assertEqual(r.status, 200)
        self.assertEqual(r.reason, "OK")
        self.assertEqual(r.read(), "hello world")
        self.assertEqual(self.had_response.called, True)
Example #3
0
    def test_flow_404(self):
        """Verify that cassette can returns 404 from file."""

        # First run
        with cassette.play(self.filename, file_format=self.file_format):
            self.assertRaises(urllib2.HTTPError, urllib2.urlopen, TEST_URL_404)

        self.assertEqual(self.had_response.called, False)

        # Second run, it has the response.
        with cassette.play(self.filename, file_format=self.file_format):
            self.assertRaises(urllib2.HTTPError, urllib2.urlopen, TEST_URL_404)

        self.assertEqual(self.had_response.called, True)
Example #4
0
    def test_flow_404(self):
        """Verify that cassette can returns 404 from file."""

        # First run
        with cassette.play(self.filename, file_format=self.file_format):
            self.assertRaises(urllib2.HTTPError, urllib2.urlopen, TEST_URL_404)

        self.assertEqual(self.had_response.called, False)

        # Second run, it has the response.
        with cassette.play(self.filename, file_format=self.file_format):
            self.assertRaises(urllib2.HTTPError, urllib2.urlopen, TEST_URL_404)

        self.assertEqual(self.had_response.called, True)
Example #5
0
    def test_flow_404(self):
        """Verify that cassette can returns 404 from file."""

        # First run
        with cassette.play(TEMPORARY_RESPONSES_FILENAME):
            self.assertRaises(urllib2.HTTPError, urllib2.urlopen, TEST_URL_404)

        self.assertEqual(self.had_response.called, False)

        # Second run, it has the response.
        with cassette.play(TEMPORARY_RESPONSES_FILENAME):
            self.assertRaises(urllib2.HTTPError, urllib2.urlopen, TEST_URL_404)

        self.assertEqual(self.had_response.called, True)
Example #6
0
    def helper_requestslib(self, url):
        with mock.patch.object(CassetteLibrary, '_had_response', autospec=True) as was_cached:
            with cassette.play(self.filename, file_format=self.file_format):
                r0 = requests.get(url)

            assert not was_cached.called

        with mock.patch.object(CassetteLibrary, '_had_response', autospec=True) as was_cached:
            with cassette.play(self.filename, file_format=self.file_format):
                r1 = requests.get(url)

                assert was_cached.called

        assert r0.text == r1.text
        return r1
Example #7
0
    def test_httplib_getheader_with_present_header(self):
        with cassette.play(RESPONSES_FILENAME):
            conn = httplib.HTTPConnection("127.0.0.1", 5000)
            conn.request("GET", "/index")
            r = conn.getresponse()

        assert r.getheader('content-length')
Example #8
0
    def test_httplib_getheader_with_absent_header(self):
        with cassette.play(self.responses_filename):
            conn = httplib.HTTPConnection("127.0.0.1", 5000)
            conn.request("GET", "/index")
            r = conn.getresponse()

        assert r.getheader('X-FOOBAR') is None
Example #9
0
    def test_httplib_getheader_with_absent_header(self):
        with cassette.play(self.responses_filename):
            conn = httplib.HTTPConnection("127.0.0.1", 5000)
            conn.request("GET", "/index")
            r = conn.getresponse()

        assert r.getheader('X-FOOBAR') is None
Example #10
0
    def helper_requestslib(self, url):
        with mock.patch.object(CassetteLibrary, '_had_response',
                               autospec=True) as was_cached:
            with cassette.play(self.filename, file_format=self.file_format):
                r0 = requests.get(url)

            assert not was_cached.called

        with mock.patch.object(CassetteLibrary, '_had_response',
                               autospec=True) as was_cached:
            with cassette.play(self.filename, file_format=self.file_format):
                r1 = requests.get(url)

                assert was_cached.called

        assert r0.text == r1.text
        return r1
    def test_read_file(self):
        """Verify that cassette can read a file."""

        with cassette.play(RESPONSES_FILENAME):
            r = urllib2.urlopen("http://www.internic.net/domain/named.root")

        self.assertFalse(self.mocked_urlopen.called)
        self.assertIn("A.ROOT-SERVERS.NET", r.read(100000))
    def test_flow(self):
        """Verify the cassette behavior."""

        # First run
        with cassette.play(TEMPORARY_RESPONSES_FILENAME):
            r = urllib2.urlopen("http://www.internic.net/domain/named.root")

        self.assertTrue(self.mocked_urlopen.called)
        self.assertIn("A.ROOT-SERVERS.NET", r.read(100000))

        self.mocked_urlopen.reset_mock()

        # Second run
        with cassette.play(TEMPORARY_RESPONSES_FILENAME):
            r = urllib2.urlopen("http://www.internic.net/domain/named.root")

        self.assertFalse(self.mocked_urlopen.called)
        self.assertIn("A.ROOT-SERVERS.NET", r.read(100000))
Example #13
0
    def check_urllib2_flow(self,
                           url,
                           expected_content=None,
                           allow_incomplete_match=False,
                           data=None):
        """Verify the urllib2 flow."""

        if not url.startswith("http"):
            url = url_for(url)

        # First run
        with cassette.play(self.filename, file_format=self.file_format):
            r = urllib2.urlopen(url, data)  # 1st run

        self.assertEqual(self.had_response.called, False)
        if expected_content:
            content = unicode(r.read(), "utf-8")
            if allow_incomplete_match:
                self.assertIn(expected_content, content)
            else:
                self.assertEqual(content, expected_content)

        self.had_response.reset_mock()

        # Second run
        with cassette.play(self.filename, file_format=self.file_format):
            r = urllib2.urlopen(url, data)  # 2nd run

        self.assertEqual(self.had_response.called, True)
        if expected_content:
            content = unicode(r.read(), "utf-8")
            if allow_incomplete_match:
                self.assertIn(expected_content, content)
            else:
                self.assertEqual(content, expected_content)

        if r.headers["Content-Type"] == "application/json":
            try:
                r.json = json.loads(r.read())
            except ValueError:
                pass

        return r
Example #14
0
    def test_read_twice(self):
        """Verify that response are not empty."""

        url = TEST_URL
        expected_content = "hello world"

        with cassette.play(self.responses_filename):
            r = urllib2.urlopen(url)
            self.assertEqual(r.read(), expected_content)
            r = urllib2.urlopen(url)
            self.assertEqual(r.read(), expected_content)
Example #15
0
    def test_read_twice(self):
        """Verify that response are not empty."""

        url = TEST_URL
        expected_content = "hello world"

        with cassette.play(self.responses_filename):
            r = urllib2.urlopen(url)
            self.assertEqual(r.read(), expected_content)
            r = urllib2.urlopen(url)
            self.assertEqual(r.read(), expected_content)
Example #16
0
    def check_urllib2_flow(self, url, expected_content=None,
                           allow_incomplete_match=False,
                           data=None):
        """Verify the urllib2 flow."""

        if not url.startswith("http"):
            url = url_for(url)

        # First run
        with cassette.play(self.filename, file_format=self.file_format):
            r = urllib2.urlopen(url, data)  # 1st run

        self.assertEqual(self.had_response.called, False)
        if expected_content:
            content = unicode(r.read(), "utf-8")
            if allow_incomplete_match:
                self.assertIn(expected_content, content)
            else:
                self.assertEqual(content, expected_content)

        self.had_response.reset_mock()

        # Second run
        with cassette.play(self.filename, file_format=self.file_format):
            r = urllib2.urlopen(url, data)  # 2nd run

        self.assertEqual(self.had_response.called, True)
        if expected_content:
            content = unicode(r.read(), "utf-8")
            if allow_incomplete_match:
                self.assertIn(expected_content, content)
            else:
                self.assertEqual(content, expected_content)

        if r.headers["Content-Type"] == "application/json":
            try:
                r.json = json.loads(r.read())
            except ValueError:
                pass

        return r
Example #17
0
    def test_flow_httplib(self):
        """Verify that cassette can read a file when using httplib."""

        with cassette.play(self.responses_filename):
            conn = httplib.HTTPConnection("127.0.0.1", 5000)
            conn.request("GET", "/index")
            r = conn.getresponse()

        self.assertEqual(r.status, 200)
        self.assertEqual(r.reason, "OK")
        self.assertEqual(r.read(), "hello world")
        self.assertEqual(self.had_response.called, True)
Example #18
0
    def test_flow_httplib(self):
        """Verify that cassette can read a file when using httplib."""

        with cassette.play(self.responses_filename):
            conn = httplib.HTTPConnection("127.0.0.1", 5000)
            conn.request("GET", "/index")
            r = conn.getresponse()

        self.assertEqual(r.status, 200)
        self.assertEqual(r.reason, "OK")
        self.assertEqual(r.read(), "hello world")
        self.assertEqual(self.had_response.called, True)
Example #19
0
    def check_read_from_file_flow(self, url, expected_content,
                                  allow_incomplete_match=False):
        """Verify the flow when reading from an existing file."""

        with cassette.play(RESPONSES_FILENAME):
            r = urllib2.urlopen(url)

        content = unicode(r.read(), "utf-8")
        if allow_incomplete_match:
            self.assertIn(expected_content, content)
        else:
            self.assertEqual(content, expected_content)
        self.assertEqual(self.had_response.called, True)

        return r
Example #20
0
    def check_read_from_file_flow(self, url, expected_content,
                                  allow_incomplete_match=False,
                                  request_headers=None):
        """Verify the flow when reading from an existing file."""

        with cassette.play(RESPONSES_FILENAME):
            request = urllib2.Request(url, headers=request_headers or {})
            r = urllib2.urlopen(request)

        content = unicode(r.read(), "utf-8")
        if allow_incomplete_match:
            self.assertIn(expected_content, content)
        else:
            self.assertEqual(content, expected_content)
        self.assertEqual(self.had_response.called, True)

        return r
Example #21
0
    def check_read_from_file_flow(self, url, expected_content,
                                  allow_incomplete_match=False,
                                  request_headers=None):
        """Verify the flow when reading from an existing file."""

        with cassette.play(self.responses_filename):
            request = urllib2.Request(url, headers=request_headers or {})
            r = urllib2.urlopen(request)

        content = unicode(r.read(), "utf-8")
        if allow_incomplete_match:
            self.assertIn(expected_content, content)
        else:
            self.assertEqual(content, expected_content)
        self.assertEqual(self.had_response.called, True)

        return r
Example #22
0
 def test_with_cassette(self):
     "This really turns it into a unit test, not an integration test."
     with cassette.play("calc/tests/integration/responses.yaml"):
         self.app.get('/execute/add?number=2&numbers=3')