api-request.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* global wp, JSON */
  2. ( function( QUnit ) {
  3. var originalRootUrl = window.wpApiSettings.root;
  4. var expectedHeaders = {
  5. 'X-WP-Nonce': 'not_a_real_nonce',
  6. 'Accept': 'application/json, */*;q=0.1'
  7. };
  8. QUnit.module( 'wp-api-request', {
  9. afterEach: function() {
  10. window.wpApiSettings.root = originalRootUrl;
  11. }
  12. } );
  13. QUnit.test( 'does not mutate original object', function( assert ) {
  14. var settingsOriginal = {
  15. url: 'aaaaa',
  16. path: 'wp/v2/posts',
  17. headers: {
  18. 'Header-Name': 'value'
  19. },
  20. data: {
  21. orderby: 'something'
  22. }
  23. };
  24. var settings = wp.apiRequest.buildAjaxOptions( settingsOriginal );
  25. assert.notStrictEqual( settings, settingsOriginal );
  26. assert.notStrictEqual( settings.headers, settingsOriginal.headers );
  27. assert.strictEqual( settings.data, settingsOriginal.data );
  28. assert.deepEqual( settings, {
  29. url: 'http://localhost/wp-json/wp/v2/posts',
  30. headers: {
  31. 'X-WP-Nonce': 'not_a_real_nonce',
  32. 'Accept': 'application/json, */*;q=0.1',
  33. 'Header-Name': 'value'
  34. },
  35. data: {
  36. orderby: 'something'
  37. }
  38. } );
  39. assert.deepEqual( settingsOriginal, {
  40. url: 'aaaaa',
  41. path: 'wp/v2/posts',
  42. headers: {
  43. 'Header-Name': 'value'
  44. },
  45. data: {
  46. orderby: 'something'
  47. }
  48. } );
  49. } );
  50. QUnit.test( 'does not add nonce header if already present', function( assert ) {
  51. [ 'X-WP-Nonce', 'x-wp-nonce', 'X-WP-NONCE' ].forEach( function( headerName ) {
  52. var nonceHeader = {};
  53. nonceHeader[ headerName ] = 'still_not_a_real_nonce';
  54. var settingsOriginal = {
  55. url: 'aaaa',
  56. headers: JSON.parse( JSON.stringify( nonceHeader ) )
  57. };
  58. var settings = wp.apiRequest.buildAjaxOptions( settingsOriginal );
  59. assert.notStrictEqual( settings, settingsOriginal );
  60. var expected = {
  61. Accept: 'application/json, */*;q=0.1'
  62. };
  63. expected[ headerName ] = nonceHeader[ headerName ];
  64. assert.deepEqual( settings, {
  65. url: 'aaaa',
  66. headers: expected
  67. } );
  68. } );
  69. } );
  70. QUnit.test( 'does not add nonce header if ?_wpnonce=... present', function( assert ) {
  71. var settingsOriginal = {
  72. url: 'aaaa',
  73. data: {
  74. _wpnonce: 'definitely_not_a_real_nonce'
  75. }
  76. };
  77. var settings = wp.apiRequest.buildAjaxOptions( settingsOriginal );
  78. assert.notStrictEqual( settings, settingsOriginal );
  79. assert.deepEqual( settings, {
  80. url: 'aaaa',
  81. headers: {
  82. 'Accept': 'application/json, */*;q=0.1'
  83. },
  84. data: {
  85. _wpnonce: 'definitely_not_a_real_nonce'
  86. }
  87. } );
  88. } );
  89. QUnit.test( 'does not add accept header if already present', function( assert ) {
  90. var settingsOriginal = {
  91. url: 'aaaa',
  92. headers: {
  93. 'Accept': 'text/xml'
  94. }
  95. };
  96. var settings = wp.apiRequest.buildAjaxOptions( settingsOriginal );
  97. assert.strictEqual( settingsOriginal.headers.Accept, settings.headers.Accept );
  98. } );
  99. QUnit.test( 'accepts namespace and endpoint', function( assert ) {
  100. assert.deepEqual( wp.apiRequest.buildAjaxOptions( {
  101. namespace: 'wp/v2',
  102. endpoint: 'posts'
  103. } ), {
  104. url: 'http://localhost/wp-json/wp/v2/posts',
  105. headers: expectedHeaders
  106. } );
  107. } );
  108. QUnit.test( 'accepts namespace and endpoint with slashes', function( assert ) {
  109. assert.deepEqual( wp.apiRequest.buildAjaxOptions( {
  110. namespace: '/wp/v2/',
  111. endpoint: '/posts'
  112. } ), {
  113. url: 'http://localhost/wp-json/wp/v2/posts',
  114. headers: expectedHeaders
  115. } );
  116. } );
  117. QUnit.test( 'accepts namespace and empty endpoint', function( assert ) {
  118. assert.deepEqual( wp.apiRequest.buildAjaxOptions( {
  119. namespace: 'wp/v2',
  120. endpoint: ''
  121. } ), {
  122. url: 'http://localhost/wp-json/wp/v2',
  123. headers: expectedHeaders
  124. } );
  125. } );
  126. QUnit.test( 'accepts empty namespace and empty endpoint', function( assert ) {
  127. assert.deepEqual( wp.apiRequest.buildAjaxOptions( {
  128. namespace: '',
  129. endpoint: ''
  130. } ), {
  131. url: 'http://localhost/wp-json/',
  132. headers: expectedHeaders
  133. } );
  134. } );
  135. QUnit.test(
  136. 'accepts namespace and endpoint with slashes (plain permalinks)',
  137. function( assert ) {
  138. window.wpApiSettings.root = 'http://localhost/index.php?rest_route=/';
  139. assert.deepEqual( wp.apiRequest.buildAjaxOptions( {
  140. namespace: '/wp/v2/',
  141. endpoint: '/posts?orderby=title'
  142. } ), {
  143. url: 'http://localhost/index.php?rest_route=/wp/v2/posts&orderby=title',
  144. headers: expectedHeaders
  145. } );
  146. }
  147. );
  148. QUnit.test(
  149. 'accepts empty namespace and empty endpoint (plain permalinks)',
  150. function( assert ) {
  151. window.wpApiSettings.root = 'http://localhost/index.php?rest_route=/';
  152. assert.deepEqual( wp.apiRequest.buildAjaxOptions( {
  153. namespace: '',
  154. endpoint: ''
  155. } ), {
  156. url: 'http://localhost/index.php?rest_route=/',
  157. headers: expectedHeaders
  158. } );
  159. }
  160. );
  161. } )( window.QUnit );