def test_connection_error(self):
        e = requests.exceptions.ConnectionError('mock connection error!')

        with self.flask.test_request_context('/test'):
            responses.add(responses.POST, render_server.url, body=e)

            with pytest.raises(RenderServerError):
                render_server.render('/path')
    def test_render_server_success(self, get_csrf_token, hashlib):
        get_csrf_token.return_value = 'abc123'

        with self.flask.test_request_context('/test'):
            sha = sha1()
            hashlib.sha1.return_value = sha

            markup = 'hello world!'
            path = '/widget/component.js'
            params = {'hash': sha.hexdigest()}

            responses.add(responses.POST, render_server.url, json={'markup': markup})

            result = render_server.render(path)
            assert result.render() == markup

            assert len(responses.calls) == 1
            req = responses.calls[0].request

            assert req.url == self.config.REACT_RENDER_URL + '?' + urls.urlencode(params)
            assert req.headers['content-type'] == 'application/json'
            assert req.body == '{"path": "' + path + '", ''"serializedProps": "{\\"_serverContext\\": ' \
                '{\\"location\\": \\"/test\\"}, \\"form_options\\": {\\"csrf_token\\": \\"abc123\\"}, ' \
                '\\"options\\": ' \
                '{\\"apiUrl\\": \\"http://api\\", \\"serverRender\\": true}}", ' \
                '"toStaticMarkup": false}'
    def test_react_render_not_set(self, get_csrf_token):
        get_csrf_token.return_value = 'abc123'

        self.flask.config.update({'REACT_RENDER': None})

        with self.flask.test_request_context('/test'):
            responses.add(responses.POST, render_server.url, json={})

            result = render_server.render('/widget/component.js')
            assert result.render() == ''
            assert result.get_props() == '{"_serverContext": ' \
                                         '{"location": "/test"}, "form_options": {"csrf_token": "abc123"}, ' \
                                         '"options": {"apiUrl": "http://api", ' \
                                         '"serverRender": true}}'
    def test_render_error(self,):
        with self.flask.test_request_context('/test'):
            responses.add(responses.POST, render_server.url, json={'error': 'an error'})

            with pytest.raises(ReactRenderingError):
                render_server.render('/path')
    def test_no_markup(self):
        with self.flask.test_request_context('/test'):
            responses.add(responses.POST, render_server.url, json={'markup': None})

            with pytest.raises(ReactRenderingError):
                render_server.render('/path')
    def test_non_200_status_code(self):
        with self.flask.test_request_context('/test'):
            responses.add(responses.POST, render_server.url, status=400)

            with pytest.raises(RenderServerError):
                render_server.render('/path')