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

Commit

Permalink
Add HttpService:UrlEncode (#173) (#182)
Browse files Browse the repository at this point in the history
* Add HttpService:UrlEncode (#173)

* Escape sequence/forgot whitepsace

* Wrong language with +, change to ..
  • Loading branch information
jacobselbo authored and LPGhatguy committed Feb 20, 2019
1 parent 03b70f7 commit 34a701f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
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

0 comments on commit 34a701f

Please sign in to comment.