-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest.html
93 lines (75 loc) · 5.07 KB
/
test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>QUnit JSON-RPC.js</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.14.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
<script src="json-rpc.min.js"></script>
<script>
test( "Errors", function() {
ok( JSON_RPC.PARSE_ERROR.constructor, "PARSE_ERROR user Error constructor" );
ok( JSON_RPC.INVALID_REQUEST.constructor, "INVALID_REQUEST user Error constructor" );
ok( JSON_RPC.METHOD_NOT_FOUND.constructor, "METHOD_NOT_FOUND user Error constructor" );
ok( JSON_RPC.INVALID_PARAMS.constructor, "INVALID_PARAMS user Error constructor" );
ok( JSON_RPC.INTERNAL_ERROR.constructor, "INTERNAL_ERROR user Error constructor" );
ok( JSON.stringify(JSON_RPC.PARSE_ERROR) == '{"code":-32700,"message":"An error occurred on the server while parsing the JSON text."}', "PARSE_ERROR text OK" );
ok( JSON.stringify(JSON_RPC.INVALID_REQUEST) == '{"code":-32600,"message":"The JSON sent is not a valid Request object."}', "INVALID_REQUEST text OK" );
ok( JSON.stringify(JSON_RPC.METHOD_NOT_FOUND) == '{"code":-32601,"message":"The method does not exist / is not available."}', "METHOD_NOT_FOUND text OK" );
ok( JSON.stringify(JSON_RPC.INVALID_PARAMS) == '{"code":-32602,"message":"Invalid method parameter(s)."}', "INVALID_PARAMS text OK" );
ok( JSON.stringify(JSON_RPC.INTERNAL_ERROR) == '{"code":-32603,"message":"Internal JSON-RPC error."}', "INTERNAL_ERROR text OK" );
var error = new JSON_RPC.Error(0, "Test Error", [1, 2, 3]);
ok( JSON.stringify(error) == '{"code":0,"message":"Test Error","data":[1,2,3]}', "Sample error text OK" );
});
test( "Request", function() {
var rpc0 = new JSON_RPC.Request("myMethod", [1, 2, 3]);
ok( rpc0.toString() == '{"jsonrpc":"2.0","id":0,"method":"myMethod","params":[1,2,3]}', "Constructed properly" );
var rpc1 = new JSON_RPC.Request("myMethod", [1, 2, 3]);
ok( rpc1.toString() == '{"jsonrpc":"2.0","id":1,"method":"myMethod","params":[1,2,3]}', "ID increments" );
rpc0.result = [4, 5, 6];
ok( rpc0.toString() == '{"jsonrpc":"2.0","id":0,"result":[4,5,6]}', "Manages successful result" );
rpc1.error = JSON_RPC.METHOD_NOT_FOUND;
ok( rpc1.toString() == '{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"The method does not exist / is not available."}}', "Manages error result" );
ok( rpc0.constructor === JSON_RPC.Request, "Request constructor used" );
ok( rpc1.error.constructor === JSON_RPC.Error, "Error constructor used" );
});
test( "Notification", function() {
var rpc0 = new JSON_RPC.Notification("myNotification", [1, 2, 3]);
ok( rpc0.toString() == '{"jsonrpc":"2.0","method":"myNotification","params":[1,2,3]}', "Constructed properly" );
ok( rpc0.constructor === JSON_RPC.Notification, "Notification constructor used" );
});
test( "Parser", function() {
var rpc0 = new JSON_RPC.Request("myMethod", [1, 2, 3]);
var rpc1 = new JSON_RPC.Notification("myNotification", [1, 2, 3]);
var rpc2 = JSON_RPC.parse(rpc0.toString());
var rpc3 = JSON_RPC.parse(rpc1.toString());
ok( rpc0.toString() == rpc2.toString(), "Request parsed" );
ok( rpc1.toString() == rpc3.toString(), "Notification parsed" );
ok( rpc2.constructor === JSON_RPC.Request, "Request constructor maintained" );
ok( rpc3.constructor === JSON_RPC.Notification, "Notification constructor maintained" );
rpc0.result = [4, 5, 6]
var rpc4 = JSON_RPC.parse(rpc0.toString());
ok( rpc0.toString() == rpc4.toString(), "Parsed successful result" );
rpc0.error = JSON_RPC.METHOD_NOT_FOUND;
var rpc5 = JSON_RPC.parse(rpc0.toString());
ok( rpc0.toString() == rpc5.toString(), "Parsed error result" );
ok( rpc5.error.constructor === JSON_RPC.Error, "Error constructor used" );
});
test( "Batch", function() {
var rpc0 = new JSON_RPC.Request("myMethod", [1, 2, 3]);
var rpc1 = new JSON_RPC.Request("myMethod", [4, 5, 6]);
var rpc2 = new JSON_RPC.Notification("myNotification", [1, 2, 3]);
var rpc3 = new JSON_RPC.Notification("myNotification", [4, 5, 6]);
var batch = [rpc0.toString(), rpc1.toString(), rpc2.toString(), rpc3.toString()];
var arr = JSON_RPC.parse(batch);
arr.forEach(function (el, i) {
ok( el.toString() == batch[i], "Batch request " + i + " OK" );
});
});
</script>
</body>
</html>