|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the valkey-swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 the valkey-swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of valkey-swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import NIOCore |
| 16 | +import Valkey |
| 17 | + |
| 18 | +// Fake Valkey server channel handler |
| 19 | +final class TestValkeyServerChannelHandler: ChannelInboundHandler { |
| 20 | + typealias InboundIn = ByteBuffer |
| 21 | + typealias OutboundOut = ByteBuffer |
| 22 | + |
| 23 | + private var decoder = NIOSingleStepByteToMessageProcessor(RESPTokenDecoder()) |
| 24 | + private let commandHandler: (String, ArraySlice<String>, (ByteBuffer) -> Void) -> Void |
| 25 | + |
| 26 | + static private let helloCommand = RESPToken.Value.bulkString(ByteBuffer(string: "HELLO")) |
| 27 | + static private let helloResponse = ByteBuffer(string: "%1\r\n+server\r\n+fake\r\n") |
| 28 | + static private let pingCommand = RESPToken.Value.bulkString(ByteBuffer(string: "PING")) |
| 29 | + static private let pongResponse = ByteBuffer(string: "$4\r\nPONG\r\n") |
| 30 | + static private let clientCommand = RESPToken.Value.bulkString(ByteBuffer(string: "CLIENT")) |
| 31 | + static private let setInfoSubCommand = RESPToken.Value.bulkString(ByteBuffer(string: "SETINFO")) |
| 32 | + static private let okResponse = ByteBuffer(string: "+2OK\r\n") |
| 33 | + |
| 34 | + static private let response = ByteBuffer(string: "$3\r\nBar\r\n") |
| 35 | + static func defaultHandler(command: String, parameters: ArraySlice<String>, write: (ByteBuffer) -> Void) { |
| 36 | + guard command == "GET" else { |
| 37 | + fatalError("Unexpected command: \(command)") |
| 38 | + } |
| 39 | + write(Self.response) |
| 40 | + } |
| 41 | + |
| 42 | + init(commandHandler: @escaping (String, ArraySlice<String>, (ByteBuffer) -> Void) -> Void = defaultHandler) { |
| 43 | + self.commandHandler = commandHandler |
| 44 | + } |
| 45 | + |
| 46 | + func channelRead(context: ChannelHandlerContext, data: NIOAny) { |
| 47 | + try! self.decoder.process(buffer: self.unwrapInboundIn(data)) { token in |
| 48 | + self.handleToken(context: context, token: token) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + func handleToken(context: ChannelHandlerContext, token: RESPToken) { |
| 53 | + guard let fullCommand = try? token.decode(as: [String].self) else { |
| 54 | + fatalError() |
| 55 | + } |
| 56 | + guard let command = fullCommand.first else { |
| 57 | + fatalError() |
| 58 | + } |
| 59 | + let parameters = fullCommand.dropFirst() |
| 60 | + switch command { |
| 61 | + case "HELLO": |
| 62 | + context.writeAndFlush(self.wrapOutboundOut(Self.helloResponse), promise: nil) |
| 63 | + |
| 64 | + case "PING": |
| 65 | + context.writeAndFlush(self.wrapOutboundOut(Self.pongResponse), promise: nil) |
| 66 | + |
| 67 | + case "CLIENT": |
| 68 | + switch parameters.first { |
| 69 | + case "SETINFO": |
| 70 | + context.writeAndFlush(self.wrapOutboundOut(Self.okResponse), promise: nil) |
| 71 | + default: |
| 72 | + commandHandler(command, parameters) { |
| 73 | + context.writeAndFlush(self.wrapOutboundOut($0), promise: nil) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + default: |
| 78 | + commandHandler(command, parameters) { |
| 79 | + context.writeAndFlush(self.wrapOutboundOut($0), promise: nil) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments