Skip to content

Commit

Permalink
Remove TestRequestContextProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Dec 19, 2023
1 parent 70e5372 commit c3e102b
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 143 deletions.
29 changes: 25 additions & 4 deletions Sources/Hummingbird/Server/RequestContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public protocol HBBaseRequestContext: Sendable {
/// Maximum upload size allowed for routes that don't stream the request payload. This
/// limits how much memory would be used for one request
var maxUploadSize: Int { get }
/// initialize a request context
/// - Parameters
/// - eventLoop: EventLoop that created the context
/// - allocator: ByteBuffer allocator
/// - logger: Logger used by context
init(eventLoop: EventLoop, allocator: ByteBufferAllocator, logger: Logger)
}

extension HBBaseRequestContext {
Expand Down Expand Up @@ -137,20 +143,35 @@ public protocol HBRequestContext: HBBaseRequestContext {
init(channel: Channel, logger: Logger)
}

extension HBRequestContext {
/// Initialize an `HBRequestContext`
/// - Parameters:
/// - channel: Source of request context
/// - logger: Logger
public init(channel: Channel, logger: Logger) {
self.init(eventLoop: channel.eventLoop, allocator: channel.allocator, logger: logger)
}
}

/// Implementation of a basic request context that supports everything the Hummingbird library needs
public struct HBBasicRequestContext: HBRequestContext {
/// core context
public var coreContext: HBCoreRequestContext

/// Initialize an `HBRequestContext`
/// - Parameters:
/// - applicationContext: Context from Application that instigated the request
/// - source: Source of request context
/// - eventLoop: EventLoop context was created on
/// - allocator: Allocator
/// - logger: Logger
public init(
channel: Channel,
eventLoop: EventLoop,
allocator: ByteBufferAllocator,
logger: Logger
) {
self.coreContext = .init(eventLoop: channel.eventLoop, allocator: channel.allocator, logger: logger)
self.coreContext = .init(
eventLoop: eventLoop,
allocator: allocator,
logger: logger
)
}
}
2 changes: 1 addition & 1 deletion Sources/HummingbirdXCT/Application+XCT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extension HBApplicationProtocol where Responder.Context: HBRequestContext {
}
}

extension HBApplicationProtocol where Responder.Context: HBTestRequestContextProtocol {
extension HBApplicationProtocol where Responder.Context: HBBaseRequestContext {
// MARK: Initialization

/// Creates a version of `HBApplication` that can be used for testing code
Expand Down
40 changes: 1 addition & 39 deletions Sources/HummingbirdXCT/HBXCTRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,8 @@ import NIOCore
import NIOPosix
import Tracing

public protocol HBTestRequestContextProtocol: HBRequestContext {
init(
eventLoop: EventLoop,
allocator: ByteBufferAllocator,
logger: Logger
)
}

extension HBTestRequestContextProtocol {
public init(
channel: Channel,
logger: Logger
) {
self.init(
eventLoop: channel.eventLoop,
allocator: channel.allocator,
logger: logger
)
}
}

public struct HBTestRouterContext: HBTestRequestContextProtocol {
public init(
eventLoop: EventLoop,
allocator: ByteBufferAllocator,
logger: Logger
) {
self.coreContext = .init(
eventLoop: eventLoop,
allocator: allocator,
logger: logger
)
}

/// router context
public var coreContext: HBCoreRequestContext
}

/// Test sending values to requests to router. This does not setup a live server
struct HBXCTRouter<Responder: HBResponder>: HBXCTApplication where Responder.Context: HBTestRequestContextProtocol {
struct HBXCTRouter<Responder: HBResponder>: HBXCTApplication where Responder.Context: HBBaseRequestContext {
let eventLoopGroup: EventLoopGroup
let responder: Responder
let logger: Logger
Expand Down
6 changes: 3 additions & 3 deletions Sources/PerformanceTest/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import NIOPosix
struct PerformanceTestRequestContext: HBRequestContext {
var coreContext: HBCoreRequestContext

init(channel: Channel, logger: Logger) {
init(eventLoop: EventLoop, allocator: ByteBufferAllocator, logger: Logger) {
self.coreContext = .init(
requestDecoder: JSONDecoder(),
responseEncoder: JSONEncoder(),
eventLoop: channel.eventLoop,
allocator: channel.allocator,
eventLoop: eventLoop,
allocator: allocator,
logger: logger
)
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/HummingbirdFoundationTests/CookiesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CookieTests: XCTestCase {
}

func testSetCookie() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.post("/") { _, _ -> HBResponse in
var response = HBResponse(status: .ok, headers: [:], body: .init())
response.setCookie(.init(name: "test", value: "value"))
Expand All @@ -80,7 +80,7 @@ class CookieTests: XCTestCase {
}

func testSetCookieViaRequest() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.post("/") { _, _ in
return HBEditedResponse(headers: [.setCookie: HBCookie(name: "test", value: "value").description], response: "Hello")
}
Expand All @@ -93,7 +93,7 @@ class CookieTests: XCTestCase {
}

func testReadCookieFromRequest() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.post("/") { request, _ -> String? in
return request.cookies["test"]?.value
}
Expand Down
20 changes: 10 additions & 10 deletions Tests/HummingbirdFoundationTests/FileMiddlewareTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class HummingbirdFilesTests: XCTestCase {
}

func testRead() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBFileMiddleware("."))
let app = HBApplication(responder: router.buildResponder())

Expand All @@ -55,7 +55,7 @@ class HummingbirdFilesTests: XCTestCase {
}

func testReadLargeFile() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBFileMiddleware("."))
let app = HBApplication(responder: router.buildResponder())

Expand All @@ -74,7 +74,7 @@ class HummingbirdFilesTests: XCTestCase {
}

func testReadRange() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBFileMiddleware("."))
let app = HBApplication(responder: router.buildResponder())

Expand Down Expand Up @@ -119,7 +119,7 @@ class HummingbirdFilesTests: XCTestCase {
}

func testIfRangeRead() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBFileMiddleware("."))
let app = HBApplication(responder: router.buildResponder())

Expand Down Expand Up @@ -155,7 +155,7 @@ class HummingbirdFilesTests: XCTestCase {
}

func testHead() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBFileMiddleware("."))
let app = HBApplication(responder: router.buildResponder())

Expand All @@ -179,7 +179,7 @@ class HummingbirdFilesTests: XCTestCase {
}

func testETag() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBFileMiddleware("."))
let app = HBApplication(responder: router.buildResponder())

Expand All @@ -201,7 +201,7 @@ class HummingbirdFilesTests: XCTestCase {
}

func testIfNoneMatch() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBFileMiddleware("."))
let app = HBApplication(responder: router.buildResponder())

Expand Down Expand Up @@ -230,7 +230,7 @@ class HummingbirdFilesTests: XCTestCase {
}

func testIfModifiedSince() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBFileMiddleware("."))
let app = HBApplication(responder: router.buildResponder())

