Add unit test to check that AnyRequest handles missing parameters (#24)
* Add unit test to check that AnyRequest handles missing parameters * Conform Value to NotRequired * Add test for decoding AnyRequest with empty parameters --------- Co-authored-by: Mattt Zmuda <mattt@loopwork.com>
This commit is contained in:
parent
36a55a5b57
commit
da97a3db3c
2 changed files with 51 additions and 0 deletions
|
|
@ -11,6 +11,12 @@ public struct Empty: NotRequired, Hashable, Codable, Sendable {
|
|||
public init() {}
|
||||
}
|
||||
|
||||
extension Value: NotRequired {
|
||||
public init() {
|
||||
self = .null
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: -
|
||||
|
||||
/// A method that can be used to send requests and receive responses.
|
||||
|
|
|
|||
|
|
@ -260,4 +260,49 @@ struct RequestTests {
|
|||
from: withCursor.data(using: .utf8)!)
|
||||
#expect(decodedWithCursor.params.cursor == "next-page")
|
||||
}
|
||||
|
||||
@Test("AnyRequest parameters request decoding - without params")
|
||||
func testAnyRequestParametersRequestDecodingWithoutParams() throws {
|
||||
// Test decoding when params field is missing
|
||||
let jsonString = """
|
||||
{"jsonrpc":"2.0","id":1,"method":"ping"}
|
||||
"""
|
||||
let data = jsonString.data(using: .utf8)!
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
let decoded = try decoder.decode(AnyRequest.self, from: data)
|
||||
|
||||
#expect(decoded.id == 1)
|
||||
#expect(decoded.method == Ping.name)
|
||||
}
|
||||
|
||||
@Test("AnyRequest parameters request decoding - with null params")
|
||||
func testAnyRequestParametersRequestDecodingWithNullParams() throws {
|
||||
// Test decoding when params field is null
|
||||
let jsonString = """
|
||||
{"jsonrpc":"2.0","id":1,"method":"ping","params":null}
|
||||
"""
|
||||
let data = jsonString.data(using: .utf8)!
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
let decoded = try decoder.decode(Request<Ping>.self, from: data)
|
||||
|
||||
#expect(decoded.id == 1)
|
||||
#expect(decoded.method == Ping.name)
|
||||
}
|
||||
|
||||
@Test("AnyRequest parameters request decoding - with empty params")
|
||||
func testAnyRequestParametersRequestDecodingWithEmptyParams() throws {
|
||||
// Test decoding when params field is null
|
||||
let jsonString = """
|
||||
{"jsonrpc":"2.0","id":1,"method":"ping","params":{}}
|
||||
"""
|
||||
let data = jsonString.data(using: .utf8)!
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
let decoded = try decoder.decode(Request<Ping>.self, from: data)
|
||||
|
||||
#expect(decoded.id == 1)
|
||||
#expect(decoded.method == Ping.name)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue