|
3 | 3 | var assert = require('node:assert') |
4 | 4 | var express = require('../') |
5 | 5 | , request = require('supertest'); |
| 6 | +var qs = require('qs'); |
6 | 7 |
|
7 | 8 | describe('req', function(){ |
8 | 9 | describe('.query', function(){ |
@@ -38,6 +39,22 @@ describe('req', function(){ |
38 | 39 | .get('/?user.name=tj') |
39 | 40 | .expect(200, '{"user.name":"tj"}', done); |
40 | 41 | }); |
| 42 | + |
| 43 | + it('should not be able to access object prototype properties', function (done) { |
| 44 | + var app = createApp('extended', true); |
| 45 | + |
| 46 | + request(app) |
| 47 | + .get('/?foo=yee') |
| 48 | + .expect(200, /TypeError: req\.query\.hasOwnProperty is not a function/, done); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should be able to use object prototype property names as keys', function (done) { |
| 52 | + var app = createApp('extended', true); |
| 53 | + |
| 54 | + request(app) |
| 55 | + .get('/?hasOwnProperty=yee') |
| 56 | + .expect(200, '{"query":{"hasOwnProperty":"yee"},"error":"TypeError: req.query.hasOwnProperty is not a function"}', done); |
| 57 | + }); |
41 | 58 | }); |
42 | 59 |
|
43 | 60 | describe('when "query parser" is simple', function () { |
@@ -48,6 +65,22 @@ describe('req', function(){ |
48 | 65 | .get('/?user%5Bname%5D=tj') |
49 | 66 | .expect(200, '{"user[name]":"tj"}', done); |
50 | 67 | }); |
| 68 | + |
| 69 | + it('should not be able to access object prototype properties', function (done) { |
| 70 | + var app = createApp('simple', true); |
| 71 | + |
| 72 | + request(app) |
| 73 | + .get('/?foo=yee') |
| 74 | + .expect(200, /TypeError: req\.query\.hasOwnProperty is not a function/, done); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should be able to use object prototype property names as keys', function (done) { |
| 78 | + var app = createApp('simple', true); |
| 79 | + |
| 80 | + request(app) |
| 81 | + .get('/?hasOwnProperty=yee') |
| 82 | + .expect(200, '{"query":{"hasOwnProperty":"yee"},"error":"TypeError: req.query.hasOwnProperty is not a function"}', done); |
| 83 | + }); |
51 | 84 | }); |
52 | 85 |
|
53 | 86 | describe('when "query parser" is a function', function () { |
@@ -60,6 +93,18 @@ describe('req', function(){ |
60 | 93 | .get('/?user%5Bname%5D=tj') |
61 | 94 | .expect(200, '{"length":17}', done); |
62 | 95 | }); |
| 96 | + |
| 97 | + // test exists to verify behavior for folks wishing to workaround our qs defaults |
| 98 | + it('should drop object prototype property names and be able to access object prototype properties', function (done) { |
| 99 | + var app = createApp( |
| 100 | + function (str) { |
| 101 | + return qs.parse(str) |
| 102 | + }, true); |
| 103 | + |
| 104 | + request(app) |
| 105 | + .get('/?hasOwnProperty=biscuits') |
| 106 | + .expect(200, '{"query":{},"hasOwnProperty":false}', done); |
| 107 | + }); |
63 | 108 | }); |
64 | 109 |
|
65 | 110 | describe('when "query parser" disabled', function () { |
@@ -70,6 +115,22 @@ describe('req', function(){ |
70 | 115 | .get('/?user%5Bname%5D=tj') |
71 | 116 | .expect(200, '{}', done); |
72 | 117 | }); |
| 118 | + |
| 119 | + it('should not be able to access object prototype properties', function (done) { |
| 120 | + var app = createApp('extended', true); |
| 121 | + |
| 122 | + request(app) |
| 123 | + .get('/?foo=yee') |
| 124 | + .expect(200, /TypeError: req\.query\.hasOwnProperty is not a function/, done); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should be able to use object prototype property names as keys', function (done) { |
| 128 | + var app = createApp('extended', true); |
| 129 | + |
| 130 | + request(app) |
| 131 | + .get('/?hasOwnProperty=yee') |
| 132 | + .expect(200, '{"query":{"hasOwnProperty":"yee"},"error":"TypeError: req.query.hasOwnProperty is not a function"}', done); |
| 133 | + }); |
73 | 134 | }); |
74 | 135 |
|
75 | 136 | describe('when "query parser" enabled', function () { |
@@ -80,6 +141,22 @@ describe('req', function(){ |
80 | 141 | .get('/?user%5Bname%5D=tj') |
81 | 142 | .expect(200, '{"user[name]":"tj"}', done); |
82 | 143 | }); |
| 144 | + |
| 145 | + it('should not be able to access object prototype properties', function (done) { |
| 146 | + var app = createApp('extended', true); |
| 147 | + |
| 148 | + request(app) |
| 149 | + .get('/?foo=yee') |
| 150 | + .expect(200, /TypeError: req\.query\.hasOwnProperty is not a function/, done); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should be able to use object prototype property names as keys', function (done) { |
| 154 | + var app = createApp('extended', true); |
| 155 | + |
| 156 | + request(app) |
| 157 | + .get('/?hasOwnProperty=yee') |
| 158 | + .expect(200, '{"query":{"hasOwnProperty":"yee"},"error":"TypeError: req.query.hasOwnProperty is not a function"}', done); |
| 159 | + }); |
83 | 160 | }); |
84 | 161 |
|
85 | 162 | describe('when "query parser" an unknown value', function () { |
@@ -91,15 +168,25 @@ describe('req', function(){ |
91 | 168 | }) |
92 | 169 | }) |
93 | 170 |
|
94 | | -function createApp(setting) { |
| 171 | +function createApp(setting, isPrototypePropertyTest) { |
95 | 172 | var app = express(); |
96 | 173 |
|
97 | 174 | if (setting !== undefined) { |
98 | 175 | app.set('query parser', setting); |
99 | 176 | } |
100 | 177 |
|
101 | 178 | app.use(function (req, res) { |
102 | | - res.send(req.query); |
| 179 | + if(isPrototypePropertyTest) { |
| 180 | + try { |
| 181 | + var hasOwnProperty = req.query.hasOwnProperty('✨ express ✨'); |
| 182 | + res.send({ query: req.query, hasOwnProperty: hasOwnProperty }); |
| 183 | + } catch (error) { |
| 184 | + res.send({ query: req.query, error: error.toString() }); |
| 185 | + } |
| 186 | + } |
| 187 | + else { |
| 188 | + res.send(req.query); |
| 189 | + } |
103 | 190 | }); |
104 | 191 |
|
105 | 192 | return app; |
|