Random user login using HTTP POST

-- First, define a list with usernames and passwords
local users = {
    { username = 'joe', password = 'secret' },
    { username = 'bill', password = 'secret2' },
    { username = 'anne', password = 'verysecret' },
    { username = 'jim', password = 'topsecret' },
    { username = 'sally', password = 'ohsosecret' },
}
-- Randomize a number which will determine which user we logon as
local user_number = math.random(1, #users)
local username = users[user_number]['username']
local password = users[user_number]['password']

-- Create POST data to log us on with this particular username/password
local post_data = "login=" .. username .. "&password=" .. password

-- Execute HTTP POST operation to log in our user
local response = http.request_batch({
    {'POST', 'http://test.loadimpact.com/user_login.php', data=post_data, response_body_bytes=10240}
})

-- Extract HTTP response body
local response_body = response[1]['body']

-- If we didn't get e.g. "Welcome, jim", our login failed
local search_for = "Welcome, " .. username
if string.find(response_body, search_for) == nil then
    log.info('Failed to logon as user: ' .. username)
end

See also:

Feedback and Knowledge Base