0797f32dfbb2c1fc3b32a5cabe3f9d6b9a0bfb84
2 QUnit
.module( 'mediawiki.api.options', QUnit
.newMwEnvironment( {
4 this.server
= this.sandbox
.useFakeServer();
8 QUnit
.test( 'saveOption', function ( assert
) {
13 stub
= this.sandbox
.stub( mw
.Api
.prototype, 'saveOptions' );
15 api
.saveOption( 'foo', 'bar' );
17 assert
.ok( stub
.calledOnce
, '#saveOptions called once' );
18 assert
.deepEqual( stub
.getCall( 0 ).args
, [ { foo
: 'bar' } ], '#saveOptions called correctly' );
21 QUnit
.test( 'saveOptions without Unit Separator', function ( assert
) {
24 var api
= new mw
.Api( { useUS
: false } );
26 // We need to respond to the request for token first, otherwise the other requests won't be sent
27 // until after the server.respond call, which confuses sinon terribly. This sucks a lot.
28 api
.getToken( 'options' );
30 /meta=tokens&type=csrf/,
31 [ 200, { 'Content-Type': 'application/json' },
32 '{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ]
35 api
.saveOptions( {} ).done( function () {
36 assert
.ok( true, 'Request completed: empty case' );
38 api
.saveOptions( { foo
: 'bar' } ).done( function () {
39 assert
.ok( true, 'Request completed: simple' );
41 api
.saveOptions( { foo
: 'bar', baz
: 'quux' } ).done( function () {
42 assert
.ok( true, 'Request completed: two options' );
44 api
.saveOptions( { foo
: 'bar|quux', bar
: 'a|b|c', baz
: 'quux' } ).done( function () {
45 assert
.ok( true, 'Request completed: not bundleable' );
47 api
.saveOptions( { foo
: null } ).done( function () {
48 assert
.ok( true, 'Request completed: reset an option' );
50 api
.saveOptions( { 'foo|bar=quux': null } ).done( function () {
51 assert
.ok( true, 'Request completed: reset an option, not bundleable' );
54 // Requests are POST, match requestBody instead of url
55 this.server
.respond( function ( request
) {
56 switch ( request
.requestBody
) {
58 case 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C':
60 case 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C':
62 case 'action=options&format=json&formatversion=2&optionname=foo&optionvalue=bar%7Cquux&token=%2B%5C':
63 case 'action=options&format=json&formatversion=2&optionname=bar&optionvalue=a%7Cb%7Cc&token=%2B%5C':
64 case 'action=options&format=json&formatversion=2&change=baz%3Dquux&token=%2B%5C':
66 case 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C':
67 // reset an option, not bundleable
68 case 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C':
69 assert
.ok( true, 'Repond to ' + request
.requestBody
);
70 request
.respond( 200, { 'Content-Type': 'application/json' },
71 '{ "options": "success" }' );
74 assert
.ok( false, 'Unexpected request: ' + request
.requestBody
);
79 QUnit
.test( 'saveOptions with Unit Separator', function ( assert
) {
82 var api
= new mw
.Api( { useUS
: true } );
84 // We need to respond to the request for token first, otherwise the other requests won't be sent
85 // until after the server.respond call, which confuses sinon terribly. This sucks a lot.
86 api
.getToken( 'options' );
88 /meta=tokens&type=csrf/,
89 [ 200, { 'Content-Type': 'application/json' },
90 '{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ]
93 api
.saveOptions( {} ).done( function () {
94 assert
.ok( true, 'Request completed: empty case' );
96 api
.saveOptions( { foo
: 'bar' } ).done( function () {
97 assert
.ok( true, 'Request completed: simple' );
99 api
.saveOptions( { foo
: 'bar', baz
: 'quux' } ).done( function () {
100 assert
.ok( true, 'Request completed: two options' );
102 api
.saveOptions( { foo
: 'bar|quux', bar
: 'a|b|c', baz
: 'quux' } ).done( function () {
103 assert
.ok( true, 'Request completed: bundleable with unit separator' );
105 api
.saveOptions( { foo
: 'bar|quux', bar
: 'a|b|c', 'baz=baz': 'quux' } ).done( function () {
106 assert
.ok( true, 'Request completed: not bundleable with unit separator' );
108 api
.saveOptions( { foo
: null } ).done( function () {
109 assert
.ok( true, 'Request completed: reset an option' );
111 api
.saveOptions( { 'foo|bar=quux': null } ).done( function () {
112 assert
.ok( true, 'Request completed: reset an option, not bundleable' );
115 // Requests are POST, match requestBody instead of url
116 this.server
.respond( function ( request
) {
117 switch ( request
.requestBody
) {
119 case 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C':
121 case 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C':
122 // bundleable with unit separator
123 case 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc%1Fbaz%3Dquux&token=%2B%5C':
124 // not bundleable with unit separator
125 case 'action=options&format=json&formatversion=2&optionname=baz%3Dbaz&optionvalue=quux&token=%2B%5C':
126 case 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc&token=%2B%5C':
128 case 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C':
129 // reset an option, not bundleable
130 case 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C':
131 assert
.ok( true, 'Repond to ' + request
.requestBody
);
132 request
.respond( 200, { 'Content-Type': 'application/json' },
133 '{ "options": "success" }' );
136 assert
.ok( false, 'Unexpected request: ' + request
.requestBody
);