【问题标题】:How Does Testing In Vapor 4 Incorporate Sessions?Vapor 4 中的测试如何包含会话?
【发布时间】:2021-09-01 22:46:34
【问题描述】:

我在 Vapor 中对 http/html(不是 JSON)服务器进行了一系列测试。在用户数据库中创建一个用户,然后使用 POST 请求登录,以确认该用户存在,并得到肯定的响应:

2021-06-17T13:17:43+0700 info codes.vapor.request : request-id=97BBFF64-52B8-4988-BA79-236A43C0212F POST /test/login/
logged in stressed_tangerine_hedgehog@sillyname.org

如果我使用正在运行的服务器手动执行相同的操作,在开发数据库上,我会得到相同的肯定结果,并且 cookie 在那里:(使用迅雷客户端):

如果我然后点击一个测试端点,一个简单的 get 指示是否存在会话用户,也使用 ThunderClient,用户存在。

但是,在测试环境中,当我跟随登录请求到同一个端点时,我没有得到任何用户:

2021-06-17T13:17:43+0700 info codes.vapor.request : request-id=5D8FA5FC-B176-4355-9A37-FAE01772A1C6 GET /test/
not logged in

测试端点如下所示:

   func getTestHandler(req: Request) -> String {
        guard let user = req.auth.get(User.self) else {
            print("not logged in")
            return "not logged in"
        }
        print(user.username)
        return "got the handler"
    }

…在迅雷客户端中返回的消息是正确的。

Vapor 测试环境中的 XCTAssert 测试是否没有实现任何形式的会话 cookie,如果没有,是否有解决方法?如果是这样,我该怎么做才能让它们工作?

【问题讨论】:

标签: swift testing vapor


【解决方案1】:

您有责任捕获登录响应中设置的 cookie,并将其与测试中的后续请求一起发送

【讨论】:

    【解决方案2】:

    第一个方法是当您需要在测试中访问登录路由时调用的方法。 loggedInUserpasswordToLoginWith 是要进行身份验证的用户。 body 提供了最终调用所需的上下文。 setLoginCookie 中的 loginPath 是您的身份验证端点。其余的应该是不言自明的。

    extension UserTests {
        
        func getResponse<T: Content>(to path: String,
                            method: HTTPMethod = .POST,
                            body: T,
                            loggedInUser: User? = nil,
                            passwordToLoginWith: String? = nil,
                            headers: HTTPHeaders = .init()) throws -> Response {
            let request = try setupRequest(to: path,
                            method: method,
                            loggedInUser: loggedInUser,
                            passwordToLoginWith: passwordToLoginWith,
                            headers: headers)
            
            try request.content.encode(body)
            return try getResponse(to: request)
        }
        
        
        func getResponse(to path: String,
                         method: HTTPMethod = .GET,
                         headers: HTTPHeaders = .init(),
                         loggedInUser: User? = nil) throws -> Response {
            let request = try setupRequest(to: path, method: method, loggedInUser: loggedInUser, passwordToLoginWith: stdPass, headers: headers)
            return try getResponse(to: request)
        }
        
        func setupRequest(to path: String,
                          method: HTTPMethod = .POST,
                          loggedInUser: User? = nil,
                          passwordToLoginWith: String? = nil,
                          headers: HTTPHeaders = .init()) throws -> Request {
                let request = Request(application: app,
                          method: method,
                          url: URI(path: path),
                          headers: headers, on:app.eventLoopGroup.next())
                request.cookies["vapor-session"] = try setLoginCookie(for: loggedInUser, password: passwordToLoginWith)
                return request
        }
        
        func setLoginCookie(for user: User?, password: String? = nil) throws -> HTTPCookies.Value? {
            if let user = user {
                let loginData = TestController.LoginContext(username: user.username, password: password!, email: user.username)
                let loginPath = "/test/login"
    
                let loginResponse = try getResponse(to: loginPath, method: .POST, body: loginData)
                let sessionCookie = loginResponse.cookies["vapor-session"]
                return sessionCookie
            } else {
                return nil
            }
        }
    
        func getResponse(to request: Request) throws -> Response {
            return try app.responder.respond(to: request).wait()
        }
    }
    

    这或多或少可以在 Github 项目中找到,链接由 0xTim 提供。这真的很有用(直到它消失在 Vapor Discord 服务器的流程中;Vapor 的社区损失了很多,因为社区问题和答案的短暂方式消失在 Discord 的黑嘴中。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-04
      • 1970-01-01
      • 2014-03-25
      • 2021-10-24
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多