【问题标题】:How to test HTTPS in Flask如何在 Flask 中测试 HTTPS
【发布时间】:2017-02-26 14:40:20
【问题描述】:

以下简化代码在我的烧瓶应用中定义。

def select_url_for():
    if request.is_secure:
        return 's3'        
    return 'local'

我尝试过这样的测试,它适用于 HTTP。

def test_select_url_for(self):
    with self.app.test_request_context(): 
        self.assertTrue(select_url_for() == 'local')

如何使用 Flask 对 HTTPS 执行类似的测试?

我找到了this,但问题没有得到解答。我需要在 HTTPS 测试模式下运行 Flask。

【问题讨论】:

    标签: python unit-testing flask https


    【解决方案1】:

    request.is_secure 检查wsgi.url_scheme 环境变量。通过使用https 覆盖它,使request.is_secure 返回True

    def test_select_url_for(self):
        with self.app.test_request_context(environ_overrides={
                'wsgi.url_scheme': 'https'
        }):
            self.assertEqual(select_url_for(), 'local')
    

    顺便说一句,使用 assertEqual(a, b) 而不是 assertTrue(a == b),它会为您提供更具可读性的断言失败消息。

    【讨论】:

      猜你喜欢
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      相关资源
      最近更新 更多