2017-11-08 20:20:50 +01:00
|
|
|
zrequire('channel');
|
2017-06-28 00:48:48 +02:00
|
|
|
|
|
|
|
set_global('$', {});
|
|
|
|
set_global('reload', {});
|
2018-04-24 00:49:54 +02:00
|
|
|
set_global('blueslip', global.make_zblueslip());
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2018-04-18 22:48:22 +02:00
|
|
|
const default_stub_xhr = 'default-stub-xhr';
|
2017-06-28 00:48:48 +02:00
|
|
|
|
|
|
|
function test_with_mock_ajax(test_params) {
|
2018-04-18 22:48:22 +02:00
|
|
|
const {
|
|
|
|
xhr = default_stub_xhr,
|
|
|
|
run_code,
|
|
|
|
check_ajax_options,
|
|
|
|
} = test_params;
|
|
|
|
|
|
|
|
let ajax_called;
|
|
|
|
let ajax_options;
|
2017-06-28 00:48:48 +02:00
|
|
|
$.ajax = function (options) {
|
|
|
|
$.ajax = undefined;
|
|
|
|
ajax_called = true;
|
|
|
|
ajax_options = options;
|
|
|
|
|
|
|
|
options.simulate_success = function (data, text_status) {
|
|
|
|
options.success(data, text_status, xhr);
|
|
|
|
};
|
|
|
|
|
|
|
|
options.simulate_error = function () {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('basics', () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
test_with_mock_ajax({
|
|
|
|
run_code: function () {
|
|
|
|
channel.post({});
|
|
|
|
},
|
|
|
|
|
|
|
|
check_ajax_options: function (options) {
|
|
|
|
assert.equal(options.type, 'POST');
|
|
|
|
assert.equal(options.dataType, 'json');
|
|
|
|
|
|
|
|
// Just make sure these don't explode.
|
|
|
|
options.simulate_success();
|
|
|
|
options.simulate_error();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
test_with_mock_ajax({
|
|
|
|
run_code: function () {
|
|
|
|
channel.patch({});
|
|
|
|
},
|
|
|
|
|
|
|
|
check_ajax_options: function (options) {
|
|
|
|
assert.equal(options.type, 'POST');
|
|
|
|
assert.equal(options.data.method, 'PATCH');
|
|
|
|
assert.equal(options.dataType, 'json');
|
|
|
|
|
|
|
|
// Just make sure these don't explode.
|
|
|
|
options.simulate_success();
|
|
|
|
options.simulate_error();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
test_with_mock_ajax({
|
|
|
|
run_code: function () {
|
|
|
|
channel.put({});
|
|
|
|
},
|
|
|
|
|
|
|
|
check_ajax_options: function (options) {
|
|
|
|
assert.equal(options.type, 'PUT');
|
|
|
|
assert.equal(options.dataType, 'json');
|
|
|
|
|
|
|
|
// Just make sure these don't explode.
|
|
|
|
options.simulate_success();
|
|
|
|
options.simulate_error();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
test_with_mock_ajax({
|
|
|
|
run_code: function () {
|
|
|
|
channel.del({});
|
|
|
|
},
|
|
|
|
|
|
|
|
check_ajax_options: function (options) {
|
|
|
|
assert.equal(options.type, 'DELETE');
|
|
|
|
assert.equal(options.dataType, 'json');
|
|
|
|
|
|
|
|
// Just make sure these don't explode.
|
|
|
|
options.simulate_success();
|
|
|
|
options.simulate_error();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
test_with_mock_ajax({
|
|
|
|
run_code: function () {
|
|
|
|
channel.get({});
|
|
|
|
},
|
|
|
|
|
|
|
|
check_ajax_options: function (options) {
|
|
|
|
assert.equal(options.type, 'GET');
|
|
|
|
assert.equal(options.dataType, 'json');
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('normal_post', () => {
|
2018-04-18 22:48:22 +02:00
|
|
|
const data = {
|
2017-06-28 00:48:48 +02:00
|
|
|
s: 'some_string',
|
|
|
|
num: 7,
|
|
|
|
lst: [1, 2, 4, 8],
|
|
|
|
};
|
|
|
|
|
2018-04-18 22:48:22 +02:00
|
|
|
let orig_success_called;
|
|
|
|
let orig_error_called;
|
|
|
|
const stub_xhr = 'stub-xhr-normal-post';
|
2017-06-28 00:48:48 +02:00
|
|
|
|
|
|
|
test_with_mock_ajax({
|
|
|
|
xhr: stub_xhr,
|
|
|
|
|
|
|
|
run_code: function () {
|
|
|
|
channel.post({
|
|
|
|
data: data,
|
|
|
|
url: '/json/endpoint',
|
|
|
|
success: function (data, text_status, xhr) {
|
|
|
|
orig_success_called = true;
|
|
|
|
assert.equal(data, 'response data');
|
|
|
|
assert.equal(text_status, 'success');
|
|
|
|
assert.equal(xhr, stub_xhr);
|
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
orig_error_called = true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
check_ajax_options: function (options) {
|
|
|
|
assert.equal(options.type, 'POST');
|
|
|
|
assert.equal(options.dataType, 'json');
|
|
|
|
assert.deepEqual(options.data, data);
|
|
|
|
assert.equal(options.url, '/json/endpoint');
|
|
|
|
|
|
|
|
options.simulate_success('response data', 'success');
|
|
|
|
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
|
|
|
|
2018-05-15 12:40:07 +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 = {
|
2017-06-28 00:48:48 +02:00
|
|
|
append: function (k, v) {
|
|
|
|
assert.equal(k, 'method');
|
|
|
|
assert.equal(v, 'PATCH');
|
|
|
|
appended = true;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
test_with_mock_ajax({
|
|
|
|
run_code: function () {
|
|
|
|
channel.patch({
|
|
|
|
data: data,
|
|
|
|
processData: false,
|
|
|
|
});
|
|
|
|
assert(appended);
|
|
|
|
},
|
|
|
|
|
|
|
|
check_ajax_options: function (options) {
|
|
|
|
assert.equal(options.type, 'POST');
|
|
|
|
assert.equal(options.dataType, 'json');
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2018-05-15 12:40:07 +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
|
|
|
},
|
|
|
|
|
|
|
|
run_code: function () {
|
|
|
|
channel.post({});
|
|
|
|
},
|
|
|
|
|
|
|
|
check_ajax_options: function (options) {
|
2018-04-18 22:48:22 +02:00
|
|
|
let reload_initiated;
|
2017-06-28 00:48:48 +02:00
|
|
|
reload.initiate = function (options) {
|
|
|
|
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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('unexpected_403_response', () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
test_with_mock_ajax({
|
|
|
|
xhr: {
|
|
|
|
status: 403,
|
|
|
|
responseText: 'unexpected',
|
|
|
|
},
|
|
|
|
|
|
|
|
run_code: function () {
|
|
|
|
channel.post({});
|
|
|
|
},
|
|
|
|
|
|
|
|
check_ajax_options: function (options) {
|
2018-04-24 00:49:54 +02:00
|
|
|
blueslip.set_test_data('error', 'Unexpected 403 response from server');
|
2017-06-28 00:48:48 +02:00
|
|
|
options.simulate_error();
|
2018-04-24 00:49:54 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 1);
|
|
|
|
blueslip.clear_test_data();
|
2017-06-28 00:48:48 +02:00
|
|
|
},
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('retry', () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
test_with_mock_ajax({
|
|
|
|
run_code: function () {
|
|
|
|
channel.post({
|
|
|
|
idempotent: true,
|
|
|
|
data: 42,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
check_ajax_options: function (options) {
|
|
|
|
global.patch_builtin('setTimeout', function (f, delay) {
|
|
|
|
assert.equal(delay, 0);
|
|
|
|
f();
|
|
|
|
});
|
|
|
|
|
|
|
|
test_with_mock_ajax({
|
|
|
|
run_code: function () {
|
|
|
|
options.simulate_success();
|
|
|
|
},
|
|
|
|
|
|
|
|
check_ajax_options: function (options) {
|
|
|
|
assert.equal(options.data, 42);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-04-24 00:49:54 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('log').length, 1);
|
|
|
|
assert.equal(blueslip.get_test_logs('log')[0], 'Retrying idempotent[object Object]');
|
|
|
|
blueslip.clear_test_data();
|
2017-06-28 00:48:48 +02:00
|
|
|
},
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('too_many_pending', () => {
|
2017-06-28 00:48:48 +02:00
|
|
|
$.ajax = function () {
|
2018-04-18 22:48:22 +02:00
|
|
|
const xhr = 'stub';
|
2017-06-28 00:48:48 +02:00
|
|
|
return xhr;
|
|
|
|
};
|
|
|
|
|
2018-04-24 00:49:54 +02:00
|
|
|
blueslip.set_test_data('warn',
|
2018-05-07 03:30:13 +02:00
|
|
|
'The length of pending_requests is over 50. ' +
|
|
|
|
'Most likely they are not being correctly removed.');
|
2017-06-28 00:48:48 +02:00
|
|
|
_.times(50, function () {
|
|
|
|
channel.post({});
|
|
|
|
});
|
2018-04-24 00:49:54 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('warn').length, 1);
|
|
|
|
blueslip.clear_test_data();
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 00:48:48 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('xhr_error_message', () => {
|
2018-04-18 22:48:22 +02:00
|
|
|
let xhr = {
|
2017-06-28 00:48:48 +02:00
|
|
|
status: '200',
|
|
|
|
responseText: 'does not matter',
|
|
|
|
};
|
2018-04-18 22:48:22 +02:00
|
|
|
let msg = 'data added';
|
2017-06-28 00:48:48 +02:00
|
|
|
assert.equal(channel.xhr_error_message(msg, xhr), 'data added');
|
|
|
|
|
|
|
|
xhr = {
|
|
|
|
status: '404',
|
|
|
|
responseText: '{"msg": "file not found"}',
|
|
|
|
};
|
|
|
|
msg = 'some message';
|
|
|
|
assert.equal(channel.xhr_error_message(msg, xhr), 'some message: file not found');
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|