Skip to content

Commit

Permalink
Openresty Update: Fix random ngx.encode_args
Browse files Browse the repository at this point in the history
On ngx.encode_args, the encoding is not longer sorted because Luajit
changed. This way always set the encode on the sorted way to make test
easier to validate.

Signed-off-by: Eloy Coto <[email protected]>
  • Loading branch information
eloycoto committed May 24, 2021
1 parent 366a224 commit df3b9ea
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions spec/backend_client_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,35 @@ local http_ng_resty = require 'resty.http_ng.backend.resty'
local ReportsBatch = require 'apicast.policy.3scale_batcher.reports_batch'
local backend_calls_metrics = require 'apicast.metrics.3scale_backend_calls'

function encode(data)
tablex = require "pl.tablex"

if tablex.size(data) == 0 then
return ""
end

local result = ""
local first = true

for k,v in tablex.sort(data) do
local prefix = "&"
if first then
prefix = ""
first = false
end
result = string.format("%s%s%s=%s",result, prefix, k, v)
end
return result
end

describe('backend client', function()

local test_backend
local options_header_oauth_native = 'rejection_reason_header=1&limit_headers=1'
local options_header_no_oauth_native = 'rejection_reason_header=1&limit_headers=1&no_body=1'

before_each(function()
stub(ngx, 'encode_args', encode)
test_backend = http_ng.backend()
stub(backend_calls_metrics, 'report')
end)
Expand Down Expand Up @@ -48,8 +70,7 @@ describe('backend client', function()
})
test_backend.expect{
url = 'http://example.com/transactions/authrep.xml?' ..
ngx.encode_args({ auth = service.backend_authentication.value, service_id = service.id }) ..
'&usage%5Bhits%5D=1&user_key=foobar',
string.format("auth=val&service_id=42&usage[hits]=1&user_key=foobar"),
headers = { ['3scale-options'] = options_header_no_oauth_native }
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
Expand Down Expand Up @@ -138,7 +159,7 @@ describe('backend client', function()
})
test_backend.expect{
url = 'http://example.com/transactions/authorize.xml?' ..
ngx.encode_args({ auth = service.backend_authentication.value, service_id = service.id }),
ngx.encode_args({ service_id = service.id, auth = service.backend_authentication.value }),
headers = { host = 'example.com',
['3scale-options'] = options_header_no_oauth_native }
}.respond_with{ status = 200 }
Expand All @@ -159,8 +180,7 @@ describe('backend client', function()
})
test_backend.expect{
url = 'http://example.com/transactions/authorize.xml?' ..
ngx.encode_args({ auth = service.backend_authentication.value, service_id = service.id }) ..
'&usage%5Bhits%5D=1&user_key=foobar',
string.format("auth=%s&service_id=%s&usage[hits]=1&user_key=foobar",service.backend_authentication.value, service.id),
headers = { ['3scale-options'] = options_header_no_oauth_native }
}.respond_with{ status = 200 }
local backend_client = assert(_M:new(service, test_backend))
Expand Down Expand Up @@ -273,8 +293,8 @@ describe('backend client', function()

test_backend.expect{
url = 'http://example.com/transactions.xml?' ..
ngx.encode_args({ auth = service.backend_authentication.value,
service_id = service.id }),
ngx.encode_args({ service_id = service.id,
auth = service.backend_authentication.value}),
body = ngx.encode_args(transactions)
}.respond_with{ status = 200 }

Expand Down Expand Up @@ -305,8 +325,8 @@ describe('backend client', function()

test_backend.expect{
url = 'http://example.com/transactions.xml?' ..
ngx.encode_args({ auth = service.backend_authentication.value,
service_id = service.id }),
ngx.encode_args({ service_id = service.id,
auth = service.backend_authentication.value}),
body = ngx.encode_args(transactions)
}.respond_with{ status = 200 }

Expand Down Expand Up @@ -337,8 +357,8 @@ describe('backend client', function()

test_backend.expect{
url = 'http://example.com/transactions.xml?' ..
ngx.encode_args({ auth = service.backend_authentication.value,
service_id = service.id }),
ngx.encode_args({ service_id = service.id,
auth = service.backend_authentication.value}),
body = ngx.encode_args(transactions)
}.respond_with{ status = 200 }

Expand All @@ -362,6 +382,8 @@ describe('backend client', function()

describe('store_oauth_token', function()
it('makes the right call to backend', function()


local service_id = '42'
local service = configuration.parse_service({
id = service_id,
Expand All @@ -376,16 +398,17 @@ describe('backend client', function()
test_backend.expect{
-- Notice that the service_id appears twice, but it's not a problem.
url = 'http://example.com/services/42/'..
'oauth_access_tokens.xml?service_token=123&service_id=42',
body = 'user_id=a_user_id&ttl=3600&token=my_token&app_id=an_app_id',
'oauth_access_tokens.xml?service_id=42&service_token=123',
body = 'app_id=an_app_id&token=my_token&ttl=3600&user_id=a_user_id'
}.respond_with{ status = 200 }

local backend_client = assert(_M:new(service, test_backend))

local res = backend_client:store_oauth_token({
app_id = 'an_app_id',
token = 'my_token',
ttl = 3600,
app_id = 'an_app_id',
user_id = 'a_user_id'
user_id = 'a_user_id',
})

assert.equal(200, res.status)
Expand Down

0 comments on commit df3b9ea

Please sign in to comment.