From b4f8a86cd6ea00c13c7604c959c2f993e5e4e2cb Mon Sep 17 00:00:00 2001 From: Jiale Zhi Date: Tue, 29 Oct 2013 11:26:14 -0700 Subject: Add test cases for empty value and no cookie header --- lib/resty/cookie.lua | 25 ++++++++++++------------ t/sanity.t | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/lib/resty/cookie.lua b/lib/resty/cookie.lua index 652df57..87f8e74 100644 --- a/lib/resty/cookie.lua +++ b/lib/resty/cookie.lua @@ -1,10 +1,14 @@ -- Copyright (C) 2013 Jiale Zhi (calio), Cloudflare Inc. -require "luacov" +-- require "luacov" local type = type local get_string_byte = string.byte local get_string_sub = string.sub +local EQUAL = get_string_byte("=") +local SEMICOLON = get_string_byte(";") +local SPACE = get_string_byte(" ") + local ok, new_tab = pcall(require, "table.new") if not ok then @@ -13,7 +17,6 @@ end local _M = new_tab(0, 2) - _M._VERSION = '0.01' @@ -22,9 +25,10 @@ local mt = { __index = _M } local function get_cookie_table(text_cookie) if type(text_cookie) ~= "string" then - return nil, string.format( + ngx.log(ngx.ERR, string.format( "expect text_cookie to be \"string\" but found %s", - type(text_cookie)) + type(text_cookie))) + return {} end local cookie_table = {} @@ -39,13 +43,13 @@ local function get_cookie_table(text_cookie) while j <= #text_cookie do if state == EXPECT_KEY then - if get_string_byte(text_cookie, j) == get_string_byte("=") then + if get_string_byte(text_cookie, j) == EQUAL then key = get_string_sub(text_cookie, i, j - 1) state = EXPECT_VALUE i = j + 1 end elseif state == EXPECT_VALUE then - if get_string_byte(text_cookie, j) == get_string_byte(";") then + if get_string_byte(text_cookie, j) == SEMICOLON then value = get_string_sub(text_cookie, i, j - 1) cookie_table[key] = value @@ -54,7 +58,7 @@ local function get_cookie_table(text_cookie) i = j + 1 end elseif state == EXPECT_SP then - if get_string_byte(text_cookie, j) ~= get_string_byte(" ") then + if get_string_byte(text_cookie, j) ~= SPACE then state = EXPECT_KEY i = j j = j - 1 @@ -90,12 +94,7 @@ function _M.get_all(self) local err if self.cookie_table == nil then - ngx.log(ngx.NOTICE, "call get_cookie_table") - self.cookie_table, err = get_cookie_table(self._cookie) - if self.cookie_table == nil then - ngx.log(ngx.ERR, "failed to get all cookies: ", err) - return {} - end + self.cookie_table = get_cookie_table(self._cookie) end return self.cookie_table diff --git a/t/sanity.t b/t/sanity.t index c9d7262..bc52348 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() * 2); +plan tests => repeat_each() * (blocks() * 2 + 1); my $pwd = cwd(); @@ -16,7 +16,7 @@ our $HttpConfig = qq{ $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; -no_long_string(); +#no_long_string(); log_level('debug'); @@ -76,3 +76,54 @@ Cookie: SID=31d4d96e407aad42; lang=en-US --- response_body lang => en-US + +=== TEST 3: no cookie header +--- 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 field = cookie:get("lang") + ngx.say("lang", " => ", field) + '; + } +--- request +GET /t +--- error_log +no cookie found in current request +--- response_body + + +=== TEST 4: empty value +--- 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 fields = cookie:get_all() + + for k, v in pairs(fields) do + ngx.say(k, " => ", v) + end + '; + } +--- request +GET /t +--- more_headers +Cookie: SID= +--- response_body +SID => + + -- cgit v1.2.3-70-g09d2