diff --git a/mocks/pv.lua b/mocks/pv.lua index 4ede7f8..c31eddd 100644 --- a/mocks/pv.lua +++ b/mocks/pv.lua @@ -55,6 +55,25 @@ local pvMock = { t.vars = {} t.hdr = hdr + function t._is_pvheader(id) + local patterns = { + '%$(x_%l+)%(([%w_-]+)%)$', + '%$%((x_%l+)%(([%w_-]+)%)%)$', + '%$%((x_%l+)%(([%w_-]+)%)%[%*%]%)$', + '%$%((x_%l+)%(([%w_-]+)%)%[(%d+)%]%)$', + } + for _,v in pairs(patterns) do + for _type, key, indx in string.gmatch(id, v) do + if _ == 4 then + indx = tonumber(indx) + end + return { id=string.lower(key), + indx=indx, clean=(v==patterns[3]), + type=_type } + end + end + end + function t._is_sht(id) local patterns = { '%$sht%(([%w_^%[]+)=>(.*)%)$', @@ -230,10 +249,16 @@ local pvMock = { end if not result then result = t._is_sht(id) - if string.match(result.key, '^%$') then + if result and string.match(result.key, '^%$') then result.key = t.get(result.key) end end + if not result then + result = t._is_pvheader(id) + if result and string.match(result.id, '^%$') then + result.id = string.lower(t.get(result.id)) + end + end if not result then error(string.format("not implemented or wrong id:%s", id)) end @@ -274,7 +299,7 @@ local pvMock = { end end end - elseif result.type == 'avp' then + elseif result.type == 'avp' or result.type == 'x_hdr' then if t.vars[result.private_id] then if not result.indx then result.indx = 0 @@ -339,7 +364,7 @@ local pvMock = { temp[result.key] = utils.Stack:new() temp[result.key]:push(value) t.vars[result.private_id]:push(temp) - elseif result.type == 'avp' then + elseif result.type == 'avp' or result.type == 'x_hdr' then t.vars[result.private_id] = utils.Stack:new() t.vars[result.private_id]:push(value) elseif result.type == 'pv' and result.mode == 'rw' then @@ -378,7 +403,7 @@ local pvMock = { end t.vars[result.private_id][result.indx][result.key]:push(value) end - elseif result.type == 'avp' then + elseif result.type == 'avp' or result.type == 'x_hdr' then t.vars[result.private_id]:push(value) elseif result.type == 'pv' and result.mode == 'rw' then t.vars_pv.rw[result.id] = value @@ -438,7 +463,7 @@ local pvMock = { -- xavp(g[1]=>k) t.vars[result.private_id][result.indx][result.key] = nil end - elseif result.type == 'avp' then + elseif result.type == 'avp' or result.type == 'x_hdr' then if result.clean then t.vars[result.private_id] = nil return diff --git a/tests/mocks_pv.lua b/tests/mocks_pv.lua index 3af279a..7022310 100644 --- a/tests/mocks_pv.lua +++ b/tests/mocks_pv.lua @@ -93,6 +93,39 @@ TestPVMock = {} lu.assertEquals(result.key, '$hola') end + function TestPVMock:test_is_pvheader() + local result = self.pv._is_pvheader("$x_hdr(From)") + lu.assertNotNil(result) + lu.assertEquals(result.type, 'x_hdr') + lu.assertEquals(result.id, 'from') + lu.assertIsNil(result.key) + lu.assertIsNil(result.indx) + lu.assertIsNil(result.kindx) + lu.assertFalse(result.clean) + end + + function TestPVMock:test_is_pvheader_indx() + local result = self.pv._is_pvheader("$(x_hdr(P-Asserted-Identity)[2])") + lu.assertNotNil(result) + lu.assertEquals(result.type, 'x_hdr') + lu.assertEquals(result.id, 'p-asserted-identity') + lu.assertIsNil(result.key) + lu.assertEquals(result.indx, 2) + lu.assertIsNil(result.kindx) + lu.assertFalse(result.clean) + end + + function TestPVMock:test_is_pvheader_clean() + local result = self.pv._is_pvheader("$(x_hdr(P-Asserted-Identity)[*])") + lu.assertNotNil(result) + lu.assertEquals(result.type, 'x_hdr') + lu.assertEquals(result.id, 'p-asserted-identity') + lu.assertIsNil(result.key) + lu.assertNil(result.indx) + lu.assertIsNil(result.kindx) + lu.assertTrue(result.clean) + end + function TestPVMock:test_is_xavi_simple() local result = self.pv._is_xavi("$xavi(ID=>KEY)") lu.assertNotNil(result) @@ -589,3 +622,16 @@ TestPVMock = {} self.pv.unset("$sht(nono=>nono)") lu.assertNil(self.pv.get("$sht(nono=>nono)")) end + + function TestPVMock:test_x_hdr_sets() + local value = "+43567890" + self.pv.sets("$x_hdr(P-Asserted-Identity)", value) + lu.assertEquals(self.pv.get("$x_hdr(p-asserted-Identity)"), value) + end + + function TestPVMock:test_x_hdr_unset() + self.pv.sets("$x_hdr(P-Asserted-Identity)", "+43567890") + lu.assertNotNil(self.pv.get("$x_hdr(P-Asserted-Identity)")) + self.pv.unset("$x_hdr(P-Asserted-Identity)") + lu.assertNil(self.pv.get("$x_hdr(P-Asserted-identity)")) + end \ No newline at end of file