Source

json-rpc / tests / run.scm

(require-extension test)
(use json-rpc-client)

; Setup global string ports and server for testing
(define input (open-input-string "some-string"))
(define output (open-output-string))
(define xbmc (json-rpc-server input output "2.0"))

; Setup own test procedure with locally scoped output ports
(define (test-server message expected method params)
  (let ((output (open-output-string)))
    (if (null? params)
	 ((json-rpc-server input output "2.0") method)
	 ((json-rpc-server input output "2.0") method params))
    (test
     message
     expected
     (get-output-string output))))

(test-group "JSON-RPC string output checks"
	    (test-server "Call with only a method" "{\"jsonrpc\":\"2.0\",\"method\":\"Player.PlayPause\",\"id\":\"1\"}" "Player.PlayPause" '())
	    (test-server "Call with a method and a one dimensional params" "{\"jsonrpc\":\"2.0\",\"method\":\"Player.PlayPause\",\"params\":[\"playerid:0\"],\"id\":\"1\"}" "Player.PlayPause" 'playerid:0))

; Do some actual error testing
(test-group "Non-port or non-version calls"
	    (test-error "Non port call on input" (define xbmc0 (json-rpc-server "input" output "2.0")))
	    (test-error "Non port call on output" (define xbmc1 (json-rpc-server input "output" "2.0"))))

(test-group "Non-string method calls"
	    (test-error "Call method as symbol" (xbmc 'foo))
	    (test-error "Call method as number" (xbmc 1))
	    (test-error "Call method as list" (xbmc '(1 2)))
	    (test-error "Call method as vector" (xbmc '#()))
	    (test-error "Call method as boolean" (xbmc 1)))

(test-group "Non-keyword param calls"
	    (test-error "Call params as a list" (xbmc "foo" '(a b c)))
	    (test-error "Call params as a string" (xbmc "foo" "bar"))
	    (test-error "Call params as a number" (xbmc "foo" 1)))

(test-exit)