-
-
Notifications
You must be signed in to change notification settings - Fork 8
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 #15 from brokenhandsio/vapor4
Vapor 4
- Loading branch information
Showing
7 changed files
with
223 additions
and
208 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
// swift-tools-version:4.0 | ||
// swift-tools-version:5.2 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "LeafErrorMiddleware", | ||
platforms: [ | ||
.macOS(.v10_15), | ||
], | ||
products: [ | ||
.library(name: "LeafErrorMiddleware", targets: ["LeafErrorMiddleware"]), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"), | ||
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-rc"), | ||
], | ||
targets: [ | ||
.target(name: "LeafErrorMiddleware", dependencies: ["Vapor"]), | ||
.target(name: "LeafErrorMiddleware", dependencies: [ | ||
.product(name: "Vapor", package: "vapor"), | ||
]), | ||
.testTarget(name: "LeafErrorMiddlewareTests", dependencies: ["LeafErrorMiddleware"]), | ||
] | ||
) |
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
25 changes: 17 additions & 8 deletions
25
Tests/LeafErrorMiddlewareTests/Fakes/CapturingLogger.swift
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 |
---|---|---|
@@ -1,14 +1,23 @@ | ||
import Vapor | ||
|
||
class CapturingLogger: Logger, Service { | ||
|
||
var enabled: [LogLevel] = [] | ||
class CapturingLogger: LogHandler { | ||
|
||
subscript(metadataKey key: String) -> Logger.Metadata.Value? { | ||
get { return self.metadata[key] } | ||
set { self.metadata[key] = newValue } | ||
} | ||
|
||
var metadata: Logger.Metadata = [:] | ||
|
||
var logLevel: Logger.Level = .trace | ||
|
||
var enabled: [Logger.Level] = [] | ||
|
||
private(set) var message: String? | ||
private(set) var logLevel: LogLevel? | ||
|
||
func log(_ string: String, at level: LogLevel, file: String, function: String, line: UInt, column: UInt) { | ||
self.message = string | ||
self.logLevel = level | ||
private(set) var logLevelUsed: Logger.Level? | ||
func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) { | ||
self.message = message.description | ||
self.logLevelUsed = level | ||
} | ||
} |
23 changes: 15 additions & 8 deletions
23
Tests/LeafErrorMiddlewareTests/Fakes/ThrowingViewRenderer.swift
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 |
---|---|---|
@@ -1,23 +1,30 @@ | ||
import Vapor | ||
|
||
class ThrowingViewRenderer: ViewRenderer, Service { | ||
class ThrowingViewRenderer: ViewRenderer { | ||
|
||
var shouldCache = false | ||
var worker: Worker | ||
var eventLoop: EventLoop | ||
var shouldThrow = false | ||
|
||
init(worker: Worker) { | ||
self.worker = worker | ||
init(eventLoop: EventLoop) { | ||
self.eventLoop = eventLoop | ||
} | ||
|
||
private(set) var capturedContext: Encodable? = nil | ||
private(set) var leafPath: String? = nil | ||
func render<E>(_ path: String, _ context: E, userInfo: [AnyHashable : Any]) -> EventLoopFuture<View> where E : Encodable { | ||
func render<E>(_ name: String, _ context: E) -> EventLoopFuture<View> where E : Encodable { | ||
self.capturedContext = context | ||
self.leafPath = path | ||
self.leafPath = name | ||
if shouldThrow { | ||
return Future.map(on: worker) { throw TestError() } | ||
return self.eventLoop.makeFailedFuture(TestError()) | ||
} | ||
return Future.map(on: worker) { return View(data: "Test".convertToData()) } | ||
let response = "Test" | ||
var byteBuffer = ByteBufferAllocator().buffer(capacity: response.count) | ||
byteBuffer.writeString(response) | ||
return self.eventLoop.future(View(data: byteBuffer)) | ||
} | ||
|
||
func `for`(_ request: Request) -> ViewRenderer { | ||
return self | ||
} | ||
} |
Oops, something went wrong.