Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #22 from NeedleInAJayStack/feature/forwardedHeader
Browse files Browse the repository at this point in the history
Adds hostname extraction for ‘forwarded’ header
  • Loading branch information
siemensikkema committed Sep 3, 2021
2 parents 1bf4692 + f5a52f6 commit 9009cfb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Gatekeeper/Request+Hostname.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Vapor

extension Request {
var hostname: String? {
headers.first(name: .xForwardedFor) ?? remoteAddress?.hostname
headers.forwarded.first?.for ?? headers.first(name: .xForwardedFor) ?? remoteAddress?.hostname
}
}
21 changes: 21 additions & 0 deletions Tests/GatekeeperTests/GatekeeperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ class GatekeeperTests: XCTestCase {
})
}

func testGateKeeperForwardedSupported() throws {
let app = Application(.testing)
defer { app.shutdown() }
app.gatekeeper.config = .init(maxRequests: 10, per: .second)

app.grouped(GatekeeperMiddleware()).get("test") { req -> HTTPStatus in
return .ok
}

try app.test(
.GET,
"test",
beforeRequest: { req in
req.headers.forwarded = [HTTPHeaders.Forwarded(for: "\"[::1]\"")]
},
afterResponse: { res in
XCTAssertEqual(res.status, .ok)
}
)
}

func testGateKeeperCountRefresh() throws {
let app = Application(.testing)
defer { app.shutdown() }
Expand Down

0 comments on commit 9009cfb

Please sign in to comment.