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 #4 from valen90/feature/vapor-two
Browse files Browse the repository at this point in the history
Feature/vapor two
  • Loading branch information
steffendsommer authored May 30, 2017
2 parents f6d022c + 0598116 commit 2cc5889
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
os:
- linux
- osx
language: generic
sudo: required
dist: trusty
osx_image: xcode8
script:
- eval "$(curl -sL https://swift.vapor.sh/ci)"
- eval "$(curl -sL https://gist.githubusercontent.com/BrettRToomey/1256d98cce5546c696cc68d9f61ad46f/raw/d5fb266a17f26c41b59fd00867a3a2474c3efee3/TravisSwift31)"
- eval "$(curl -sL https://swift.vapor.sh/codecov)"
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import PackageDescription
let package = Package(
name: "Gatekeeper",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1)
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2)
]
)
12 changes: 10 additions & 2 deletions Sources/RateLimiter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public struct RateLimiter: Middleware {

public func respond(to request: Request, chainingTo next: Responder) throws -> Response {
guard let peer = request.peerAddress?.address() else {
throw Abort.custom(status: .forbidden, message: "Unable to verify peer.")
throw Abort(
.forbidden,
metadata: nil,
reason: "Unable to verify peer."
)
}

var entry = try cache.get(peer)
Expand All @@ -73,7 +77,11 @@ public struct RateLimiter: Middleware {

requestsLeft -= 1
guard requestsLeft >= 0 else {
throw Abort.custom(status: .tooManyRequests, message: "Slow down.")
throw Abort(
.tooManyRequests,
metadata: nil,
reason: "Slow down."
)
}

let response = try next.respond(to: request)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SSLEnforcer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public struct SSLEnforcer: Middleware {
private let error: AbortError

public init(error: AbortError, drop: Droplet, environments: [Environment] = [.production]) {
shouldEnforce = environments.contains(drop.environment)
shouldEnforce = environments.contains(drop.config.environment)
self.error = error
}

Expand Down
41 changes: 31 additions & 10 deletions Tests/GatekeeperTests/GatekeeperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ class GatekeeperTests: XCTestCase {
do {
_ = try middleware.respond(to: request, chainingTo: MockResponder())
XCTAssertTrue(i <= 10, "ran \(i) times.")
} catch Abort.custom(status: let status, message: _) {
XCTAssertEqual(status, .tooManyRequests)
XCTAssertEqual(i, 11, "Should've failed after the 11th attempt.")
break
} catch let error as Abort {
switch error.status {
case .tooManyRequests:
//success
XCTAssertEqual(i, 11, "Should've failed after the 11th attempt.")
break
default:
XCTFail("Expected too many request: \(error)")
}
} catch {
XCTFail("Caught wrong error: \(error)")
}
Expand All @@ -42,8 +47,14 @@ class GatekeeperTests: XCTestCase {

do {
_ = try middleware.respond(to: request, chainingTo: MockResponder())
} catch Abort.custom(status: let status, message: _){
XCTAssertEqual(status, .forbidden)
} catch let error as Abort {
switch error.status {
case .forbidden:
//success
break
default:
XCTFail("Expected forbidden")
}
} catch {
XCTFail("Rate limiter failed: \(error)")
}
Expand Down Expand Up @@ -94,8 +105,14 @@ class GatekeeperTests: XCTestCase {
do {
_ = try middleware.respond(to: request, chainingTo: MockResponder())
XCTFail("Should've been rejected")
} catch Abort.badRequest {
// success
} catch let error as Abort {
switch error.status {
case .badRequest:
// success
break
default:
XCTFail("Expected bad request")
}
} catch {
XCTFail("Request failed: \(error)")
}
Expand Down Expand Up @@ -129,11 +146,15 @@ class GatekeeperTests: XCTestCase {

extension GatekeeperTests {
var developmentDrop: Droplet {
return Droplet(environment: .development)
let config = try! Config()
config.environment = .development
return try! Droplet(config: config)
}

var productionDrop: Droplet {
return Droplet(environment: .production)
let config = try! Config()
config.environment = .production
return try! Droplet(config: config)
}

func getHTTPRequest() -> Request {
Expand Down

0 comments on commit 2cc5889

Please sign in to comment.