小编And*_*usa的帖子

如何正确使用单元测试的assertRaises()与NoneType对象?

我做了一个简单的测试用例:

def setUp(self):

  self.testListNone = None

def testListSlicing(self):

  self.assertRaises(TypeError, self.testListNone[:1])
Run Code Online (Sandbox Code Playgroud)

我期待测试通过,但我得到例外:

Traceback (most recent call last):

    self.assertRaises(TypeError, self.testListNone[:1])

TypeError: 'NoneType' object is unsubscriptable
Run Code Online (Sandbox Code Playgroud)

我认为assertRaises会通过,因为会引发TypeError异常?

python unit-testing

154
推荐指数
4
解决办法
10万
查看次数

通过cookie实现更安全的Play Scala框架会话的更好方法

我真的很喜欢在用户浏览器上保存会话数据,但不喜欢会话cookie在播放框架中不是很安全的事实.如果有人窃取了cookie,他/她可以使用它来永久访问该网站,因为cookie签名没有到期,并且cookie过期对此没有帮助,因为如果有人偷了它,它不会停止重用cookie.

我已经添加了时间戳,以便在1小时后每隔5分钟使会话到期,如果用户仍在使用该站点,则更新时间戳,以便cookie签名滚动并过期.

我是scala和play框架的新手,所以任何建议或更好的方法来实现相同的将非常感激.

trait Secured {
  def withAuth(f: => String => Request[AnyContent] => Result) = {
    Security.Authenticated(username, onUnauthorized) { user =>
        Action(request => {

          val sessionRolloverPeriod = 300
          val sessionExpiryTime = 3600
          val sessionCreationTime: Int = request.session("ts").toInt
          val currentTime = System.currentTimeMillis() / 1000L

          if(currentTime <= (sessionCreationTime + sessionExpiryTime)) {
            if(currentTime >= (sessionCreationTime + sessionRolloverPeriod)) {
              f(user)(request).withSession(request.session + ("ts" -> (System.currentTimeMillis() / 1000L).toString))
            } else {
              f(user)(request)
            }
          } else {
            Results.Redirect(routes.Auth.login()).withNewSession
          }
        }
      )
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

每5分钟生产一次饼干:

The cookies …
Run Code Online (Sandbox Code Playgroud)

scala playframework-2.0

8
推荐指数
1
解决办法
1364
查看次数

标签 统计

playframework-2.0 ×1

python ×1

scala ×1

unit-testing ×1