Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit ac771a22b97f930fcab2cc5ab2b0bdb4f95ae757 1 parent 03e47a1
Ivan De Marino detro authored
8 src/modules/webpage.js
@@ -158,6 +158,14 @@ exports.create = function (opts) {
158 158 data: arg2
159 159 }, this.settings);
160 160 return;
  161 + } else if (arguments.length === 5) {
  162 + this.onLoadFinished = arg4;
  163 + this.openUrl(url, {
  164 + operation: arg1,
  165 + data: arg2,
  166 + headers : arg3
  167 + }, this.settings);
  168 + return;
161 169 }
162 170 throw "Wrong use of WebPage#open";
163 171 };
13 src/webpage.cpp
@@ -45,6 +45,7 @@
45 45 #include <QWebFrame>
46 46 #include <QWebPage>
47 47 #include <QWebInspector>
  48 +#include <QMapIterator>
48 49
49 50 #include "networkaccessmanager.h"
50 51 #include "utils.h"
@@ -346,6 +347,7 @@ void WebPage::openUrl(const QString &address, const QVariant &op, const QVariant
346 347 {
347 348 QString operation;
348 349 QByteArray body;
  350 + QNetworkRequest request;
349 351
350 352 applySettings(settings);
351 353 m_webPage->triggerAction(QWebPage::Stop);
@@ -356,6 +358,13 @@ void WebPage::openUrl(const QString &address, const QVariant &op, const QVariant
356 358 if (op.type() == QVariant::Map) {
357 359 operation = op.toMap().value("operation").toString();
358 360 body = op.toMap().value("data").toByteArray();
  361 + if (op.toMap().contains("headers")) {
  362 + QMapIterator<QString, QVariant> i(op.toMap().value("headers").toMap());
  363 + while (i.hasNext()) {
  364 + i.next();
  365 + request.setRawHeader(i.key().toUtf8(), i.value().toString().toUtf8());
  366 + }
  367 + }
359 368 }
360 369
361 370 if (operation.isEmpty())
@@ -391,8 +400,8 @@ void WebPage::openUrl(const QString &address, const QVariant &op, const QVariant
391 400 url.setScheme("file");
392 401 }
393 402 #endif
394   -
395   - m_mainFrame->load(QNetworkRequest(url), networkOp, body);
  403 + request.setUrl(url);
  404 + m_mainFrame->load(request, networkOp, body);
396 405 }
397 406 }
398 407
34 test/webserver-spec.js
@@ -31,14 +31,18 @@ function checkRequest(request, response) {
31 31 if (expectedPostData !== false) {
32 32 expect(request.method).toEqual("POST");
33 33 expect(request.hasOwnProperty('post')).toBeTruthy();
34   - expect(typeof request.post).toEqual('object');
35 34 console.log("request.post => " + JSON.stringify(request.post, null, 4));
36 35 console.log("expectedPostData => " + JSON.stringify(expectedPostData, null, 4));
37 36 console.log("request.headers => " + JSON.stringify(request.headers, null, 4));
38   - expect(request.post).toEqual(expectedPostData);
39 37 if (request.headers["Content-Type"] && request.headers["Content-Type"] === "application/x-www-form-urlencoded") {
  38 + expect(typeof request.post).toEqual('object');
  39 + expect(request.post).toEqual(expectedPostData);
40 40 expect(request.hasOwnProperty('postRaw')).toBeTruthy();
41 41 expect(typeof request.postRaw).toEqual('string');
  42 + } else {
  43 + expect(typeof request.post).toEqual('string');
  44 + expect(request.post).toNotEqual(expectedPostData);
  45 + expect(request.hasOwnProperty('postRaw')).toBeFalsy();
42 46 }
43 47 expectedPostData = false;
44 48 }
@@ -109,7 +113,7 @@ describe("WebServer object", function() {
109 113 });
110 114 });
111 115
112   - it("should handle post requests", function() {
  116 + it("should handle post requests ('Content-Type' = 'application/x-www-form-urlencoded')", function() {
113 117 var page = require('webpage').create();
114 118 var url = "http://localhost:12345/foo/bar.txt?asdf=true";
115 119 //note: sorted by key (map)
@@ -117,7 +121,29 @@ describe("WebServer object", function() {
117 121 var handled = false;
118 122 runs(function() {
119 123 expect(handled).toEqual(false);
120   - page.open(url, 'post', "universe=expanding&answer=42", function (status) {
  124 + page.open(url, 'post', "universe=expanding&answer=42", { "Content-Type" : "application/x-www-form-urlencoded" }, function (status) {
  125 + expect(status == 'success').toEqual(true);
  126 + expect(page.plainText).toEqual("request handled");
  127 + handled = true;
  128 + });
  129 + });
  130 +
  131 + waits(50);
  132 +
  133 + runs(function() {
  134 + expect(handled).toEqual(true);
  135 + });
  136 + });
  137 +
  138 + it("should handle post requests ('Content-Type' = 'ANY')", function() {
  139 + var page = require('webpage').create();
  140 + var url = "http://localhost:12345/foo/bar.txt?asdf=true";
  141 + //note: sorted by key (map)
  142 + expectedPostData = {'answer' : "42", 'universe' : "expanding"};
  143 + var handled = false;
  144 + runs(function() {
  145 + expect(handled).toEqual(false);
  146 + page.open(url, 'post', "universe=expanding&answer=42", { "Content-Type" : "application/json;charset=UTF-8" }, function (status) {
121 147 expect(status == 'success').toEqual(true);
122 148 expect(page.plainText).toEqual("request handled");
123 149 handled = true;

0 comments on commit ac771a2

Please sign in to comment.
Something went wrong with that request. Please try again.