This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
forked from heroku/heroku-kong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_spec.lua
144 lines (110 loc) Β· 4.46 KB
/
handler_spec.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
local handler = require "kong.plugins.ndfd-xml-as-json.handler"
local json = require "cjson"
local xml = require "xml"
describe("kong.plugins.ndfd-xml-as-json.handler", function()
it("should return a table", function()
assert.is.equal("table", type(handler))
end)
describe("#new", function()
it("is a function", function()
assert.is.equal("function", type(handler.new))
end)
end)
describe("#access", function()
it("is a function", function()
assert.is.equal("function", type(handler.access))
end)
it("should transform request JSON to XML", function()
local params = {
latitude = 38.99,
longitude = -77.01
}
local body_data = json.encode(params)
local headers = {}
ngx.req.read_body = function() return nil end
ngx.req.get_body_data = function() return body_data end
ngx.req.set_body_data = function(new_data) body_data = new_data end
ngx.req.set_header = function(k,v) headers[k] = v end
ngx.req.clear_header = function(k) headers[k] = nil end
local subject = handler()
subject:access()
local req_xml = xml.load(body_data)
assert.is.equal(string.format("%f", params.latitude), xml.find(req_xml, 'latitude')[1])
assert.is.equal(string.format("%f", params.longitude), xml.find(req_xml, 'longitude')[1])
assert.is.equal("1", xml.find(req_xml, 'maxt')[1])
assert.is.equal("text/xml", headers["Content-Type"])
end)
end)
describe("#header_filter", function()
it("is a function", function()
assert.is.equal("function", type(handler.header_filter))
end)
end)
describe("#body_filter", function()
it("is a function", function()
assert.is.equal("function", type(handler.body_filter))
end)
describe("for a typical XML response", function()
before_each(function()
local data_file = io.open("./spec/data/ndfd-response.xml", "r")
local body_data = data_file:read("*a")
data_file:close()
ngx.arg = {}
ngx.arg[1] = body_data
ngx.arg[2] = true -- EOF flag
subject = handler()
end)
it("should transform response XML to JSON", function()
subject:body_filter()
local res_json = json.decode(ngx.arg[1])
assert.is.equal('temperature', res_json.name)
assert.is.equal('maximum', res_json.type)
assert.is.equal('Fahrenheit', res_json.units)
assert.is.equal('38.99', res_json.latitude)
assert.is.equal('-77.01', res_json.longitude)
assert.is.equal("table", type(res_json.values))
end)
it("returns a list of merged values & times", function()
subject:body_filter()
local res_json = json.decode(ngx.arg[1])
assert.is.equal("46", res_json.values[1].value)
assert.is.equal("2016-01-14T07:00:00-05:00", res_json.values[1].time)
assert.is.equal("49", res_json.values[2].value)
assert.is.equal("2016-01-15T07:00:00-05:00", res_json.values[2].time)
assert.is.equal("48", res_json.values[3].value)
assert.is.equal("2016-01-16T07:00:00-05:00", res_json.values[3].time)
assert.is.equal("38", res_json.values[4].value)
assert.is.equal("2016-01-17T07:00:00-05:00", res_json.values[4].time)
assert.is.equal("28", res_json.values[5].value)
assert.is.equal("2016-01-18T07:00:00-05:00", res_json.values[5].time)
assert.is.equal("30", res_json.values[6].value)
assert.is.equal("2016-01-19T07:00:00-05:00", res_json.values[6].time)
assert.is.equal("35", res_json.values[7].value)
assert.is.equal("2016-01-20T07:00:00-05:00", res_json.values[7].time)
end)
end)
it("should collect chunked response", function()
data_file = io.open("./spec/data/ndfd-response.xml", "r")
local subject = handler()
ngx.arg = {}
-- Chunk by line in the data file
ngx.arg[1] = data_file:read("*l")
subject:body_filter()
ngx.arg[1] = data_file:read("*l")
subject:body_filter()
ngx.arg[1] = data_file:read("*l")
subject:body_filter()
ngx.arg[1] = data_file:read("*l")
subject:body_filter()
ngx.arg[1] = data_file:read("*l")
subject:body_filter()
-- All the remaining content
ngx.arg[1] = data_file:read("*a")
ngx.arg[2] = true -- EOF flag
subject:body_filter()
data_file:close()
local res_json = json.decode(ngx.arg[1])
assert.is.equal('maximum', res_json.type)
end)
end)
end)