Esempio n. 1
0
    def test_204_no_content_for_configuration_without_response(self):
        mock = pymoq.Mock()
        mock.create_stub('/books/2/chapters')

        with mock.run():
            response = requests.get('http://localhost:8080/books/2/chapters')
            self.assertEqual(response.status_code, 204)
Esempio n. 2
0
    def test_stub_can_be_configured_to_handle_put_method(self):
        mock = pymoq.Mock()
        mock.create_stub('/books/1', method='put')

        with mock.run():
            response = requests.put('http://localhost:8080/books/1')
            self.assertEqual(response.status_code, 204)
Esempio n. 3
0
    def test_stub_can_be_configured_with_regex_pattern(self):
        mock = pymoq.Mock()
        mock.create_stub('/books/[0-9]+/chapters')

        with mock.run():
            response = requests.get('http://localhost:8080/books/234/chapters')
            self.assertEqual(response.status_code, 204)
Esempio n. 4
0
    def test_port_can_be_configured(self):
        mock = pymoq.Mock(port=8090)
        mock.create_stub('/books/2/chapters')

        with mock.run():
            response = requests.get('http://localhost:8090/books/2/chapters')
            self.assertEqual(response.status_code, 204)
Esempio n. 5
0
    def test_content_can_be_empty(self):
        mock = pymoq.Mock()
        path_to_file = path.join(self.dir_path, 'test_content_can_be_empty.json')
        mock.load(path_to_file)

        with mock.run():
            response = requests.get('http://localhost:8080/books')
            self.assertEqual(response.status_code, 204)
Esempio n. 6
0
    def test_stub_can_be_configured_with_http_status(self):
        mock = pymoq.Mock()
        mock.create_stub('/books').response(None, http_status=201)

        with mock.run():
            response = requests.get(
                'http://localhost:8080/books/2/description')
            self.assertEqual(response.status_code, 201)
Esempio n. 7
0
    def test_assert_requested_once_not_fails_for_one_request(self):
        mock = pymoq.Mock()
        stub = mock.create_stub('/books')

        with mock.run():
            requests.get('http://localhost:8080/books')

        stub.assert_requested_once()
Esempio n. 8
0
    def test_assert_requested_times_not_fails_for_valid_count(self):
        mock = pymoq.Mock()
        stub = mock.create_stub('/books')

        with mock.run():
            requests.get('http://localhost:8080/books')
            requests.get('http://localhost:8080/books')

        stub.assert_requested_times(2)
Esempio n. 9
0
    def test_assert_requested_times_fails_if_not_enough_requests(self):
        mock = pymoq.Mock()
        stub = mock.create_stub('/books')

        with mock.run():
            requests.get('http://localhost:8080/books')

        with self.assertRaises(AssertionError):
            stub.assert_requested_times(2)
Esempio n. 10
0
    def test_content_other_than_json(self):
        mock = pymoq.Mock()
        path_to_file = path.join(self.dir_path, 'test_content_other_than_json.json')
        mock.load(path_to_file)

        with mock.run():
            response = requests.get('http://localhost:8080/books')
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.text, 'Lorem ipsum dolor sit amet')
Esempio n. 11
0
    def test_assert_requested_once_fails_if_no_request(self):
        mock = pymoq.Mock()
        mock.create_stub('/books/1')
        stub = mock.create_stub('/books')

        with mock.run():
            requests.get('http://localhost:8080/books/1')

        with self.assertRaises(AssertionError):
            stub.assert_requested_once()
Esempio n. 12
0
    def test_501_if_not_configured(self):
        mock = pymoq.Mock()

        urls = [
            'http://localhost:8080/books/',
            'http://localhost:8080/books/2',
            'http://localhost:8080/books/2/chapters',
        ]

        with mock.run():
            for url in urls:
                response = requests.get(url)
                self.assertEqual(response.status_code, 501)
Esempio n. 13
0
    def test_stub_can_be_configured_with_response_body(self):
        content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'

        mock = pymoq.Mock()
        mock.create_stub('/books/2/description').response(content)

        with mock.run():
            response = requests.get(
                'http://localhost:8080/books/2/description')
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.headers['content-type'],
                             'text/plain; charset=utf-8')
            self.assertEqual(response.encoding, 'utf-8')
            self.assertEqual(response.text, content)
