Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Option to pass "headers" when using "open()" on "webpage" module.

  • Loading branch information...
commit ac771a22b97f930fcab2cc5ab2b0bdb4f95ae757 1 parent 03e47a1
@detro detro authored
Showing with 49 additions and 6 deletions.
  1. +8 −0 src/modules/webpage.js
  2. +11 −2 src/webpage.cpp
  3. +30 −4 test/webserver-spec.js
View
8 src/modules/webpage.js
@@ -158,6 +158,14 @@ exports.create = function (opts) {
data: arg2
}, this.settings);
return;
+ } else if (arguments.length === 5) {
+ this.onLoadFinished = arg4;
+ this.openUrl(url, {
+ operation: arg1,
+ data: arg2,
+ headers : arg3
+ }, this.settings);
+ return;
}
throw "Wrong use of WebPage#open";
};
View
13 src/webpage.cpp
@@ -45,6 +45,7 @@
#include <QWebFrame>
#include <QWebPage>
#include <QWebInspector>
+#include <QMapIterator>
#include "networkaccessmanager.h"
#include "utils.h"
@@ -346,6 +347,7 @@ void WebPage::openUrl(const QString &address, const QVariant &op, const QVariant
{
QString operation;
QByteArray body;
+ QNetworkRequest request;
applySettings(settings);
m_webPage->triggerAction(QWebPage::Stop);
@@ -356,6 +358,13 @@ void WebPage::openUrl(const QString &address, const QVariant &op, const QVariant
if (op.type() == QVariant::Map) {
operation = op.toMap().value("operation").toString();
body = op.toMap().value("data").toByteArray();
+ if (op.toMap().contains("headers")) {
+ QMapIterator<QString, QVariant> i(op.toMap().value("headers").toMap());
+ while (i.hasNext()) {
+ i.next();
+ request.setRawHeader(i.key().toUtf8(), i.value().toString().toUtf8());
+ }
+ }
}
if (operation.isEmpty())
@@ -391,8 +400,8 @@ void WebPage::openUrl(const QString &address, const QVariant &op, const QVariant
url.setScheme("file");
}
#endif
-
- m_mainFrame->load(QNetworkRequest(url), networkOp, body);
+ request.setUrl(url);
+ m_mainFrame->load(request, networkOp, body);
}
}
View
34 test/webserver-spec.js
@@ -31,14 +31,18 @@ function checkRequest(request, response) {
if (expectedPostData !== false) {
expect(request.method).toEqual("POST");
expect(request.hasOwnProperty('post')).toBeTruthy();
- expect(typeof request.post).toEqual('object');
console.log("request.post => " + JSON.stringify(request.post, null, 4));
console.log("expectedPostData => " + JSON.stringify(expectedPostData, null, 4));
console.log("request.headers => " + JSON.stringify(request.headers, null, 4));
- expect(request.post).toEqual(expectedPostData);
if (request.headers["Content-Type"] && request.headers["Content-Type"] === "application/x-www-form-urlencoded") {
+ expect(typeof request.post).toEqual('object');
+ expect(request.post).toEqual(expectedPostData);
expect(request.hasOwnProperty('postRaw')).toBeTruthy();
expect(typeof request.postRaw).toEqual('string');
+ } else {
+ expect(typeof request.post).toEqual('string');
+ expect(request.post).toNotEqual(expectedPostData);
+ expect(request.hasOwnProperty('postRaw')).toBeFalsy();
}
expectedPostData = false;
}
@@ -109,7 +113,7 @@ describe("WebServer object", function() {
});
});
- it("should handle post requests", function() {
+ it("should handle post requests ('Content-Type' = 'application/x-www-form-urlencoded')", function() {
var page = require('webpage').create();
var url = "http://localhost:12345/foo/bar.txt?asdf=true";
//note: sorted by key (map)
@@ -117,7 +121,29 @@ describe("WebServer object", function() {
var handled = false;
runs(function() {
expect(handled).toEqual(false);
- page.open(url, 'post', "universe=expanding&answer=42", function (status) {
+ page.open(url, 'post', "universe=expanding&answer=42", { "Content-Type" : "application/x-www-form-urlencoded" }, function (status) {
+ expect(status == 'success').toEqual(true);
+ expect(page.plainText).toEqual("request handled");
+ handled = true;
+ });
+ });
+
+ waits(50);
+
+ runs(function() {
+ expect(handled).toEqual(true);
+ });
+ });
+
+ it("should handle post requests ('Content-Type' = 'ANY')", function() {
+ var page = require('webpage').create();
+ var url = "http://localhost:12345/foo/bar.txt?asdf=true";
+ //note: sorted by key (map)
+ expectedPostData = {'answer' : "42", 'universe' : "expanding"};
+ var handled = false;
+ runs(function() {
+ expect(handled).toEqual(false);
+ page.open(url, 'post', "universe=expanding&answer=42", { "Content-Type" : "application/json;charset=UTF-8" }, function (status) {
expect(status == 'success').toEqual(true);
expect(page.plainText).toEqual("request handled");
handled = true;
Please sign in to comment.
Something went wrong with that request. Please try again.