This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from nodes-vapor/feature/use-new-error-protocol
Feature/use new error protocol
- Loading branch information
Showing
9 changed files
with
256 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import XCTest | ||
@testable import Vapor | ||
@testable import Bugsnag | ||
import HTTP | ||
|
||
class BugsnagMiddlewareTests: XCTestCase { | ||
private var connectionManager: ConnectionManagerMock! | ||
private var middleware: BugsnagMiddleware! | ||
|
||
static var allTests : [(String, (BugsnagMiddlewareTests) -> () throws -> Void)] { | ||
return [ | ||
("testThatErrorsNotConformingToAbortErrorAreRethrown", testThatErrorsNotConformingToAbortErrorAreRethrown), | ||
("testThatAbortErrorsAreRethrown", testThatAbortErrorsAreRethrown), | ||
("testErrorNotConformingToAbortErrorWillBeReported", testErrorNotConformingToAbortErrorWillBeReported), | ||
("testBadRequestAbortErrorWillBeReported", testBadRequestAbortErrorWillBeReported), | ||
("testCustomErrorWillBeReported", testCustomErrorWillBeReported), | ||
("testErrorNotReportedWhenExplicitlyToldNotTo", testErrorNotReportedWhenExplicitlyToldNotTo), | ||
("testErrorReportedWhenExplicitlyToldSo", testErrorReportedWhenExplicitlyToldSo) | ||
] | ||
} | ||
|
||
override func setUp() { | ||
let drop = Droplet() | ||
let config = ConfigurationMock() | ||
self.connectionManager = ConnectionManagerMock(drop: drop, config: config) | ||
self.middleware = BugsnagMiddleware(connectionManager: connectionManager) | ||
} | ||
|
||
override func tearDown() { | ||
self.connectionManager = nil | ||
self.middleware = nil | ||
} | ||
|
||
|
||
// MARK: Rethrowing. | ||
|
||
func testThatErrorsNotConformingToAbortErrorAreRethrown() { | ||
let next = ErrorResponderMock(error: MyCustomError()) | ||
let req = try? Request(method: .get, uri: "some-random-uri") | ||
|
||
do { | ||
_ = try middleware.respond(to: req!, chainingTo: next) | ||
XCTFail("Error not conforming to AbortError wasn't rethrown.") | ||
} catch {} | ||
} | ||
|
||
func testThatAbortErrorsAreRethrown() { | ||
let next = ErrorResponderMock(error: Abort.badRequest) | ||
let req = try? Request(method: .get, uri: "some-random-uri") | ||
|
||
do { | ||
_ = try middleware.respond(to: req!, chainingTo: next) | ||
XCTFail("Error conforming to AbortError wasn't rethrown.") | ||
} catch {} | ||
} | ||
|
||
|
||
// MARK: Automatic reporting. | ||
|
||
func testErrorNotConformingToAbortErrorWillBeReported() { | ||
let error = MyCustomError() | ||
let next = ErrorResponderMock(error: error) | ||
let req = try? Request(method: .get, uri: "some-random-uri") | ||
_ = try? middleware.respond(to: req!, chainingTo: next) | ||
|
||
XCTAssertEqual(connectionManager.lastPost!.status, .internalServerError) | ||
XCTAssertEqual(connectionManager.lastPost!.message, Status.internalServerError.reasonPhrase) | ||
XCTAssertEqual(connectionManager.lastPost!.metadata, nil) | ||
XCTAssertEqual(connectionManager.lastPost!.request.uri.description, req!.uri.description) | ||
} | ||
|
||
func testBadRequestAbortErrorWillBeReported() { | ||
let next = ErrorResponderMock(error: Abort.badRequest) | ||
let req = try? Request(method: .get, uri: "some-random-uri") | ||
_ = try? middleware.respond(to: req!, chainingTo: next) | ||
|
||
XCTAssertEqual(connectionManager.lastPost!.status, .badRequest) | ||
XCTAssertEqual(connectionManager.lastPost!.message, Abort.badRequest.message) | ||
XCTAssertEqual(connectionManager.lastPost!.metadata, nil) | ||
XCTAssertEqual(connectionManager.lastPost!.request.uri.description, req!.uri.description) | ||
} | ||
|
||
func testCustomErrorWillBeReported() { | ||
let message = "My custom error" | ||
let code = 1337 | ||
let status = Status.conflict | ||
let metadata: Node? = Node([ | ||
"key1": "value1", | ||
"key2": "value2" | ||
]) | ||
let error = MyCustomAbortError(message: message, code: code, status: status, metadata: metadata) | ||
|
||
let next = ErrorResponderMock(error: error) | ||
let req = try? Request(method: .get, uri: "some-random-uri") | ||
_ = try? middleware.respond(to: req!, chainingTo: next) | ||
|
||
XCTAssertEqual(connectionManager.lastPost!.status, error.status) | ||
XCTAssertEqual(connectionManager.lastPost!.message, error.message) | ||
XCTAssertEqual(connectionManager.lastPost!.metadata, error.metadata) | ||
XCTAssertEqual(connectionManager.lastPost!.request.uri.description, req!.uri.description) | ||
} | ||
|
||
|
||
// MARK: Manual reporting. | ||
|
||
func testErrorNotReportedWhenExplicitlyToldNotTo() { | ||
let error = MyCustomAbortError( | ||
message: "", | ||
code: 0, | ||
status: .accepted, | ||
metadata: Node(["report": false]) | ||
) | ||
|
||
let next = ErrorResponderMock(error: error) | ||
let req = try? Request(method: .get, uri: "some-random-uri") | ||
_ = try? middleware.respond(to: req!, chainingTo: next) | ||
|
||
XCTAssertNil(connectionManager.lastPost) | ||
} | ||
|
||
func testErrorReportedWhenExplicitlyToldSo() { | ||
let error = MyCustomAbortError( | ||
message: "", | ||
code: 0, | ||
status: .accepted, | ||
metadata: Node(["report": true]) | ||
) | ||
|
||
let next = ErrorResponderMock(error: error) | ||
let req = try? Request(method: .get, uri: "some-random-uri") | ||
_ = try? middleware.respond(to: req!, chainingTo: next) | ||
|
||
XCTAssertNotNil(connectionManager.lastPost) | ||
} | ||
} | ||
|
||
|
||
// MARK: - Misc. | ||
|
||
private struct MyCustomError: Error {} | ||
|
||
private struct MyCustomAbortError: AbortError { | ||
let message: String | ||
let code: Int | ||
let status: Status | ||
let metadata: Node? | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Vapor | ||
import Bugsnag | ||
|
||
internal class ConfigurationMock: ConfigurationType { | ||
let apiKey = "" | ||
let notifyReleaseStages: [String] = [] | ||
let endpoint = "" | ||
let filters: [String] = [] | ||
|
||
required init(drop: Droplet) throws {} | ||
init() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Vapor | ||
import Bugsnag | ||
import HTTP | ||
|
||
internal class ConnectionManagerMock: ConnectionManagerType { | ||
let drop: Droplet | ||
let config: ConfigurationType | ||
var lastPost: (status: Status, message: String, metadata: Node?, request: Request)? = nil | ||
|
||
required init(drop: Droplet, config: ConfigurationType) { | ||
self.drop = drop | ||
self.config = config | ||
} | ||
|
||
func post(status: Status, message: String, metadata: Node?, request: Request) throws -> Status { | ||
self.lastPost = (status, message, metadata, request) | ||
return Status.accepted // Just some random status. | ||
} | ||
} |
Oops, something went wrong.