aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/resty/cookie.lua12
-rw-r--r--t/sanity.t52
2 files changed, 60 insertions, 4 deletions
diff --git a/lib/resty/cookie.lua b/lib/resty/cookie.lua
index a96da60..dfb8f8a 100644
--- a/lib/resty/cookie.lua
+++ b/lib/resty/cookie.lua
@@ -165,9 +165,11 @@ function _M.set(self, cookie)
if set_cookie_type == "string" then
-- only one cookie has been setted
- t[1] = set_cookie
- t[2] = cookie_str
- ngx_header['Set-Cookie'] = t
+ if set_cookie ~= cookie_str then
+ t[1] = set_cookie
+ t[2] = cookie_str
+ ngx_header['Set-Cookie'] = t
+ end
elseif set_cookie_type == "table" then
-- more than one cookies has been setted
local size = #set_cookie
@@ -176,6 +178,10 @@ function _M.set(self, cookie)
-- so create a new table, copy all the values, and then set it back
for i=1, size do
t[i] = ngx_header['Set-Cookie'][i]
+ if t[i] == cookie_str then
+ -- new cookie is duplicated
+ return true
+ end
end
t[size + 1] = cookie_str
ngx_header['Set-Cookie'] = t
diff --git a/t/sanity.t b/t/sanity.t
index 1385ebb..952b81e 100644
--- a/t/sanity.t
+++ b/t/sanity.t
@@ -5,7 +5,7 @@ use Cwd qw(cwd);
repeat_each(2);
-plan tests => repeat_each() * (blocks() * 3 + 2);
+plan tests => repeat_each() * (blocks() * 3 + 4);
my $pwd = cwd();
@@ -257,3 +257,53 @@ key, so use "--- raw_response_headers_like" instead
--- raw_response_headers_like: Set-Cookie: Name=Bob; Path=/\r\nSet-Cookie: Age=20\r\nSet-Cookie: ID=0xf7898; Expires=Wed, 09 Jun 2021 10:18:14 GMT
--- response_body
Set cookie
+
+
+
+=== TEST 8: remove duplicated cookies in cookie:set
+--- http_config eval: $::HttpConfig
+--- config
+ location /t {
+ content_by_lua '
+ local ck = require "resty.cookie"
+ local cookie, err = ck:new()
+ if not cookie then
+ ngx.log(ngx.ERR, err)
+ return
+ end
+
+ local ok, err = cookie:set({
+ key = "Name", value = "Bob", path = "/",
+ })
+ if not ok then
+ ngx.log(ngx.ERR, err)
+ return
+ end
+
+ local ok, err = cookie:set({
+ key = "Age", value = "20",
+ })
+ if not ok then
+ ngx.log(ngx.ERR, err)
+ return
+ end
+
+ local ok, err = cookie:set({
+ key = "Name", value = "Bob", path = "/",
+ })
+ if not ok then
+ ngx.log(ngx.ERR, err)
+ return
+ end
+
+ ngx.say("Set cookie")
+ ';
+ }
+--- request
+GET /t
+--- no_error_log
+[error]
+--- raw_response_headers_like: Set-Cookie: Name=Bob; Path=/\r\nSet-Cookie: Age=20\r\n
+--- raw_response_headers_unlike: Set-Cookie: Name=Bob; Path=/\r\nSet-Cookie: Age=20\r\nSet-Cookie: Name=Bob; Path=/
+--- response_body
+Set cookie