Skip to content

Commit

Permalink
Add test for curl_url_escape
Browse files Browse the repository at this point in the history
  • Loading branch information
bluesmoon committed Aug 8, 2023
1 parent b6ae22f commit 50135a7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/CurlHTTP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,27 @@ LibCURL.curl_easy_setopt(curl::CurlEasy, opt::Any, ptrval::Ptr) = curl_easy_seto
LibCURL.curl_easy_setopt(curl::CurlEasy, opt::Any, ptrval::AbstractString) = curl_easy_setopt(curl.handle, opt, ptrval)
LibCURL.curl_easy_setopt(curl::CurlEasy, opt::Any, param::Any) = curl_easy_setopt(curl.handle, opt, param)

LibCURL.curl_easy_escape(curl::CurlEasy, s, l) = (s_esc = curl_easy_escape(curl.handle, s, l); s_ret = deepcopy(pointer_to_string(s_esc)); curl_free(s_esc); s_ret;)
function LibCURL.curl_easy_escape(curl::CurlEasy, s, l)
s_esc = curl_easy_escape(curl.handle, s, l)
s_len = ccall(:strlen, Csize_t, (Ptr{Cvoid}, ), s_esc)
s_ret = Array{UInt8}(undef, s_len)

unsafe_copyto!(pointer(s_ret), s_esc, s_len)

curl_free(s_esc)
String(s_ret)
end

LibCURL.curl_easy_duphandle(curl::CurlEasy) = CurlEasy(curl_easy_duphandle(curl.handle))

"""
curl_url_escape(::CurlEasy, ::String) → String
* curl_url_escape(::CurlEasy, ::String) → String
* curl_url_escape(::String) → String
Use curl to do URL escaping
"""
curl_url_escape(curl::CurlEasy, s::AbstractString) = curl_easy_escape(curl, s, 0)
curl_url_escape(s::AbstractString) = curl_url_escape(CurlEasy(curl_easy_init()), s)

"""
Cleanup the `CurlHandle` automatically determining what needs to be done for `curl_easy` vs `curl_multi` handles.
Expand Down
9 changes: 7 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ function test_Certs()
@test CurlEasy(cacertpath=LibCURL.cacert) isa CurlEasy
end

function test_UrlEscape()
@test "hello%20world%2C%20how%20are%20you%3F%20I%27m%20fine%21%20%23escape" == curl_url_escape("hello world, how are you? I'm fine! #escape")
end

@testset "Curl" begin
@testset "GET" begin
test_GET()
Expand All @@ -335,6 +339,7 @@ end
@testset "writeCB" begin test_writeCB() end
@testset "headerCB" begin test_headerCB() end
@testset "multiPOST" begin test_multiPOST() end
@testset "test_multi_writeCB" begin test_multi_writeCB() end
@testset "test Certs" begin test_Certs() end
@testset "multi writeCB" begin test_multi_writeCB() end
@testset "Certs" begin test_Certs() end
@testset "URL Escape" begin test_UrlEscape() end
end

0 comments on commit 50135a7

Please sign in to comment.