-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.irule
101 lines (92 loc) · 5.25 KB
/
test.irule
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
94
95
96
97
98
99
100
101
###############################################################################
# Testsuite for the Json library for the F5 iRules language to parse and decode Json.
#
# SPDX-License-Identifier: GPL-2.0-only
# (C) 2024 Juergen Mang <[email protected]>
###############################################################################
###############################################################################
# Simple json_get test function
#
# @param {string} json Json string to parse
# @param {string} path JsonPath expression
# @param {string} expected Expected value
# @returns {string} Test result
proc test_json_get { json path expected } {
set len -1 ; # Length of returned token
set token ""
set idx [call json::json_get $json $path len]
if { $idx >= 0 } {
set token [string range $json $idx [expr {$idx + $len - 1}]]
}
if { ($token eq "") || ($token ne $expected && $expected ne "") } {
return "<tr><td colspan=\"2\">$path</td></tr><tr><td>ERROR: $idx</td><td>found: $token, expected: $expected</td></tr>"
}
return "<tr><td colspan=\"2\">$path</td></tr><tr><td>OK: $idx</td><td>found: $token, expected: $expected</td></tr>"
}
###############################################################################
# Simple json decode test function
#
# @param {string} token Json string to decode
# @param {string} expected Expected value
# @returns {string} Test result
proc test_json_decode { token expected } {
puts "- decode: $token"
set decoded [call json::json_decode_str "\"test\"test\""]
if { $decoded eq "test\"test" } {
return "<tr><td>OK</td><td>decoded $decoded, expected: $expected</td></tr>"
}
return "<tr><td>ERROR</td><td>decoded $decoded, expected: $expected</td></tr>"
}
when HTTP_REQUEST priority 100 {
set response "<html><head><title>Json Test</title></head><body>"
append response "<h2>Test 1</h2>"
set json "{\"text1\":\"string1\",\"object1\":{\"text2\":\"string2\",\"array\":\[\"mem1\",\"mem2\"\],\"bool1\":false},\"num1\":1,\"bool2\":true}"
append response "<table><tr><td colspan=\"2\">$json</td></tr>"
append response [call test_json_get $json "$.text1" "\"string1\""]
append response [call test_json_get $json "$.object1.text2" "\"string2\""]
append response [call test_json_get $json "$.object1.bool1" "false"]
append response [call test_json_get $json "$.num1" "1"]
append response [call test_json_get $json "$.bool2" "true"]
append response [call test_json_get $json "$.object1" ""]
append response [call test_json_get $json "$.object1.array" ""]
append response [call test_json_get $json "$.object1.array\[0\]" "\"mem1\""]
append response [call test_json_get $json "$.object1.array\[1\]" "\"mem2\""]
append response "</table>"
append response "<h2>Test 2</h2>"
set json "{\"text1\":\"string1\",\"object1\":{\"text2\":\"string2\",\"array1\":\[\"mem1\",\"mem2\"\]},\"num1\":1,\"bool2\":true}"
append response "<table><tr><td colspan=\"2\">$json</td></tr>"
append response [call test_json_get $json "$.num1" "1"]
append response "</table>"
append response "<h2>Test 3</h2>"
set json "{\"object1\": {\"array1\":\[{\"v1\":\"mem1\",\"v2\":\"mem2\"},{\"v3\":\"mem3\"}\],\"num1\":1},\"bool2\":true}"
append response "<table><tr><td colspan=\"2\">$json</td></tr>"
append response [call test_json_get $json "$.object1.num1" "1"]
append response [call test_json_get $json "$.object1.array1" ""]
append response [call test_json_get $json "$.object1.array1\[0\].v1" "\"mem1\""]
append response [call test_json_get $json "$.object1.array1\[0\].v2" "\"mem2\""]
append response [call test_json_get $json "$.object1.array1\[1\].v3" "\"mem3\""]
append response [call test_json_get $json "$.bool2" "true"]
append response "</table>"
append response "<h2>Test 4</h2>"
set json "{\"key1\":\"val\\\"ue1\",\"key\\n2\":\"value2\"}"
append response "<table><tr><td colspan=\"2\">$json</td></tr>"
append response [call test_json_get $json "$.key1" "\"val\\\"ue1\""]
append response [call test_json_get $json "$.key\\n2" "\"value2\""]
append response "</table>"
append response "<h2>Test 5</h2>"
set json [ifile get "test-json"]
append response "<table><tr><td colspan=\"2\">Large json file</td></tr>"
append response [call test_json_get $json "$.policy.template.name" "\"POLICY_TEMPLATE_RAPID_DEPLOYMENT\""]
append response [call test_json_get $json "$.policy.template.name" "\"POLICY_TEMPLATE_RAPID_DEPLOYMENT\""]
append response [call test_json_get $json "$.policy.signature-settings.signatureStaging" "false"]
append response [call test_json_get $json "$.policy.blocking-settings.violations\[1\].name" "\"VIOL_SESSION_AWARENESS\""]
append response [call test_json_get $json "$.policy.policy-builder-server-technologies.enableServerTechnologiesDetection" "true"]
append response [call test_json_get $json "$.policy.urls\[2\].urlContentProfiles\[0\].headerValue" "\"text/html\""]
append response "</table>"
append response "<h2>Test 6</h2>"
append response "<table>"
append response [call test_json_decode "\"test\"test\"" "test\"test"]
append response "</table>"
append response "</body></html>"
HTTP::respond 200 content $response "Content-Type" "text/html"
}