Allow params field to be omitted in requests for methods that don't require them (#17)
* Allow `params` field to be omitted in requests for methods with empty parameters * Add ping request to roundtrip test * Generalize decoding of non-required parameters
This commit is contained in:
parent
3e628db6d8
commit
6f1f65a392
1 changed files with 206 additions and 25 deletions
|
|
@ -76,4 +76,49 @@ struct RequestTests {
|
|||
#expect(decoded.id == "test-id")
|
||||
#expect(decoded.method == EmptyMethod.name)
|
||||
}
|
||||
|
||||
@Test("NotRequired parameters request decoding - with params")
|
||||
func testNotRequiredParametersRequestDecodingWithParams() throws {
|
||||
// Test decoding when params field is present
|
||||
let jsonString = """
|
||||
{"jsonrpc":"2.0","id":"test-id","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 == "test-id")
|
||||
#expect(decoded.method == Ping.name)
|
||||
}
|
||||
|
||||
@Test("NotRequired parameters request decoding - without params")
|
||||
func testNotRequiredParametersRequestDecodingWithoutParams() throws {
|
||||
// Test decoding when params field is missing
|
||||
let jsonString = """
|
||||
{"jsonrpc":"2.0","id":"test-id","method":"ping"}
|
||||
"""
|
||||
let data = jsonString.data(using: .utf8)!
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
let decoded = try decoder.decode(Request<Ping>.self, from: data)
|
||||
|
||||
#expect(decoded.id == "test-id")
|
||||
#expect(decoded.method == Ping.name)
|
||||
}
|
||||
|
||||
@Test("NotRequired parameters request decoding - with null params")
|
||||
func testNotRequiredParametersRequestDecodingWithNullParams() throws {
|
||||
// Test decoding when params field is null
|
||||
let jsonString = """
|
||||
{"jsonrpc":"2.0","id":"test-id","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 == "test-id")
|
||||
#expect(decoded.method == Ping.name)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue