| 1 | QUnit.module( 'mediawiki.rest', ( hooks ) => { |
| 2 | let server; |
| 3 | hooks.beforeEach( function () { |
| 4 | server = this.sandbox.useFakeServer(); |
| 5 | server.respondImmediately = true; |
| 6 | } ); |
| 7 | |
| 8 | QUnit.test( 'get()', async ( assert ) => { |
| 9 | const api = new mw.Rest(); |
| 10 | |
| 11 | let headers; |
| 12 | server.respondWith( 'GET', |
| 13 | /rest.php\/test\/rest\/path\?queryParam=%2Fslash-will-be-encoded%3F$/, |
| 14 | ( request ) => { |
| 15 | headers = request.requestHeaders; |
| 16 | request.respond( 200, { 'Content-Type': 'application/json' }, '{}' ); |
| 17 | } |
| 18 | ); |
| 19 | |
| 20 | const data = await api.get( |
| 21 | '/test/rest/path', |
| 22 | { queryParam: '/slash-will-be-encoded?' }, |
| 23 | { MyHeader: 'MyHeaderValue' } |
| 24 | ); |
| 25 | |
| 26 | assert.propContains( headers, { |
| 27 | MyHeader: 'MyHeaderValue' |
| 28 | }, 'request headers' ); |
| 29 | assert.deepEqual( data, {}, 'succeeds without errors' ); |
| 30 | } ); |
| 31 | |
| 32 | QUnit.test( 'get() respects ajaxOptions url', async ( assert ) => { |
| 33 | const api = new mw.Rest( { |
| 34 | ajax: { |
| 35 | url: '/test.php' |
| 36 | } |
| 37 | } ); |
| 38 | |
| 39 | server.respondWith( 'GET', |
| 40 | /test.php\/test\/rest\/path$/, |
| 41 | [ 200, { 'Content-Type': 'application/json' }, '{}' ] |
| 42 | ); |
| 43 | |
| 44 | const data = await api.get( '/test/rest/path' ); |
| 45 | assert.deepEqual( data, {}, 'succeeds without errors' ); |
| 46 | } ); |
| 47 | |
| 48 | QUnit.test( 'post()', async ( assert ) => { |
| 49 | const api = new mw.Rest(); |
| 50 | |
| 51 | let headers, body; |
| 52 | server.respondWith( 'POST', |
| 53 | /rest.php\/test\/bla\/bla\/bla$/, |
| 54 | ( request ) => { |
| 55 | headers = request.requestHeaders; |
| 56 | body = request.requestBody; |
| 57 | request.respond( 201, { 'Content-Type': 'application/json' }, '{}' ); |
| 58 | } |
| 59 | ); |
| 60 | |
| 61 | const data = await api.post( '/test/bla/bla/bla', { |
| 62 | param: 'value' |
| 63 | }, { |
| 64 | authorization: 'my_token' |
| 65 | } ); |
| 66 | |
| 67 | assert.propContains( headers, { |
| 68 | 'Content-Type': 'application/json;charset=utf-8', |
| 69 | authorization: 'my_token' |
| 70 | }, 'request headers' ); |
| 71 | assert.deepEqual( JSON.parse( body ), { param: 'value' }, 'body' ); |
| 72 | assert.deepEqual( data, {}, 'succeeds without errors' ); |
| 73 | } ); |
| 74 | |
| 75 | QUnit.test( 'put()', async ( assert ) => { |
| 76 | const api = new mw.Rest(); |
| 77 | |
| 78 | let headers, body; |
| 79 | server.respondWith( 'PUT', |
| 80 | /rest.php\/test\/bla\/bla\/bla$/, |
| 81 | ( request ) => { |
| 82 | headers = request.requestHeaders; |
| 83 | body = request.requestBody; |
| 84 | request.respond( 201, { 'Content-Type': 'application/json' }, '{}' ); |
| 85 | } |
| 86 | ); |
| 87 | |
| 88 | const data = await api.put( '/test/bla/bla/bla', { |
| 89 | param: 'value' |
| 90 | }, { |
| 91 | authorization: 'my_token' |
| 92 | } ); |
| 93 | |
| 94 | assert.propContains( headers, { |
| 95 | 'Content-Type': 'application/json;charset=utf-8', |
| 96 | authorization: 'my_token' |
| 97 | }, 'request headers' ); |
| 98 | assert.deepEqual( JSON.parse( body ), { param: 'value' }, 'body' ); |
| 99 | assert.deepEqual( data, {}, 'succeeds without errors' ); |
| 100 | } ); |
| 101 | |
| 102 | QUnit.test( 'delete()', async ( assert ) => { |
| 103 | const api = new mw.Rest(); |
| 104 | |
| 105 | let headers, body; |
| 106 | server.respond( 'DELETE', |
| 107 | /rest.php\/test\/bla\/bla\/bla$/, |
| 108 | ( request ) => { |
| 109 | headers = request.requestHeaders; |
| 110 | body = request.requestBody; |
| 111 | request.respond( 201, { 'Content-Type': 'application/json' }, '{}' ); |
| 112 | } |
| 113 | ); |
| 114 | |
| 115 | const data = await api.delete( '/test/bla/bla/bla', { |
| 116 | param: 'value' |
| 117 | }, { |
| 118 | authorization: 'my_token' |
| 119 | } ); |
| 120 | |
| 121 | assert.propContains( headers, { |
| 122 | 'Content-Type': 'application/json;charset=utf-8', |
| 123 | authorization: 'my_token' |
| 124 | }, 'request headers' ); |
| 125 | assert.deepEqual( JSON.parse( body ), { param: 'value' }, 'body' ); |
| 126 | assert.deepEqual( data, {}, 'succeeds without errors' ); |
| 127 | } ); |
| 128 | |
| 129 | QUnit.test( 'http error', ( assert ) => { |
| 130 | const api = new mw.Rest(); |
| 131 | |
| 132 | server.respond( [ 404, {}, 'FAIL' ] ); |
| 133 | |
| 134 | const promise = api.get( '/test/rest/path' ); |
| 135 | assert.rejects( promise, /http/, 'API error should reject the deferred' ); |
| 136 | } ); |
| 137 | |
| 138 | QUnit.test( '#abort', function ( assert ) { |
| 139 | const requests = []; |
| 140 | const api = new mw.Rest(); |
| 141 | this.sandbox.stub( $, 'ajax', () => { |
| 142 | const request = $.Deferred(); |
| 143 | request.abort = this.sandbox.spy(); |
| 144 | requests.push( request ); |
| 145 | return request; |
| 146 | } ); |
| 147 | api.get( '/test1' ); |
| 148 | api.post( '/test2', { a: 1 } ); |
| 149 | api.abort(); |
| 150 | assert.strictEqual( requests.length, 2, 'Check both requests triggered' ); |
| 151 | requests.forEach( ( request, i ) => { |
| 152 | assert.true( request.abort.calledOnce, 'abort request number ' + i ); |
| 153 | } ); |
| 154 | } ); |
| 155 | } ); |