2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-07-25 02:02:35 +02:00
|
|
|
const _ = require("lodash");
|
2021-02-28 00:48:40 +01:00
|
|
|
const rewiremock = require("rewiremock/node");
|
2020-07-25 02:02:35 +02:00
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
const {set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2018-08-01 21:17:03 +02:00
|
|
|
|
2021-02-28 00:48:40 +01:00
|
|
|
const reload = {__esModule: true};
|
|
|
|
|
|
|
|
rewiremock("../../static/js/reload").with(reload);
|
2020-12-01 23:21:38 +01:00
|
|
|
|
|
|
|
set_global("setTimeout", (f, delay) => {
|
|
|
|
assert.equal(delay, 0);
|
|
|
|
f();
|
|
|
|
});
|
|
|
|
|
2021-02-28 00:48:40 +01:00
|
|
|
rewiremock.enable();
|
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const channel = zrequire("channel");
|
2018-08-01 21:17:03 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const default_stub_xhr = "default-stub-xhr";
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2021-02-22 12:18:39 +01:00
|
|
|
const $ = set_global("$", {});
|
|
|
|
|
2017-06-28 00:48:48 +02:00
|
|
|
function test_with_mock_ajax(test_params) {
|
2020-07-15 00:34:28 +02:00
|
|
|
const {xhr = default_stub_xhr, run_code, check_ajax_options} = test_params;
|
2018-04-18 22:48:22 +02:00
|
|
|
|
|
|
|
let ajax_called;
|
|
|
|
let ajax_options;
|
2021-02-23 14:37:26 +01:00
|
|
|
$.ajax = (options) => {
|
2017-06-28 00:48:48 +02:00
|
|
|
$.ajax = undefined;
|
|
|
|
ajax_called = true;
|
|
|
|
ajax_options = options;
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
options.simulate_success = (data, text_status) => {
|
2017-06-28 00:48:48 +02:00
|
|
|
options.success(data, text_status, xhr);
|
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
options.simulate_error = () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
options.error(xhr);
|
|
|
|
};
|
|
|
|
|
|
|
|
return xhr;
|
|
|
|
};
|
|
|
|
|
2018-04-18 22:48:22 +02:00
|
|
|
run_code();
|
2017-06-28 00:48:48 +02:00
|
|
|
assert(ajax_called);
|
2018-04-18 22:48:22 +02:00
|
|
|
check_ajax_options(ajax_options);
|
2017-06-28 00:48:48 +02:00
|
|
|
}
|
|
|
|
|
2021-02-22 12:04:22 +01:00
|
|
|
run_test("post", () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
test_with_mock_ajax({
|
2020-07-20 22:18:43 +02:00
|
|
|
run_code() {
|
2017-06-28 00:48:48 +02:00
|
|
|
channel.post({});
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
check_ajax_options(options) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(options.type, "POST");
|
|
|
|
assert.equal(options.dataType, "json");
|
2017-06-28 00:48:48 +02:00
|
|
|
|
|
|
|
// Just make sure these don't explode.
|
|
|
|
options.simulate_success();
|
|
|
|
options.simulate_error();
|
|
|
|
},
|
|
|
|
});
|
2021-02-22 12:04:22 +01:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2021-02-22 12:04:22 +01:00
|
|
|
run_test("patch", () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
test_with_mock_ajax({
|
2020-07-20 22:18:43 +02:00
|
|
|
run_code() {
|
2017-06-28 00:48:48 +02:00
|
|
|
channel.patch({});
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
check_ajax_options(options) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(options.type, "POST");
|
|
|
|
assert.equal(options.data.method, "PATCH");
|
|
|
|
assert.equal(options.dataType, "json");
|
2017-06-28 00:48:48 +02:00
|
|
|
|
|
|
|
// Just make sure these don't explode.
|
|
|
|
options.simulate_success();
|
|
|
|
options.simulate_error();
|
|
|
|
},
|
|
|
|
});
|
2021-02-22 12:04:22 +01:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2021-02-22 12:04:22 +01:00
|
|
|
run_test("put", () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
test_with_mock_ajax({
|
2020-07-20 22:18:43 +02:00
|
|
|
run_code() {
|
2017-06-28 00:48:48 +02:00
|
|
|
channel.put({});
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
check_ajax_options(options) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(options.type, "PUT");
|
|
|
|
assert.equal(options.dataType, "json");
|
2017-06-28 00:48:48 +02:00
|
|
|
|
|
|
|
// Just make sure these don't explode.
|
|
|
|
options.simulate_success();
|
|
|
|
options.simulate_error();
|
|
|
|
},
|
|
|
|
});
|
2021-02-22 12:04:22 +01:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2021-02-22 12:04:22 +01:00
|
|
|
run_test("delete", () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
test_with_mock_ajax({
|
2020-07-20 22:18:43 +02:00
|
|
|
run_code() {
|
2017-06-28 00:48:48 +02:00
|
|
|
channel.del({});
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
check_ajax_options(options) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(options.type, "DELETE");
|
|
|
|
assert.equal(options.dataType, "json");
|
2017-06-28 00:48:48 +02:00
|
|
|
|
|
|
|
// Just make sure these don't explode.
|
|
|
|
options.simulate_success();
|
|
|
|
options.simulate_error();
|
|
|
|
},
|
|
|
|
});
|
2021-02-22 12:04:22 +01:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2021-02-22 12:04:22 +01:00
|
|
|
run_test("get", () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
test_with_mock_ajax({
|
2020-07-20 22:18:43 +02:00
|
|
|
run_code() {
|
2017-06-28 00:48:48 +02:00
|
|
|
channel.get({});
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
check_ajax_options(options) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(options.type, "GET");
|
|
|
|
assert.equal(options.dataType, "json");
|
2017-06-28 00:48:48 +02:00
|
|
|
|
|
|
|
// Just make sure these don't explode.
|
|
|
|
options.simulate_success();
|
|
|
|
options.simulate_error();
|
|
|
|
},
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("normal_post", () => {
|
2018-04-18 22:48:22 +02:00
|
|
|
const data = {
|
2020-07-15 01:29:15 +02:00
|
|
|
s: "some_string",
|
2017-06-28 00:48:48 +02:00
|
|
|
num: 7,
|
|
|
|
lst: [1, 2, 4, 8],
|
|
|
|
};
|
|
|
|
|
2018-04-18 22:48:22 +02:00
|
|
|
let orig_success_called;
|
|
|
|
let orig_error_called;
|
2020-07-15 01:29:15 +02:00
|
|
|
const stub_xhr = "stub-xhr-normal-post";
|
2017-06-28 00:48:48 +02:00
|
|
|
|
|
|
|
test_with_mock_ajax({
|
|
|
|
xhr: stub_xhr,
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
run_code() {
|
2017-06-28 00:48:48 +02:00
|
|
|
channel.post({
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/endpoint",
|
2020-07-20 22:18:43 +02:00
|
|
|
success(data, text_status, xhr) {
|
2017-06-28 00:48:48 +02:00
|
|
|
orig_success_called = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(data, "response data");
|
|
|
|
assert.equal(text_status, "success");
|
2017-06-28 00:48:48 +02:00
|
|
|
assert.equal(xhr, stub_xhr);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error() {
|
2017-06-28 00:48:48 +02:00
|
|
|
orig_error_called = true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
check_ajax_options(options) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(options.type, "POST");
|
|
|
|
assert.equal(options.dataType, "json");
|
2017-06-28 00:48:48 +02:00
|
|
|
assert.deepEqual(options.data, data);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(options.url, "/json/endpoint");
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
options.simulate_success("response data", "success");
|
2017-06-28 00:48:48 +02:00
|
|
|
assert(orig_success_called);
|
|
|
|
|
|
|
|
options.simulate_error();
|
|
|
|
assert(orig_error_called);
|
|
|
|
},
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("patch_with_form_data", () => {
|
2018-04-18 22:48:22 +02:00
|
|
|
let appended;
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2018-04-18 22:48:22 +02:00
|
|
|
const data = {
|
2020-07-20 22:18:43 +02:00
|
|
|
append(k, v) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(k, "method");
|
|
|
|
assert.equal(v, "PATCH");
|
2017-06-28 00:48:48 +02:00
|
|
|
appended = true;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
test_with_mock_ajax({
|
2020-07-20 22:18:43 +02:00
|
|
|
run_code() {
|
2017-06-28 00:48:48 +02:00
|
|
|
channel.patch({
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
2017-06-28 00:48:48 +02:00
|
|
|
processData: false,
|
|
|
|
});
|
|
|
|
assert(appended);
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
check_ajax_options(options) {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(options.type, "POST");
|
|
|
|
assert.equal(options.dataType, "json");
|
2017-06-28 00:48:48 +02:00
|
|
|
|
|
|
|
// Just make sure these don't explode.
|
|
|
|
options.simulate_success();
|
|
|
|
options.simulate_error();
|
|
|
|
},
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("reload_on_403_error", () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
test_with_mock_ajax({
|
|
|
|
xhr: {
|
|
|
|
status: 403,
|
2017-07-25 03:30:13 +02:00
|
|
|
responseText: '{"msg": "CSRF Fehler: etwas", "code": "CSRF_FAILED"}',
|
2017-06-28 00:48:48 +02:00
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
run_code() {
|
2017-06-28 00:48:48 +02:00
|
|
|
channel.post({});
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
check_ajax_options(options) {
|
2018-04-18 22:48:22 +02:00
|
|
|
let reload_initiated;
|
2021-02-23 14:37:26 +01:00
|
|
|
reload.initiate = (options) => {
|
2017-06-28 00:48:48 +02:00
|
|
|
reload_initiated = true;
|
|
|
|
assert.deepEqual(options, {
|
|
|
|
immediate: true,
|
|
|
|
save_pointer: true,
|
|
|
|
save_narrow: true,
|
|
|
|
save_compose: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
options.simulate_error();
|
|
|
|
assert(reload_initiated);
|
|
|
|
},
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("unexpected_403_response", () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
test_with_mock_ajax({
|
|
|
|
xhr: {
|
|
|
|
status: 403,
|
2020-07-15 01:29:15 +02:00
|
|
|
responseText: "unexpected",
|
2017-06-28 00:48:48 +02:00
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
run_code() {
|
2017-06-28 00:48:48 +02:00
|
|
|
channel.post({});
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
check_ajax_options(options) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Unexpected 403 response from server");
|
2017-06-28 00:48:48 +02:00
|
|
|
options.simulate_error();
|
|
|
|
},
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("retry", () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
test_with_mock_ajax({
|
2020-07-20 22:18:43 +02:00
|
|
|
run_code() {
|
2017-06-28 00:48:48 +02:00
|
|
|
channel.post({
|
|
|
|
idempotent: true,
|
|
|
|
data: 42,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
check_ajax_options(options) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("log", "Retrying idempotent[object Object]");
|
2017-06-28 00:48:48 +02:00
|
|
|
test_with_mock_ajax({
|
2020-07-20 22:18:43 +02:00
|
|
|
run_code() {
|
2017-06-28 00:48:48 +02:00
|
|
|
options.simulate_success();
|
|
|
|
},
|
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
check_ajax_options(options) {
|
2017-06-28 00:48:48 +02:00
|
|
|
assert.equal(options.data, 42);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("too_many_pending", () => {
|
2021-02-23 14:37:26 +01:00
|
|
|
$.ajax = () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const xhr = "stub";
|
2017-06-28 00:48:48 +02:00
|
|
|
return xhr;
|
|
|
|
};
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.expect(
|
|
|
|
"warn",
|
|
|
|
"The length of pending_requests is over 50. " +
|
|
|
|
"Most likely they are not being correctly removed.",
|
|
|
|
);
|
2020-07-02 01:45:54 +02:00
|
|
|
_.times(50, () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
channel.post({});
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("xhr_error_message", () => {
|
2018-04-18 22:48:22 +02:00
|
|
|
let xhr = {
|
2020-07-15 01:29:15 +02:00
|
|
|
status: "200",
|
|
|
|
responseText: "does not matter",
|
2017-06-28 00:48:48 +02:00
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
let msg = "data added";
|
|
|
|
assert.equal(channel.xhr_error_message(msg, xhr), "data added");
|
2017-06-28 00:48:48 +02:00
|
|
|
|
|
|
|
xhr = {
|
2020-07-15 01:29:15 +02:00
|
|
|
status: "404",
|
2017-06-28 00:48:48 +02:00
|
|
|
responseText: '{"msg": "file not found"}',
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
msg = "some message";
|
|
|
|
assert.equal(channel.xhr_error_message(msg, xhr), "some message: file not found");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2021-02-28 00:48:40 +01:00
|
|
|
rewiremock.disable();
|