@@ -23,6 +23,51 @@ describe('Service object', function()
2323
2424 ngx .var = {}
2525 stub (ngx .req , ' get_method' , function () return ' POST' end )
26+ stub (ngx .req , ' get_headers' , function () return {[" Content-Type" ] = ' application/x-www-form-urlencoded' } end )
27+ stub (ngx .req , ' read_body' )
28+ stub (ngx .req , ' get_post_args' , function () return { user_key = ' post' } end )
29+
30+ assert .same ({ ' post' , user_key = ' post' }, assert (service :extract_credentials ()))
31+ end )
32+
33+ it (' unknown POST request returns empty' , function ()
34+ local service = Service .new ({
35+ backend_version = 1 ,
36+ credentials = { location = ' query' }
37+ })
38+
39+ ngx .var = {}
40+ stub (ngx .req , ' get_method' , function () return ' POST' end )
41+ -- No Content-Type header
42+ stub (ngx .req , ' get_headers' , function () return {} end )
43+
44+ assert .same ({}, assert (service :extract_credentials ()))
45+ end )
46+
47+ it (' urlencoded POST request without credentials' , function ()
48+ local service = Service .new ({
49+ backend_version = 1 ,
50+ credentials = { location = ' query' }
51+ })
52+
53+ ngx .var = {}
54+ stub (ngx .req , ' get_method' , function () return ' POST' end )
55+ stub (ngx .req , ' get_headers' , function () return {[" Content-Type" ] = ' application/x-www-form-urlencoded' } end )
56+ stub (ngx .req , ' read_body' )
57+ stub (ngx .req , ' get_post_args' , function () return {} end )
58+
59+ assert .same ({}, assert (service :extract_credentials ()))
60+ end )
61+
62+ it (' urlencoded POST request with multiple Content-Type headers' , function ()
63+ local service = Service .new ({
64+ backend_version = 1 ,
65+ credentials = { location = ' query' }
66+ })
67+
68+ ngx .var = {}
69+ stub (ngx .req , ' get_method' , function () return ' POST' end )
70+ stub (ngx .req , ' get_headers' , function () return {[" Content-Type" ] = {' other' , ' application/x-www-form-urlencoded' } } end )
2671 stub (ngx .req , ' read_body' )
2772 stub (ngx .req , ' get_post_args' , function () return { user_key = ' post' } end )
2873
@@ -74,6 +119,7 @@ describe('Service object', function()
74119
75120 ngx .var = {}
76121 stub (ngx .req , ' get_method' , function () return ' POST' end )
122+ stub (ngx .req , ' get_headers' , function () return {[" Content-Type" ] = ' application/x-www-form-urlencoded' } end )
77123 stub (ngx .req , ' read_body' )
78124 stub (ngx .req , ' get_post_args' , function () return { app_id = ' post' } end )
79125
@@ -128,6 +174,7 @@ describe('Service object', function()
128174
129175 ngx .var = {}
130176 stub (ngx .req , ' get_method' , function () return ' POST' end )
177+ stub (ngx .req , ' get_headers' , function () return {[" Content-Type" ] = ' application/x-www-form-urlencoded' } end )
131178 stub (ngx .req , ' read_body' )
132179 stub (ngx .req , ' get_post_args' , function () return { access_token = ' post' } end )
133180
0 commit comments