<commit_before>local lunit, RUN = lunit do
RUN   = lunit and function()end or function ()
  local res = lunit.run()
  if res.errors + res.failed > 0 then
    os.exit(-1)
  end
  return os.exit(0)
end
lunit = require "lunit"
end

local _,luacov   = pcall(require, "luacov")
local TEST_CASE  = assert(lunit.TEST_CASE)
local skip       = lunit.skip or function() end

local curl       = require "cURL"
local scurl      = require "cURL.safe"
local json       = require "dkjson"
local fname      = "./test.download"

local utils = require "utils"

local function weak_ptr(val)
  return setmetatable({value = val},{__mode = 'v'})
end

local function gc_collect()
  for i = 1, 5 do
    collectgarbage("collect")
  end
end

-- Bug. libcurl 7.56.0 does not add `Content-Type: text/plain`
local text_plain = utils.is_curl_eq(7,56,0) and 'test/plain' or 'text/plain'

local GET_URL = "http://127.0.0.1:7090/get"

local ENABLE = true

local _ENV = TEST_CASE'version'        if ENABLE then

function test()
  assert_match("^%d+%.%d+%.%d+%-?", curl._VERSION)
  assert_equal("Lua-cURL",          curl._NAME   )
end

end

local _ENV = TEST_CASE'easy'           if ENABLE then

local e1, e2
function teardown()
  if e1 then e1:close() end
  if e2 then e2:close() end
  e1, e2 = nil
end

if curl.OPT_STREAM_DEPENDS then

function test_easy_setopt_stream_depends_1()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt_stream_depends(e2)
  end)
end

function test_easy_setopt_stream_depends_2()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt(curl.OPT_STREAM_DEPENDS, e2)
  end)
end

function test_easy_setopt_stream_depends_3()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt{[curl.OPT_STREAM_DEPENDS] = e2}
  end)
end

function test_easy_setopt_stream_depends_4()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt{stream_depends = e2}
  end)
end

function test_easy_setopt_stream_depends_e_1()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt_stream_depends_e(e2)
  end)
end

function test_easy_setopt_stream_depends_e_2()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt(curl.OPT_STREAM_DEPENDS_E, e2)
  end)
end

function test_easy_setopt_stream_depends_e_3()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt{[curl.OPT_STREAM_DEPENDS_E] = e2}
  end)
end

function test_easy_setopt_stream_depends_e_4()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt{stream_depends_e = e2}
  end)
end

end

function test_easy_setopt_share()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.share())
  assert_pass(function()
    e1:setopt_share(e2)
  end)
end

end

local _ENV = TEST_CASE'multi_iterator' if ENABLE then

local url = GET_URL

local c, t, m

local function json_data()
  return json.decode(table.concat(t))
end

function setup()
  t = {}
  m = assert(scurl.multi())
end

function teardown()
  if m then m:close() end
  if c then c:close() end
  m, c, t = nil
end

function test_add_handle()
  local base_url = url .. '?key='
  local urls = {
    base_url .. "1",
    base_url .. "2",
    "###" .. base_url .. "3",
    base_url .. "4",
    base_url .. "5",
  }

  local i = 0
  local function next_easy()
    i = i + 1
    local url = urls[i]
    if url then 
      c = assert(scurl.easy{url = url})
      t = {}
      return c
    end
  end

  assert_equal(m, m:add_handle(next_easy()))

  for data, type, easy in m:iperform() do

    if type == "done" or type == "error" then
      assert_equal(urls[i], easy:getinfo_effective_url())
      assert_equal(easy, c)
      easy:close()
      c = nil

      if i == 3 then
        assert_equal(curl.error(curl.ERROR_EASY, curl.E_UNSUPPORTED_PROTOCOL), data)
      else
        local data = json_data()
        assert_table(data.args)
        assert_equal(tostring(i), data.args.key)
      end

      easy = next_easy()
      if easy then m:add_handle(easy) end
    end

    if type == "data" then table.insert(t, data) end

  end

  assert_equal(#urls + 1, i)
  assert_nil(c)
end

function test_info_read()

  local url = GET_URL .. '?key=1'<commit_msg>Update test<commit_after>local lunit, RUN = lunit do
RUN   = lunit and function()end or function ()
  local res = lunit.run()
  if res.errors + res.failed > 0 then
    os.exit(-1)
  end
  return os.exit(0)
end
lunit = require "lunit"
end

local _,luacov   = pcall(require, "luacov")
local TEST_CASE  = assert(lunit.TEST_CASE)
local skip       = lunit.skip or function() end

local curl       = require "cURL"
local scurl      = require "cURL.safe"
local json       = require "dkjson"
local fname      = "./test.download"

local utils = require "utils"

local function weak_ptr(val)
  return setmetatable({value = val},{__mode = 'v'})
end

local function gc_collect()
  for i = 1, 5 do
    collectgarbage("collect")
  end
end

-- Bug. libcurl 7.56.0 does not add `Content-Type: text/plain`
local text_plain = utils.is_curl_eq(7,56,0) and 'test/plain' or 'text/plain'

local GET_URL = "http://127.0.0.1:7090/get"

local ENABLE = true

local _ENV = TEST_CASE'version'        if ENABLE then

function test()
  assert_match("^%d+%.%d+%.%d+%-?", curl._VERSION)
  assert_equal("Lua-cURL",          curl._NAME   )
end

end

local _ENV = TEST_CASE'easy'           if ENABLE then

local e1, e2
function teardown()
  if e1 then e1:close() end
  if e2 then e2:close() end
  e1, e2 = nil
end

if curl.OPT_STREAM_DEPENDS then

function test_easy_setopt_stream_depends_1()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt_stream_depends(e2)
  end)
