Example #1
0
def test_simulate_request_http_version(version, valid):
    app = App()

    if valid:
        testing.simulate_request(app, http_version=version)
    else:
        with pytest.raises(ValueError):
            testing.simulate_request(app, http_version=version)
Example #2
0
def test_content_length_not_set_when_streaming_response(asgi, method):
    class SynthesizedHead:
        def on_get(self, req, resp):
            def words():
                for word in ('Hello', ',', ' ', 'World!'):
                    yield word.encode()

            resp.content_type = falcon.MEDIA_TEXT
            resp.stream = words()

        on_head = on_get

    class SynthesizedHeadAsync:
        async def on_get(self, req, resp):
            # NOTE(kgriffs): Using an iterator in lieu of a generator
            #   makes this code parsable by 3.5 and also tests our support
            #   for iterators vs. generators.
            class Words:
                def __init__(self):
                    self._stream = iter(('Hello', ',', ' ', 'World!'))

                def __aiter__(self):
                    return self

                async def __anext__(self):
                    try:
                        return next(self._stream).encode()
                    except StopIteration:
                        pass  # Test Falcon's PEP 479 support

            resp.content_type = falcon.MEDIA_TEXT
            resp.stream = Words()

        on_head = on_get

    app = create_app(asgi)
    app.add_route('/', SynthesizedHeadAsync() if asgi else SynthesizedHead())

    result = testing.simulate_request(app, method)

    assert result.status_code == 200
    assert result.headers['content-type'] == falcon.MEDIA_TEXT
    assert 'content-length' not in result.headers

    if method == 'GET':
        assert result.text == 'Hello, World!'
Example #3
0
 def test_static_route(self):
     cors_config = CORS(allow_all_origins=True,
                        allow_credentials_all_origins=True,
                        allow_all_methods=True)
     origin = self.get_rand_str()
     headers = {'origin': origin}
     self.resource = CORSResource()
     api = falcon.API(middleware=[cors_config.middleware])
     api.add_static_route("/static", "/var/www/static")
     result = testing.simulate_request(api,
                                       path="/static/image.png",
                                       method="GET",
                                       headers=headers)
     self.assertEqual(result.headers.get('access-control-allow-origin'),
                      origin)
     self.assertEqual(
         result.headers.get('access-control-allow-credentials'), 'true')
Example #4
0
 def test_invalid_url(self):
     result = testing.simulate_request(self.app, 'POST', '/')
     self.assertEqual(result.status, falcon.HTTP_404)
Example #5
0
 def test_invalid_img(self):
     result = testing.simulate_request(self.app, 'POST', '/dummy', params={'image': ''})
     self.assertEqual(result.status, falcon.HTTP_200)
     self.assertFalse(result.json['success'])
     self.assertTrue(result.json['error'].startswith('AttributeError'))
Example #6
0
 def test_empty_post(self):
     result = testing.simulate_request(self.app, 'POST', '/dummy', params={})
     self.assertEqual(result.status, falcon.HTTP_200)
     self.assertFalse(result.json['success'])