Esempio n. 14
0
    def test_assert_requested_with_header_not_fails_if_any_request_had_valid_header(
            self):
        mock = pymoq.Mock()
        stub = mock.create_stub('/books')

        with mock.run():
            requests.get('http://localhost:8080/books',
                         headers={'X-Test': 'false'})
            requests.get('http://localhost:8080/books',
                         headers={'X-Test': 'true'})
            requests.get('http://localhost:8080/books',
                         headers={'X-Test': 'false'})

        stub.assert_requested_with_header('X-Test', 'true')
Esempio n. 15
0
    def test_assert_requested_with_header_fails_if_no_request_had_valid_header(
            self):
        mock = pymoq.Mock()
        stub = mock.create_stub('/books')

        with mock.run():
            requests.get('http://localhost:8080/books',
                         headers={'X-Test': 'false'})
            requests.get('http://localhost:8080/books',
                         headers={'X-Test': 'false'})
            requests.get('http://localhost:8080/books',
                         headers={'X-Test-Other': 'false'})

        with self.assertRaises(AssertionError):
            stub.assert_requested_with_header('X-Test', 'true')
Esempio n. 16
0
    def test_stub_can_be_configured_with_response_headers(self):
        content = '{"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."}'
        headers = {
            'content-type': 'application/json; charset=utf-8',
            'X-Custom-Header': 'An example'
        }

        mock = pymoq.Mock()
        mock.create_stub('/books/2').response(content, headers=headers)

        with mock.run():
            response = requests.get('http://localhost:8080/books/2')
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.headers['content-type'],
                             'application/json; charset=utf-8')
            self.assertEqual(response.headers['X-Custom-Header'], 'An example')
            self.assertEqual(response.encoding, 'utf-8')
            self.assertEqual(response.text, content)
Esempio n. 17
0
    def test_load_config_from_file(self):
        mock = pymoq.Mock()
        path_to_file = path.join(self.dir_path, 'test_load_config_from_file.json')
        mock.load(path_to_file)

        with mock.run():
            response = requests.post('http://localhost:8080/books',
                                     data={
                                         "author": "John Doe",
                                         "title": "Lorem ipsum dolor sit amet"})
            self.assertEqual(response.status_code, 201)
            self.assertEqual(response.headers['content-type'], 'application/json; charset=utf-8')
            self.assertEqual(response.headers['location'], 'http://localhost:8080/books/1')

            content = response.json()
            self.assertEqual(content['author'], 'John Doe')
            self.assertEqual(content['title'], 'Lorem ipsum dolor sit amet')
            self.assertEqual(content['id'], 1)
Esempio n. 18
0
    def test_assert_requested_body_contains_not_fails_if_any_request_contains_content(
            self):
        mock = pymoq.Mock()
        stub = mock.create_stub('/books', method='post')

        with mock.run():
            requests.post('http://localhost:8080/books',
                          json={
                              'firstName': 'Wolfgang',
                              'lastName': 'Mozart'
                          })
            requests.post('http://localhost:8080/books',
                          json={
                              'firstName': 'Jan',
                              'lastName': 'Bach'
                          })
            requests.post('http://localhost:8080/books',
                          json={
                              'firstName': 'Ludwig',
                              'lastName': 'Beethoven'
                          })

        stub.assert_requested_body_contains('Bach')
Esempio n. 19
0
    def test_direct_usage(self):
        content = '{"author": "John Doe", "title": "Lorem ipsum dolor sit amet", "id": 1}'
        headers = {
            'content-type': 'application/json; charset=utf-8',
            'location': 'http://localhost:8090/books/1'
        }

        mock = pymoq.Mock(port=8090)
        mock.create_stub('/books', method='post').response(content,
                                                           headers=headers,
                                                           http_status=201)

        with mock.run():
            response = requests.post('http://localhost:8090/books',
                                     data={
                                         "author": "John Doe",
                                         "title": "Lorem ipsum dolor sit amet"
                                     })
            self.assertEqual(response.status_code, 201)
            self.assertEqual(response.headers['content-type'],
                             'application/json; charset=utf-8')
            self.assertEqual(response.headers['location'],
                             'http://localhost:8090/books/1')
            self.assertEqual(response.text, content)