From a8ee41d52b63f32c88bad9c9a647bf6bc0689808 Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Thu, 6 Aug 2015 15:06:03 -0700 Subject: [PATCH] fix(analytics) alf serializer stringifies records As specified by ALF specs: https://github.com/ahmadnassri/har-validator/blob/8fd21c30edb23a1fed2d50b934d055d1be3dd7c9/lib/schemas/record.json#L12 --- kong/plugins/log_serializers/alf.lua | 2 ++ .../mashape-analytics/alf_serializer_spec.lua | 13 ++++++++++--- .../plugins/mashape-analytics/fixtures/requests.lua | 5 +++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/kong/plugins/log_serializers/alf.lua b/kong/plugins/log_serializers/alf.lua index 270db459220..7467658c131 100644 --- a/kong/plugins/log_serializers/alf.lua +++ b/kong/plugins/log_serializers/alf.lua @@ -36,6 +36,8 @@ local function dic_to_array(hash, fn) v = {v} end for _, val in ipairs(v) do + k = tostring(k) + val = tostring(val) table.insert(arr, {name = k, value = val}) fn(k, val) end diff --git a/spec/plugins/mashape-analytics/alf_serializer_spec.lua b/spec/plugins/mashape-analytics/alf_serializer_spec.lua index 21403f4682f..a076cedaadd 100644 --- a/spec/plugins/mashape-analytics/alf_serializer_spec.lua +++ b/spec/plugins/mashape-analytics/alf_serializer_spec.lua @@ -28,9 +28,9 @@ local function sameEntry(state, arguments) assert.True(math.abs(entry.time - fixture_entry.time) < delta) -- Compare things that are not computed in the same order depending on the platform - assert.are.equal(#(fixture_entry.request.headers), #(entry.request.headers)) - assert.are.equal(#(fixture_entry.request.queryString), #(entry.request.queryString)) - assert.are.equal(#(fixture_entry.response.headers), #(entry.response.headers)) + assert.equal(#(fixture_entry.request.headers), #(entry.request.headers)) + assert.equal(#(fixture_entry.request.queryString), #(entry.request.queryString)) + assert.equal(#(fixture_entry.response.headers), #(entry.response.headers)) entry.time = nil entry.timings = nil @@ -98,5 +98,12 @@ describe("ALF serializer", function() local alf = ALFSerializer.new_alf(fixtures.GET.NGX_STUB, "123456", "test") assert.equal("test", alf.environment) end) + -- https://github.com/ahmadnassri/har-validator/blob/8fd21c30edb23a1fed2d50b934d055d1be3dd7c9/lib/schemas/record.json#L12 + it("should convert all records to strings", function() + local alf = ALFSerializer.new_alf(fixtures.GET.NGX_STUB, "123456", "test") + for _, record in ipairs(alf.har.log.entries[1].request.queryString) do + assert.equal("string", type(record.value)) + end + end) end) end) diff --git a/spec/plugins/mashape-analytics/fixtures/requests.lua b/spec/plugins/mashape-analytics/fixtures/requests.lua index 3f4fa453a3c..4a5fa0c87da 100644 --- a/spec/plugins/mashape-analytics/fixtures/requests.lua +++ b/spec/plugins/mashape-analytics/fixtures/requests.lua @@ -8,7 +8,7 @@ return { get_method = function() return "GET" end, http_version = function() return 1.1 end, get_headers = function() return {["Accept"] = "/*/", ["Host"] = "mockbin.com"} end, - get_uri_args = function() return {["hello"] = "world", ["foo"] = "bar"} end, + get_uri_args = function() return {["hello"] = "world", ["foo"] = "bar", ["number"] = 2} end, get_post_args = function() return {["hello"] = {"world", "earth"}} end }, resp = { @@ -56,7 +56,8 @@ return { }, queryString = { {name = "foo", value = "bar"}, - {name = "hello", value = "world"} + {name = "hello", value = "world"}, + {name = "hello", value = "2"} }, url = "http://mockbin.com/request" },