Created
July 23, 2023 15:41
-
-
Save ptoffy/4a1f76fa0ed73455e8a8e1a8cb07a566 to your computer and use it in GitHub Desktop.
SwiftNIO Dummy Test
This file contains hidden or 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
class DummyTests: XCTestCase { | |
var eventLoopGroup: EventLoopGroup! | |
var serverChannel: Channel? | |
var serverAddress: SocketAddress? | |
override func setUpWithError() throws { | |
eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) | |
try startTestServer() | |
} | |
override func tearDownWithError() throws { | |
try stopTestServer() | |
try eventLoopGroup.syncShutdownGracefully() | |
} | |
func startTestServer() throws { | |
let bootstrap = ServerBootstrap(group: eventLoopGroup) | |
.serverChannelOption(ChannelOptions.backlog, value: 256) | |
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) | |
.childChannelInitializer { channel in | |
channel.pipeline.configureHTTPServerPipeline().flatMap { | |
channel.pipeline.addHandler(DummyHandler()) | |
} | |
} | |
serverChannel = try bootstrap.bind(host: "localhost", port: 8088).wait() | |
serverAddress = serverChannel!.localAddress | |
} | |
func stopTestServer() throws { | |
try serverChannel?.close().wait() | |
} | |
func testDoPingServer() async throws { | |
// Assuming the serverName variable in your function is set to the test server address | |
let serverName = "http://localhost:8088" | |
let isServerRunning = try await doPingServer() | |
XCTAssertTrue(isServerRunning, "Server should be running and respond with 'pong'") | |
} | |
} | |
class DummyHandler: ChannelInboundHandler { | |
typealias InboundIn = HTTPServerRequestPart | |
typealias OutboundOut = HTTPServerResponsePart | |
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | |
let requestPart = unwrapInboundIn(data) | |
switch requestPart { | |
case .head(let header): | |
let response = createResponse(for: header.uri) | |
let head = HTTPResponseHead(version: header.version, status: response.status, headers: response.headers) | |
context.write(self.wrapOutboundOut(.head(head)), promise: nil) | |
var buffer = context.channel.allocator.buffer(capacity: response.body.count) | |
buffer.writeString(response.body) | |
context.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil) | |
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil) | |
case .body: | |
break | |
case .end: | |
context.close(promise: nil) | |
} | |
} | |
private func createResponse(for uri: String) -> (status: HTTPResponseStatus, headers: HTTPHeaders, body: String) { | |
switch uri { | |
case "/ping": | |
let body = #"{"ping": "pong"}"# | |
let headers = HTTPHeaders([("Content-Length", "\(body.count)")]) | |
return (.ok, headers, body) | |
default: | |
return (.notFound, HTTPHeaders(), "Not found") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment