tests/test_util_http.lua
author Kim Alvefur <zash@zash.se>
Sun, 25 Nov 2018 13:16:17 +0100
changeset 9647 bb8486491b48
parent 7517 ea58c0fe1cd7
permissions -rw-r--r--
mod_proxy65: Check what port is used at the time of the query Could have changed between startup and time of request
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4339
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
-- Prosody IM
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5604
diff changeset
     4
--
4339
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
-- COPYING file in the source package for more information.
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
--
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
function urlencode(urlencode)
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
	assert_equal(urlencode("helloworld123"), "helloworld123", "Normal characters not escaped");
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
	assert_equal(urlencode("hello world"), "hello%20world", "Spaces escaped");
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
	assert_equal(urlencode("This & that = something"), "This%20%26%20that%20%3d%20something", "Important URL chars escaped");
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
end
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
function urldecode(urldecode)
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
	assert_equal("helloworld123", urldecode("helloworld123"), "Normal characters not escaped");
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
	assert_equal("hello world", urldecode("hello%20world"), "Spaces escaped");
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
	assert_equal("This & that = something", urldecode("This%20%26%20that%20%3d%20something"), "Important URL chars escaped");
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
	assert_equal("This & that = something", urldecode("This%20%26%20that%20%3D%20something"), "Important URL chars escaped");
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
end
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
function formencode(formencode)
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
	assert_equal(formencode({ { name = "one", value = "1"}, { name = "two", value = "2" } }), "one=1&two=2", "Form encoded");
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
	assert_equal(formencode({ { name = "one two", value = "1"}, { name = "two one&", value = "2" } }), "one+two=1&two+one%26=2", "Form encoded");
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
end
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
function formdecode(formdecode)
7517
ea58c0fe1cd7 test_util_http: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
    28
	do
ea58c0fe1cd7 test_util_http: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
    29
		local t = formdecode("one=1&two=2");
ea58c0fe1cd7 test_util_http: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
    30
		assert_table(t[1]);
ea58c0fe1cd7 test_util_http: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
    31
		assert_equal(t[1].name, "one"); assert_equal(t[1].value, "1");
ea58c0fe1cd7 test_util_http: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
    32
		assert_table(t[2]);
ea58c0fe1cd7 test_util_http: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
    33
		assert_equal(t[2].name, "two"); assert_equal(t[2].value, "2");
ea58c0fe1cd7 test_util_http: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
    34
	end
4339
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
7517
ea58c0fe1cd7 test_util_http: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
    36
	do
ea58c0fe1cd7 test_util_http: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
    37
		local t = formdecode("one+two=1&two+one%26=2");
ea58c0fe1cd7 test_util_http: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
    38
		assert_equal(t[1].name, "one two"); assert_equal(t[1].value, "1");
ea58c0fe1cd7 test_util_http: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
    39
		assert_equal(t[2].name, "two one&"); assert_equal(t[2].value, "2");
ea58c0fe1cd7 test_util_http: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 5776
diff changeset
    40
	end
4339
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
end