test_fixture.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # https://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. import requests
  13. import requests_mock
  14. from requests_mock.contrib import fixture
  15. from . import base
  16. class MockingTests(base.TestCase):
  17. def setUp(self):
  18. super(MockingTests, self).setUp()
  19. self.mocker = self.useFixture(fixture.Fixture())
  20. def test_failure(self):
  21. self.assertRaises(requests_mock.NoMockAddress,
  22. requests.get,
  23. 'http://www.google.com')
  24. def test_basic(self):
  25. test_url = 'http://www.google.com/'
  26. self.mocker.register_uri('GET', test_url, text='response')
  27. resp = requests.get(test_url)
  28. self.assertEqual('response', resp.text)
  29. self.assertEqual(test_url, self.mocker.last_request.url)
  30. def test_fixture_has_normal_attr_error(self):
  31. self.assertRaises(AttributeError, lambda: self.mocker.unknown)