Testing a system that requires uploading a file

Currently, there is no way to upload a file and make it available in a user scenario for testing file uploads. However, there are a few methods for testing systems where file uploads is a requirement. We have the following example code that will help you with scripting your scenarios to allow for this:

-- LOADIMPACT script example for file upload

local boundary = "dV4Dzl7FuKKkC94lMJgmmw"
local postData = ""
local ds = datastore.open("random-string-datastore")
local noOfRows = 26 -- datastore:get_length() -- hardcoded to work in sandbox


-- generate a random string for a given length
randomString = function (length)
  local rndString = ""
  local index = 1
  while(string.len(rndString) < (length * 1000000)) do
    if(index > noOfRows) then
      index = 1
    end
    row = ds:get(index)
    index = index + 1
    rndString = rndString..row[1]
  end
  return rndString
end

  local uid = util.unique()
-- for writing json data to post
writeJSONContent = function ()
  local jsonData =  '{\"description\":\"Audio Upload\", \"uid\":\"'..uid..'\"}'
  postData = postData..'--'..boundary..'\r\n'
  postData = postData..'Content-Type:application/vnd.playground.audio+json; version=1.0; charset=UTF-8\r\n'
  postData = postData..'\r\n'
  postData = postData..jsonData..'\r\n'
end

-- for writing random string to post request
writeFileContent = function (contentType, size)
    fileData = randomString(size)
    postData = postData.."--"..boundary..'\r\n'
    postData = postData..'Content-Type: '..contentType..'\r\n'
    postData = postData..'Content-Transfer-Encoding: binary'..'\r\n'
    postData = postData..'Content-Length: '..string.len(fileData).."\r\n"
    postData = postData.."\r\n"
    postData = postData..fileData.."\r\n"
end

-- generate unique profile id
--
local id = util.unique()
local service = "Facebook"
local post_data = "{\"service\":\""..service.."\",\"personID\":\""..id.."\"}"

-- request to create a profile
local response = http.post({
  url = "https://api.loadimpactsample.com/profiles", 
  data = post_data,
  headers= {
      ["Accept"]="application/vnd.playground.profile+json; version=1.0; charset=UTF-8",
      ["Authorization"]="x-token oFpXeHV5E-WTswHQqC0rPQ",
      ["Content-Type"]="application/vnd.playground.profile-query+json; version=1.0; charset=UTF-8"
    }, 
  response_body_bytes = 10000
})

log.info(response.body)
local profileID = json.parse(response.body).profileID

--time for es to refresh
client.sleep(1)

-- writing post body
writeJSONContent()
writeFileContent("image/png", 1) -- size in MB
writeFileContent("audio/ogg", 2) -- size in MB
postData = postData.."--"..boundary.."--"


--video upload request
local response = http.post({
    url = "https://api.loadimpactsample.com/profiles/"..profileID,
    data = postData,
    auto_redirect = false,
    headers = {
      ["Accept"] = "application/vnd.playground.audio+json; version=1.0; charset=UTF-8",
        ["Authorization"] = "x-token oFpXeHV5E-WTswHQqC0rPQ",
        ["Content-Type"] = "multipart/mixed; boundary="..boundary,
        ["Transfer-Encoding"] = "chunked"
    },
    response_body_bytes = 100000
})

if(response.status_code == 504) then
  log.info(uid)
end
log.info(response.status_code)

Feedback and Knowledge Base