mod_pep: Use correct field for full JID (thanks Milan*)
[prosody.git] / tests / test_util_jid.lua
index ac17f25354f7889029e4c2390cc188bf3557fd29..c697e63f5c33c85f66374092f52ad8a78a555fbe 100644 (file)
@@ -1,7 +1,7 @@
 -- Prosody IM
 -- Copyright (C) 2008-2010 Matthew Wild
 -- Copyright (C) 2008-2010 Waqas Hussain
--- 
+--
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
 --
@@ -28,15 +28,18 @@ function split(split)
 
        -- Valid JIDs
        test("node@server",             "node", "server", nil           );
-       test("node@server/resource",    "node", "server", "resource"    );
-       test("server",                  nil,    "server", nil           );
-       test("server/resource",         nil,    "server", "resource"    );
+       test("node@server/resource",    "node", "server", "resource"        );
+       test("server",                  nil,    "server", nil               );
+       test("server/resource",         nil,    "server", "resource"        );
+       test("server/resource@foo",     nil,    "server", "resource@foo"    );
+       test("server/resource@foo/bar", nil,    "server", "resource@foo/bar");
 
        -- Always invalid JIDs
        test(nil,                nil, nil, nil);
        test("node@/server",     nil, nil, nil);
        test("@server",          nil, nil, nil);
        test("@server/resource", nil, nil, nil);
+       test("@/resource", nil, nil, nil);
 end
 
 function bare(bare)
@@ -68,3 +71,73 @@ function compare(compare)
        assert_equal(compare("user@other-host", "host"), false, "host should not match");
        assert_equal(compare("user@other-host", "user@host"), false, "host should not match");
 end
+
+function node(node)
+       local function test(jid, expected_node)
+               assert_equal(node(jid), expected_node, "Unexpected node for "..tostring(jid));
+       end
+
+       test("example.com", nil);
+       test("foo.example.com", nil);
+       test("foo.example.com/resource", nil);
+       test("foo.example.com/some resource", nil);
+       test("foo.example.com/some@resource", nil);
+
+       test("foo@foo.example.com/some@resource", "foo");
+       test("foo@example/some@resource", "foo");
+
+       test("foo@example/@resource", "foo");
+       test("foo@example@resource", nil);
+       test("foo@example", "foo");
+       test("foo", nil);
+
+       test(nil, nil);
+end
+
+function host(host)
+       local function test(jid, expected_host)
+               assert_equal(host(jid), expected_host, "Unexpected host for "..tostring(jid));
+       end
+
+       test("example.com", "example.com");
+       test("foo.example.com", "foo.example.com");
+       test("foo.example.com/resource", "foo.example.com");
+       test("foo.example.com/some resource", "foo.example.com");
+       test("foo.example.com/some@resource", "foo.example.com");
+
+       test("foo@foo.example.com/some@resource", "foo.example.com");
+       test("foo@example/some@resource", "example");
+
+       test("foo@example/@resource", "example");
+       test("foo@example@resource", nil);
+       test("foo@example", "example");
+       test("foo", "foo");
+
+       test(nil, nil);
+end
+
+function resource(resource)
+       local function test(jid, expected_resource)
+               assert_equal(resource(jid), expected_resource, "Unexpected resource for "..tostring(jid));
+       end
+
+       test("example.com", nil);
+       test("foo.example.com", nil);
+       test("foo.example.com/resource", "resource");
+       test("foo.example.com/some resource", "some resource");
+       test("foo.example.com/some@resource", "some@resource");
+
+       test("foo@foo.example.com/some@resource", "some@resource");
+       test("foo@example/some@resource", "some@resource");
+
+       test("foo@example/@resource", "@resource");
+       test("foo@example@resource", nil);
+       test("foo@example", nil);
+       test("foo", nil);
+       test("/foo", nil);
+       test("@x/foo", nil);
+       test("@/foo", nil);
+
+       test(nil, nil);
+end
+