Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[Bug #5811] Serialize parameters to Json when content type indicates

- Delegate serialization (as before) when GET
- Also detect vendor prefixed Json
- Invoke user-defined request configuration before internal one
  • Loading branch information...
commit 567ff9fb7ad242fbc5d5ec3b69bbf3d69c56f3d8 1 parent 6353f18
trkoch authored
View
11 framework/source/class/qx/io/rest/Resource.js
@@ -343,11 +343,12 @@ qx.Class.define("qx.io.rest.Resource",
this.__checkParameters(params, config.check);
- this.__configureRequest(req, data, action, config);
if (this.__configureRequestCallback) {
this.__configureRequestCallback.call(this, req, action, params);
}
+ this.__configureRequest(req, data, action, config);
+
// Handle successful request
req.addListenerOnce("success", function successHandler() {
var props = [req.getResponse(), null, false, req, action, req.getPhase()];
@@ -425,6 +426,14 @@ qx.Class.define("qx.io.rest.Resource",
req.set({method: config.method, url: config.url});
if (data) {
+ var contentType = req.getRequestHeader("Content-Type");
+
+ if (qx.util.Request.methodAllowsRequestBody(req.getMethod())) {
+ if ((/application\/.*\+?json/).test(contentType)) {
+ data = qx.lang.Json.stringify(data);
+ }
+ }
+
req.setRequestData(data);
}
},
View
37 framework/source/class/qx/test/io/rest/Resource.js
@@ -117,13 +117,13 @@ qx.Class.define("qx.test.io.rest.Resource",
this.assertCalled(callback);
},
- "test: configure request receives pre-configured but unsent request": function() {
+ "test: configure request receives vanilla request": function() {
var res = this.res,
req = this.req;
res.configureRequest(qx.lang.Function.bind(function(req) {
- this.assertCalledWith(req.setMethod, "GET");
- this.assertCalledWith(req.setUrl, "/photos");
+ this.assertNotCalled(req.setMethod);
+ this.assertNotCalled(req.setUrl);
this.assertNotCalled(req.send);
}, this));
@@ -394,6 +394,37 @@ qx.Class.define("qx.test.io.rest.Resource",
this.assertCalledWith(req.setUrl, "/photos/1/comments?id=2");
},
+ "test: invoke action when content type json": function() {
+ var res = this.res,
+ req = this.req;
+
+ req.setRequestHeader.restore();
+ req.getRequestHeader.restore();
+
+ res.configureRequest(function(req) {
+ req.setRequestHeader("Content-Type", "application/json");
+ });
+
+ res.map("post", "POST", "/photos/{id}/meta");
+ res.post({id: 1, location: "Karlsruhe"});
+
+ this.assertCalledWith(req.setRequestData, '{"location":"Karlsruhe"}');
+ },
+
+ "test: invoke action when content type json and get": function() {
+ var res = this.res,
+ req = this.req;
+
+ req.setMethod.restore();
+ req.getMethod.restore();
+
+ this.spy(qx.lang.Json, "stringify");
+ req.getRequestHeader.withArgs("Content-Type").returns("application/json");
+ res.get();
+
+ this.assertNotCalled(qx.lang.Json.stringify);
+ },
+
"test: invoke action for url with port": function() {
var res = this.res,
req = this.req;
Please sign in to comment.
Something went wrong with that request. Please try again.