end

function test_easy_setopt_stream_depends_2()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt(curl.OPT_STREAM_DEPENDS, e2)
  end)
end

function test_easy_setopt_stream_depends_3()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt{[curl.OPT_STREAM_DEPENDS] = e2}
  end)
end

function test_easy_setopt_stream_depends_4()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt{stream_depends = e2}
  end)
end

function test_easy_setopt_stream_depends_e_1()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt_stream_depends_e(e2)
  end)
end

function test_easy_setopt_stream_depends_e_2()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt(curl.OPT_STREAM_DEPENDS_E, e2)
  end)
end

function test_easy_setopt_stream_depends_e_3()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt{[curl.OPT_STREAM_DEPENDS_E] = e2}
  end)
end

function test_easy_setopt_stream_depends_e_4()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.easy())
  assert_pass(function()
    e1:setopt{stream_depends_e = e2}
  end)
end

end

function test_easy_setopt_share()
  e1 = assert(scurl.easy())
  e2 = assert(scurl.share())
  assert_pass(function()
    e1:setopt_share(e2)
  end)
end

end

local _ENV = TEST_CASE'multi_iterator' if ENABLE then

local url = GET_URL

local c, t, m

local function json_data()
  return json.decode(table.concat(t))
end

function setup()
  t = {}
  m = assert(scurl.multi())
end

function teardown()
  if m then m:close() end
  if c then c:close() end
  m, c, t = nil
end

function test_add_handle()
  local base_url = url .. '?key='
  local urls = {
    base_url .. "1",
    base_url .. "2",
    "###" .. base_url .. "3",
    base_url .. "4",
    base_url .. "5",
  }

  local i = 0
  local function next_easy()
    i = i + 1
    local url = urls[i]
    if url then 
      c = assert(scurl.easy{url = url})
      t = {}
      return c
    end
  end

  assert_equal(m, m:add_handle(next_easy()))

  for data, type, easy in m:iperform() do

    if type == "done" or type == "error" then
      assert_equal(urls[i], easy:getinfo_effective_url())
      assert_equal(easy, c)
      easy:close()
      c = nil

      if i == 3 then
        if is_curl_ge(7, 62,0) then
          assert_equal(curl.error(curl.ERROR_EASY, curl.E_URL_MALFORMAT), data)
        else
          assert_equal(curl.error(curl.ERROR_EASY, curl.E_UNSUPPORTED_PROTOCOL), data)
        end
      else
        local data = json_data()
        assert_table(data.args)
        assert_equal(tostring(i), data.args.key)
      end

      easy = next_easy()
      if easy then m:add_handle(easy) end
    end

    if type == "data" then table.insert(t, data) end

  end

  assert_equal(#urls + 1, i)
  assert_nil(c)
end

function test_info_read()

  local url = GET_URL .. '?key=1'
{'fraction_non_alphanumeric': 0.12713687308955055, 'fraction_numerical': 0.01969885656062493, 'mean_word_length': 3.360315893385982, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2533129', 'n_tokens_mistral': 3571, 'n_tokens_neox': 3443, 'n_words': 785}