Bug 1477076 - Add POST support to addEngineWithDetails. r=florian

Differential Revision: https://phabricator.services.mozilla.com/D2254

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Michael Kaply 2018-07-20 18:40:00 +00:00
Родитель 04f1e1bc1e
Коммит 96e550bb6b
3 изменённых файлов: 36 добавлений и 2 удалений

Просмотреть файл

@ -367,12 +367,17 @@ interface nsIBrowserSearchService : nsISupports
* suggestURL: http://example.com/?suggest={searchTerms},
* });
*
* Using this method, you can use a new parameter, suggestURL:
* Using this method, you can use new parameters:
*
* @param suggestURL [optional]
* Optional: The URL to which search suggestion requests
* should be sent.
*
* @param postData [optional]
* Optional: For POST requests, a string of URL parameters
* to send, separated by '&'. The string will be subjected
* to OpenSearch parameter substitution.
*
*/
void addEngineWithDetails(in AString name,
in jsval iconURL,

Просмотреть файл

@ -41,7 +41,7 @@ const BinaryInputStream = Components.Constructor(
"@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream", "setInputStream");
XPCOMUtils.defineLazyGlobalGetters(this, ["DOMParser", "XMLHttpRequest"]);
XPCOMUtils.defineLazyGlobalGetters(this, ["DOMParser", "XMLHttpRequest", "URLSearchParams"]);
// A text encoder to UTF8, used whenever we commit the cache to disk.
XPCOMUtils.defineLazyGetter(this, "gEncoder",
@ -1742,6 +1742,12 @@ Engine.prototype = {
if (aParams.queryCharset) {
this._queryCharset = aParams.queryCharset;
}
if (aParams.postData) {
let queries = new URLSearchParams(aParams.postData);
for (let [name, value] of queries) {
this.addParam(name, value);
}
}
this._name = aName;
this.alias = aParams.alias;

Просмотреть файл

@ -12,6 +12,9 @@ const kAlias = "alias_foo";
const kSearchTerm = "foo";
const kExtensionID = "test@example.com";
const URLTYPE_SUGGEST_JSON = "application/x-suggestions+json";
const kSearchEnginePOSTID = "addEngineWithDetails_post_test_engine";
const kSearchEnginePOSTURL = "http://example.com/";
const kSearchEnginePOSTData = "search={searchTerms}&extra=more";
add_task(async function test_addEngineWithDetails() {
Assert.ok(!Services.search.isInitialized);
@ -51,3 +54,23 @@ add_task(async function test_addEngineWithDetails() {
Services.search.currentEngine.getSubmission(kSearchTerm, URLTYPE_SUGGEST_JSON);
Assert.equal(submissionSuggest.uri.spec, expectedSuggestURL);
});
add_task(async function test_addEngineWithDetailsPOST() {
Assert.ok(Services.search.isInitialized);
Services.search.addEngineWithDetails(kSearchEnginePOSTID, {
template: kSearchEnginePOSTURL,
method: "POST",
postData: kSearchEnginePOSTData,
});
let engine = Services.search.getEngineByName(kSearchEnginePOSTID);
let expectedPOSTData = kSearchEnginePOSTData.replace("{searchTerms}", kSearchTerm);
let submission = engine.getSubmission(kSearchTerm, null, "searchbar");
Assert.equal(submission.uri.spec, kSearchEnginePOSTURL);
let sis = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
sis.init(submission.postData);
let data = sis.read(submission.postData.available());
Assert.equal(data, expectedPOSTData);
});