Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Add HttpService:UrlEncode (#173) #182

Merged
merged 3 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/instances/HttpService.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ function HttpService.prototype:JSONDecode(input)
return json.decode(input)
end

function HttpService.prototype:UrlEncode(input)
local url = input:gsub("\n", "\r\n")

return url:gsub("([^%w])", function(c)
return string.format("%%%02X", string.byte(c))
end)
end

function HttpService.prototype:GenerateGUID(wrapInCurlyBraces)
local argType = typeof(wrapInCurlyBraces)
if wrapInCurlyBraces ~= nil and argType ~= "boolean" then
Expand Down
9 changes: 9 additions & 0 deletions lib/instances/HttpService_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ describe("instances.HttpService", function()
assert.are.same(instance:JSONDecode("[1,true]"), { 1, true })
end)

it("should URL encode properly", function()
local instance = HttpService:new()

assert.equal(instance:UrlEncode("testing test&test !@#$%^&*()+" ..
"_-=~`<>,./?| François"),
"testing%20test%26test%20%21%40%23%24%25%5E%26%2A%28%29" ..
"%2B%5F%2D%3D%7E%60%3C%3E%2C%2E%2F%3F%7C%20Fran%C3%A7ois")
end)

describe("GenerateGUID", function()
it("should omit curly braces when wrapInCurlyBraces is false", function()
local instance = HttpService:new()
Expand Down