|
| 1 | +use Test::Nginx::Socket::Lua 'no_plan'; |
| 2 | + |
| 3 | +log_level('warn'); |
| 4 | +no_long_string(); |
| 5 | +repeat_each(2); |
| 6 | + |
| 7 | +our $HttpConfig = <<'_EOC_'; |
| 8 | + lua_socket_log_errors off; |
| 9 | + lua_package_path 'lib/?.lua;/usr/local/share/lua/5.3/?.lua;/usr/share/lua/5.1/?.lua;;'; |
| 10 | + init_by_lua_block { |
| 11 | + local cjson = require("cjson.safe") |
| 12 | +
|
| 13 | + function check_res(data, err, val, status) |
| 14 | + if err then |
| 15 | + ngx.say("err: ", err) |
| 16 | + end |
| 17 | +
|
| 18 | + if val then |
| 19 | + if data and data.body.kvs==nil then |
| 20 | + ngx.exit(404) |
| 21 | + end |
| 22 | + if data and data.body.kvs and val ~= data.body.kvs[1].value then |
| 23 | + ngx.say("failed to check value") |
| 24 | + ngx.log(ngx.ERR, "failed to check value, got: ", data.body.kvs[1].value, |
| 25 | + ", expect: ", val) |
| 26 | + else |
| 27 | + ngx.say("checked val as expect: ", val) |
| 28 | + end |
| 29 | + end |
| 30 | +
|
| 31 | + if status and status ~= data.status then |
| 32 | + ngx.exit(data.status) |
| 33 | + end |
| 34 | + end |
| 35 | + } |
| 36 | +_EOC_ |
| 37 | +
|
| 38 | +run_tests(); |
| 39 | +
|
| 40 | +__DATA__ |
| 41 | +
|
| 42 | +=== TEST 1: raw string serializer |
| 43 | +--- http_config eval: $::HttpConfig |
| 44 | +--- config |
| 45 | + location /t { |
| 46 | + content_by_lua_block { |
| 47 | + local cjson = require("cjson.safe") |
| 48 | +
|
| 49 | + local etcd, err = require("resty.etcd").new({ |
| 50 | + protocol = "v3", |
| 51 | + serializer = "raw" |
| 52 | + }) |
| 53 | + check_res(etcd, err) |
| 54 | +
|
| 55 | + local res |
| 56 | + res, err = etcd:set("/dir/v3/a", 111) |
| 57 | + check_res(res, err) |
| 58 | +
|
| 59 | + res, err = etcd:set("/dir/v3/b", '"foo"') |
| 60 | + check_res(res, err) |
| 61 | +
|
| 62 | + res, err = etcd:get("/dir/v3/b") |
| 63 | + check_res(res, err, '"foo"') |
| 64 | + |
| 65 | + local s = cjson.encode({a = 1}) |
| 66 | + res, err = etcd:setx("/dir/v3/c", s) |
| 67 | + check_res(res, err, nil, 200) |
| 68 | +
|
| 69 | + res, err = etcd:get("/dir/v3/c") |
| 70 | + check_res(res, err, s, 200) |
| 71 | +
|
| 72 | + res, err = etcd:setnx("/dir/v3/d", "") |
| 73 | + check_res(res, err, nil, 200) |
| 74 | +
|
| 75 | + res, err = etcd:get("/dir/v3/d") |
| 76 | + check_res(res, err, "", 200) |
| 77 | + } |
| 78 | + } |
| 79 | +--- request |
| 80 | +GET /t |
| 81 | +--- no_error_log |
| 82 | +[error] |
| 83 | +--- response_body |
| 84 | +err: unsupported type for number |
| 85 | +checked val as expect: "foo" |
| 86 | +checked val as expect: {"a":1} |
| 87 | +checked val as expect: |
| 88 | +
|
| 89 | +
|
| 90 | +=== TEST 2: json serializer |
| 91 | +--- http_config eval: $::HttpConfig |
| 92 | +--- config |
| 93 | + location /t { |
| 94 | + content_by_lua_block { |
| 95 | + local cjson = require("cjson.safe") |
| 96 | +
|
| 97 | + local etcd, err = require("resty.etcd").new({ |
| 98 | + protocol = "v3", |
| 99 | + serializer = "json" |
| 100 | + }) |
| 101 | + check_res(etcd, err) |
| 102 | +
|
| 103 | + local res |
| 104 | + res, err = etcd:set("/dir/v3/a", 111) |
| 105 | + check_res(res, err) |
| 106 | +
|
| 107 | + res, err = etcd:get("/dir/v3/a") |
| 108 | + check_res(res, err, 111) |
| 109 | +
|
| 110 | + res, err = etcd:set("/dir/v3/b", '"foo"') |
| 111 | + check_res(res, err) |
| 112 | +
|
| 113 | + res, err = etcd:get("/dir/v3/b") |
| 114 | + check_res(res, err, '"foo"') |
| 115 | + |
| 116 | + local s = cjson.encode({a = 1}) |
| 117 | + res, err = etcd:setx("/dir/v3/c", s) |
| 118 | + check_res(res, err, nil, 200) |
| 119 | +
|
| 120 | + res, err = etcd:get("/dir/v3/c") |
| 121 | + check_res(res, err, s, 200) |
| 122 | +
|
| 123 | + res, err = etcd:setnx("/dir/v3/d", "") |
| 124 | + check_res(res, err, nil, 200) |
| 125 | +
|
| 126 | + res, err = etcd:get("/dir/v3/d") |
| 127 | + check_res(res, err, "", 200) |
| 128 | + } |
| 129 | + } |
| 130 | +--- request |
| 131 | +GET /t |
| 132 | +--- error_log |
| 133 | +failed to check value, got: nil, expect: |
| 134 | +--- response_body |
| 135 | +checked val as expect: 111 |
| 136 | +checked val as expect: "foo" |
| 137 | +checked val as expect: {"a":1} |
| 138 | +failed to check value |
0 commit comments