Merge pull request #832 from sqren/master
Enable POSTing JSON objects with casper.open()
Showing
2 changed files
with
36 additions
and
1 deletions
... | @@ -1396,7 +1396,11 @@ Casper.prototype.open = function open(location, settings) { | ... | @@ -1396,7 +1396,11 @@ Casper.prototype.open = function open(location, settings) { |
1396 | // http data | 1396 | // http data |
1397 | if (settings.data) { | 1397 | if (settings.data) { |
1398 | if (utils.isObject(settings.data)) { // query object | 1398 | if (utils.isObject(settings.data)) { // query object |
1399 | settings.data = qs.encode(settings.data); | 1399 | if (settings.headers && settings.headers["Content-Type"] === "application/json") { |
1400 | settings.data = JSON.stringify(settings.data); // convert object to JSON notation | ||
1401 | } else { | ||
1402 | settings.data = qs.encode(settings.data); // escapes all characters except alphabetic, decimal digits and ,-_.!~*'() | ||
1403 | } | ||
1400 | } else if (!utils.isString(settings.data)) { | 1404 | } else if (!utils.isString(settings.data)) { |
1401 | throw new CasperError("open(): invalid request settings data value: " + settings.data); | 1405 | throw new CasperError("open(): invalid request settings data value: " + settings.data); |
1402 | } | 1406 | } | ... | ... |
... | @@ -99,6 +99,37 @@ casper.test.begin('open() POST casing tests', 2, { | ... | @@ -99,6 +99,37 @@ casper.test.begin('open() POST casing tests', 2, { |
99 | } | 99 | } |
100 | }); | 100 | }); |
101 | 101 | ||
102 | casper.test.begin('open() POST json object', 2, { | ||
103 | setUp: setUp, | ||
104 | tearDown: tearDown, | ||
105 | test: function(test) { | ||
106 | casper.open('tests/site/index.html', { | ||
107 | method: 'POST', | ||
108 | headers: { | ||
109 | 'Content-Type': 'application/json' | ||
110 | }, | ||
111 | data: { | ||
112 | plop: 42, | ||
113 | chuck: 'norris', | ||
114 | john: {'Doe': 'is here'} | ||
115 | } | ||
116 | }).then(function() { | ||
117 | test.pass("Casper.open() can POST a JSON object"); | ||
118 | test.assertEquals(usedSettings, { | ||
119 | method: "POST", | ||
120 | headers: { | ||
121 | 'Content-Type': 'application/json' | ||
122 | }, | ||
123 | data: '{"plop":42,"chuck":"norris","john":{"Doe":"is here"}}' | ||
124 | }, "Casper.open() used the expected POST settings"); | ||
125 | }); | ||
126 | |||
127 | casper.run(function() { | ||
128 | test.done(); | ||
129 | }); | ||
130 | } | ||
131 | }); | ||
132 | |||
102 | casper.test.begin('open() PUT tests', 2, { | 133 | casper.test.begin('open() PUT tests', 2, { |
103 | setUp: setUp, | 134 | setUp: setUp, |
104 | tearDown: tearDown, | 135 | tearDown: tearDown, | ... | ... |
-
Please register or sign in to post a comment