Expand All @@ -256,7 +256,7 @@ class HummingbirdFilesTests: XCTestCase {
}

func testCacheControl() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
let cacheControl: HBCacheControl = .init([
(.text, [.maxAge(60 * 60 * 24 * 30)]),
(.imageJpeg, [.maxAge(60 * 60 * 24 * 30), .private]),
Expand Down Expand Up @@ -284,7 +284,7 @@ class HummingbirdFilesTests: XCTestCase {
}

func testIndexHtml() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBFileMiddleware(".", searchForIndexHtml: true))
let app = HBApplication(responder: router.buildResponder())

Expand Down
6 changes: 3 additions & 3 deletions Tests/HummingbirdFoundationTests/HummingBirdJSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class HummingbirdJSONTests: XCTestCase {
struct Error: Swift.Error {}

func testDecode() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBSetCodableMiddleware(decoder: JSONDecoder(), encoder: JSONEncoder()))
router.put("/user") { request, context -> HTTPResponse.Status in
guard let user = try? await request.decode(as: User.self, using: context) else { throw HBHTTPError(.badRequest) }
Expand All @@ -46,7 +46,7 @@ class HummingbirdJSONTests: XCTestCase {
}

func testEncode() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBSetCodableMiddleware(decoder: JSONDecoder(), encoder: JSONEncoder()))
router.get("/user") { _, _ -> User in
return User(name: "John Smith", email: "john.smith@email.com", age: 25)
Expand All @@ -64,7 +64,7 @@ class HummingbirdJSONTests: XCTestCase {
}

func testEncode2() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBSetCodableMiddleware(decoder: JSONDecoder(), encoder: JSONEncoder()))
router.get("/json") { _, _ in
return ["message": "Hello, world!"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class HummingBirdURLEncodedTests: XCTestCase {
struct Error: Swift.Error {}

func testDecode() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBSetCodableMiddleware(decoder: URLEncodedFormDecoder(), encoder: URLEncodedFormEncoder()))
router.put("/user") { request, context -> HTTPResponse.Status in
guard let user = try? await request.decode(as: User.self, using: context) else { throw HBHTTPError(.badRequest) }
Expand All @@ -46,7 +46,7 @@ class HummingBirdURLEncodedTests: XCTestCase {
}

func testEncode() async throws {
let router = HBRouter(context: HBTestRouterContext.self)
let router = HBRouter()
router.middlewares.add(HBSetCodableMiddleware(decoder: URLEncodedFormDecoder(), encoder: URLEncodedFormEncoder()))
router.get("/user") { _, _ -> User in
return User(name: "John Smith", email: "john.smith@email.com", age: 25)
Expand Down
Loading

0 comments on commit c3e102b

Please sign in to comment.