在 HTTPRequest 上设置主机名以进行测试
Set hostname on HTTPRequest for testing
在为我的 Vapor 3 应用程序编写测试时,我 运行 遇到了一个问题,即某个框架依赖于检查传入的请求主机名
guard let peerHostName = request.http.remotePeer.hostname else {
throw Abort(
.forbidden,
reason: "Unable to verify peer"
)
}
似乎在测试如下请求时
let emails = (0...10).map { "email@test.co"}
let responder = try app.make(Responder.self)
let request = HTTPRequest(method: .POST, url: URL(string: "\(usersURI)/create")!, headers: headers)
let wrappedRequest = Request(http: request, using: app)
try wrappedRequest.content.encode(createUserReq)
try responder.respond(to: wrappedRequest)
然后请求主机名为空,因此抛出错误。有什么办法可以手动设置请求的主机名吗? hostname
属性 是只获取的,所以我不能那样设置它
解决方案是添加一个 'forwarded' header
var headers: HTTPHeaders = [
"Content-Type": "application/json",
"forwarded": "by=BY;for=127.0.0.1"
]
在为我的 Vapor 3 应用程序编写测试时,我 运行 遇到了一个问题,即某个框架依赖于检查传入的请求主机名
guard let peerHostName = request.http.remotePeer.hostname else {
throw Abort(
.forbidden,
reason: "Unable to verify peer"
)
}
似乎在测试如下请求时
let emails = (0...10).map { "email@test.co"}
let responder = try app.make(Responder.self)
let request = HTTPRequest(method: .POST, url: URL(string: "\(usersURI)/create")!, headers: headers)
let wrappedRequest = Request(http: request, using: app)
try wrappedRequest.content.encode(createUserReq)
try responder.respond(to: wrappedRequest)
然后请求主机名为空,因此抛出错误。有什么办法可以手动设置请求的主机名吗? hostname
属性 是只获取的,所以我不能那样设置它
解决方案是添加一个 'forwarded' header
var headers: HTTPHeaders = [
"Content-Type": "application/json",
"forwarded": "by=BY;for=127.0.0.1"
]