You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

selectors.custom.lua 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. local msg
  2. context("Selectors test", function()
  3. local rspamd_task = require "rspamd_task"
  4. local logger = require "rspamd_logger"
  5. local lua_selectors = require "lua_selectors"
  6. local test_helper = require "rspamd_test_helper"
  7. local cfg = rspamd_config
  8. local task
  9. test_helper.init_url_parser()
  10. before(function()
  11. local res
  12. res,task = rspamd_task.load_from_string(msg, cfg)
  13. if not res then
  14. assert_true(false, "failed to load message")
  15. end
  16. end)
  17. local function check_selector(selector_string)
  18. local sels = lua_selectors.parse_selector(cfg, selector_string)
  19. local elts = lua_selectors.process_selectors(task, sels)
  20. return elts
  21. end
  22. test("custom selector", function()
  23. lua_selectors.register_extractor(rspamd_config, "get_something", {
  24. get_value = function(task, args) -- mandatory field
  25. return 'simple value','string' -- result + type
  26. end,
  27. description = 'Sample extractor' -- optional
  28. })
  29. local elts = check_selector('get_something')
  30. assert_not_nil(elts)
  31. assert_rspamd_table_eq({actual = elts, expect = {'simple value'}})
  32. end)
  33. test("custom transform", function()
  34. lua_selectors.register_extractor(rspamd_config, "get_something", {
  35. get_value = function(task, args) -- mandatory field
  36. return 'simple value','string' -- result + type
  37. end,
  38. description = 'Sample extractor' -- optional
  39. })
  40. lua_selectors.register_transform(rspamd_config, "append_string", {
  41. types = {['string'] = true}, -- accepted types
  42. process = function(input, type, args)
  43. return input .. table.concat(args or {}),'string' -- result + type
  44. end,
  45. map_type = 'string', -- can be used in map like invocation, always return 'string' type
  46. description = 'Adds all arguments to the input string'
  47. })
  48. local elts = check_selector('get_something.append_string(" and a simple tail")')
  49. assert_not_nil(elts)
  50. assert_rspamd_table_eq({actual = elts, expect = {'simple value and a simple tail'}})
  51. local elts = check_selector('get_something.append_string(" and", " a", " simple", " nail")')
  52. assert_not_nil(elts)
  53. assert_rspamd_table_eq({actual = elts, expect = {'simple value and a simple nail'}})
  54. end)
  55. end)
  56. --[=========[ ******************* message ******************* ]=========]
  57. msg = [[
  58. From: <whoknows@nowhere.com>
  59. To: <nobody@example.com>, <no-one@example.com>
  60. Date: Wed, 19 Sep 2018 14:36:51 +0100 (BST)
  61. Subject: Test subject
  62. Content-Type: multipart/alternative;
  63. boundary="_000_6be055295eab48a5af7ad4022f33e2d0_"
  64. --_000_6be055295eab48a5af7ad4022f33e2d0_
  65. Content-Type: text/plain; charset="utf-8"
  66. Content-Transfer-Encoding: base64
  67. Hello world
  68. ]]