From 34a701f39520af5e0c13aa5b00b935bce815b274 Mon Sep 17 00:00:00 2001 From: Jacob Date: Wed, 20 Feb 2019 12:58:34 -0500 Subject: [PATCH] Add HttpService:UrlEncode (#173) (#182) * Add HttpService:UrlEncode (#173) * Escape sequence/forgot whitepsace * Wrong language with +, change to .. --- lib/instances/HttpService.lua | 8 ++++++++ lib/instances/HttpService_spec.lua | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/instances/HttpService.lua b/lib/instances/HttpService.lua index 93e3004..cc2588c 100644 --- a/lib/instances/HttpService.lua +++ b/lib/instances/HttpService.lua @@ -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 diff --git a/lib/instances/HttpService_spec.lua b/lib/instances/HttpService_spec.lua index ca45d3a..708ca7f 100644 --- a/lib/instances/HttpService_spec.lua +++ b/lib/instances/HttpService_spec.lua @@ -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()