/tests/tests/TestCore.cfc

http://github.com/atuttle/Taffy · ColdFusion CFScript · 645 lines · 460 code · 100 blank · 85 comment · 18 complexity · b8e87f5220ec2dc6970d92ef5b30414b MD5 · raw file

  1. <cfcomponent extends="base">
  2. <cffunction name="setup">
  3. <cfset reloadFramework()>
  4. </cffunction>
  5. <cfscript>
  6. function beforeTests(){
  7. variables.taffy = createObject("component","taffy.tests.Application");
  8. makePublic(variables.taffy, "getBeanFactory");
  9. variables.factory = variables.taffy.getBeanFactory();
  10. variables.factory.loadBeansFromPath( expandPath('/taffy/tests/resources'), 'taffy.tests.resources', expandPath('/taffy/tests/resources'), true );
  11. }
  12. function test_properly_notifies_unimplemented_mimes(){
  13. makePublic(variables.taffy, "mimeSupported");
  14. // debug(variables.taffy);
  15. // debug(application);
  16. assertFalse(taffy.mimeSupported("DoesNotExist"), "When given a mime type that should not exist, Taffy reported that it did.");
  17. }
  18. function test_properly_notifies_implemented_mimes(){
  19. makePublic(variables.taffy, "mimeSupported");
  20. makePublic(variables.taffy, "inspectMimeTypes");
  21. variables.taffy.inspectMimeTypes('taffy.core.nativeJsonSerializer', variables.taffy.getBeanFactory());
  22. assertTrue(taffy.mimeSupported("json"));
  23. assertTrue(taffy.mimeSupported("text/json"));
  24. assertTrue(taffy.mimeSupported("application/json"));
  25. }
  26. function test_returns_access_control_expose_headers_header(){
  27. local.result = apiCall("get", "/echo/foo.json", "");
  28. assertTrue(structKeyExists(local.result.responseHeader, "Access-Control-Expose-Headers"));
  29. debug(local.result.responseHeader["Access-Control-Expose-Headers"]);
  30. assertTrue(findNoCase("Etag", local.result.responseHeader["Access-Control-Expose-Headers"]));
  31. }
  32. function test_returns_etag_header(){
  33. //both requests should yield the same etag header
  34. local.result = apiCall("get", "/echo/foo.json", "");
  35. local.result2 = apiCall("get", "/echo/foo.json", "");
  36. assertTrue(structKeyExists(local.result.responseHeader, "Etag"));
  37. assertTrue(structKeyExists(local.result2.responseHeader, "Etag"));
  38. assertEquals(local.result2.responseHeader.etag, local.result.responseHeader.etag);
  39. }
  40. function test_returns_304_when_not_modified(){
  41. local.result = apiCall("get", "/echo/foo.json", "");
  42. assertTrue(structKeyExists(local.result.responseHeader, "Etag"));
  43. local.h = {};
  44. local.h['if-none-match'] = local.result.responseHeader.etag;
  45. local.result = apiCall("get", "/echo/foo.json", "", local.h);
  46. debug(local.result);
  47. assertEquals(304, val(local.result.responseHeader.status_code));
  48. }
  49. function test_json_result_is_json(){
  50. local.result = apiCall ("get","/echo/2.json","bar=foo");
  51. // debug(local.result);
  52. assertTrue(isJson(local.result.fileContent), "Expected JSON content back but was not able to identify it as such.");
  53. }
  54. function test_custom_status_is_returned(){
  55. local.result = apiCall("get", "/echo/1.json?foo=bar", "");
  56. // debug(local.result);
  57. // debug(application);
  58. assertEquals(999, local.result.responseHeader.status_code, "Expected status code 999 but got something else.");
  59. }
  60. function test_custom_headers_work(){
  61. local.result = apiCall("get", "/echo/-1.json", "");
  62. // debug(local.result);
  63. assertTrue(structKeyExists(local.result.responseHeader, "x-dude"), "Expected response header `x-dude` but it was not included.");
  64. }
  65. function test_global_headers_work(){
  66. local.result = apiCall("get", "/echo/1.json", "");
  67. // debug(local.result);
  68. assertTrue(structKeyExists(local.result.responseHeader, "x-foo-globalheader"), "Expected response header `x-foo-globalheader` but it was not included.");
  69. }
  70. function test_deserializer_inspection_finds_all_content_types(){
  71. makePublic(variables.taffy, "getSupportedContentTypes");
  72. local.result = variables.taffy.getSupportedContentTypes("taffy.core.baseDeserializer");
  73. // debug(local.result);
  74. assertTrue(structKeyExists(local.result, "application/x-www-form-urlencoded"));
  75. local.result = variables.taffy.getSupportedContentTypes("taffy.core.nativeJsonDeserializer");
  76. // debug(local.result);
  77. assertTrue(structKeyExists(local.result, "application/json"));
  78. assertTrue(structKeyExists(local.result, "text/json"));
  79. assertTrue(structKeyExists(local.result, "application/x-www-form-urlencoded"));
  80. }
  81. function test_deserializer_support_detection_works(){
  82. makePublic(variables.taffy, "contentTypeIsSupported");
  83. // debug(application._taffy.contentTypes);
  84. assertTrue(variables.taffy.contentTypeIsSupported("application/json"));
  85. assertTrue(variables.taffy.contentTypeIsSupported("application/json;v=1"));
  86. assertFalse(variables.taffy.contentTypeIsSupported("application/does-not-exist"));
  87. }
  88. function test_uri_regexes_are_correct(){
  89. makePublic(variables.taffy, "convertURItoRegex");
  90. local.result = taffy.convertURItoRegex("/a/{abc}/b");
  91. // debug(local.result);
  92. assertEquals( "^/a/([^\/]+)/b((?:\.)[^\.\?\/]+)?\/?$", local.result["uriregex"], "Resulted regex did not match expected. (assert 1)");
  93. assertEquals( 1, arrayLen(local.result["tokens"]), "assert 2" );
  94. assertEquals( "abc", local.result["tokens"][1], "assert 3" );
  95. local.result2 = taffy.convertURItoRegex("/a/{abc}");
  96. // debug(local.result2);
  97. assertEquals( "^/a/(?:(?:([^\/\.]+)(?:\.)([a-za-z0-9]+))\/?|([^\/\.]+))((?:\.)[^\.\?\/]+)?\/?$", local.result2["uriregex"], "Resulted regex did not match expected.");
  98. assertEquals( 1, arrayLen(local.result2["tokens"]) );
  99. assertEquals( "abc", local.result2["tokens"][1] );
  100. //custom regexes for tokens
  101. local.result3 = taffy.convertURItoRegex("/a/{b:[a-z]+(?:42){1}}");
  102. // debug(local.result3);
  103. assertEquals( "^/a/([a-z]+(?:42){1})((?:\.)[^\.\?\/]+)?\/?$", local.result3["uriregex"], "Resulted regex did not match expected. (assert 7)");
  104. assertEquals( 1, arrayLen(local.result3["tokens"]), "assert 8" );
  105. assertEquals( "b", local.result3["tokens"][1], "assert 9" );
  106. local.result4 = taffy.convertURItoRegex("/a/{b:[0-4]{1,7}(?:aaa){1}}/c/{d:\d+}");
  107. // debug(local.result4);
  108. assertEquals( "^/a/([0-4]{1,7}(?:aaa){1})/c/(\d+)((?:\.)[^\.\?\/]+)?\/?$", local.result4["uriregex"], "Resulted regex did not match expected. (assert 10)");
  109. assertEquals( 2, arrayLen(local.result4["tokens"]), "assert 11" );
  110. assertEquals( "b", local.result4["tokens"][1], "assert 12" );
  111. assertEquals( "d", local.result4["tokens"][2], "assert 13" );
  112. }
  113. function test_uri_matching_works_with_extension(){
  114. makePublic(variables.taffy, "matchURI");
  115. local.result = variables.taffy.matchURI("/echo/3.json");
  116. // debug(local.result);
  117. assertEquals('^/echo/(?:(?:([^\/\.]+)(?:\.)([a-za-z0-9]+))\/?|([^\/\.]+))((?:\.)[^\.\?\/]+)?\/?$', local.result);
  118. }
  119. function test_uri_matching_works_without_extension(){
  120. makePublic(variables.taffy, "matchURI");
  121. local.result = variables.taffy.matchURI("/echo/3");
  122. // debug(local.result);
  123. assertEquals('^/echo/(?:(?:([^\/\.]+)(?:\.)([a-za-z0-9]+))\/?|([^\/\.]+))((?:\.)[^\.\?\/]+)?\/?$', local.result);
  124. }
  125. function test_uri_matching_works_with_trailing_slash_with_extension(){
  126. makePublic(variables.taffy, "matchURI");
  127. local.result = variables.taffy.matchURI("/echo/3.json/");
  128. // debug(local.result);
  129. assertEquals('^/echo/(?:(?:([^\/\.]+)(?:\.)([a-za-z0-9]+))\/?|([^\/\.]+))((?:\.)[^\.\?\/]+)?\/?$', local.result);
  130. }
  131. function test_uri_matching_works_with_trailing_slash_without_extension(){
  132. makePublic(variables.taffy, "matchURI");
  133. local.result = variables.taffy.matchURI("/echo/3/");
  134. // debug(local.result);
  135. assertEquals('^/echo/(?:(?:([^\/\.]+)(?:\.)([a-za-z0-9]+))\/?|([^\/\.]+))((?:\.)[^\.\?\/]+)?\/?$', local.result);
  136. }
  137. function test_uri_matching_is_sorted_so_static_URIs_take_priority_over_tokens(){
  138. makePublic(variables.taffy, "matchURI");
  139. local.result = variables.taffy.matchURI("/echo/3");
  140. // debug(local.result);
  141. assertEquals('^/echo/(?:(?:([^\/\.]+)(?:\.)([a-za-z0-9]+))\/?|([^\/\.]+))((?:\.)[^\.\?\/]+)?\/?$', local.result);
  142. local.result = variables.taffy.matchURI("/echo/towel");
  143. // debug(local.result);
  144. assertEquals('^/echo/towel((?:\.)[^\.\?\/]+)?\/?$', local.result);
  145. }
  146. function test_request_parsing_works(){
  147. makePublic(variables.taffy,"buildRequestArguments");
  148. local.result = variables.taffy.buildRequestArguments(
  149. regex = '/echo/([^\/\.]+)$',
  150. tokenNamesArray = listToArray("id"),
  151. uri = '/echo/16',
  152. queryString = 'foo=bar&bar=foo',
  153. headers = structNew()
  154. );
  155. // debug(local.result);
  156. assertTrue(structKeyExists(local.result, "foo") && local.result.foo == "bar", "Missing or incorrect value for key `foo`.");
  157. assertTrue(structKeyExists(local.result, "bar") && local.result.bar == "foo", "Missing or incorrect value for key `bar`.");
  158. assertTrue(structKeyExists(local.result, "id") && local.result.id == 16, "Missing or incorrect value for key `id`.");
  159. }
  160. function test_properly_decodes_urlEncoded_put_request_body(){
  161. local.result = apiCall("put", "/echo/99.json", "foo=bar&check=mate");
  162. // debug(local.result);
  163. if (!isJson(local.result.fileContent)){
  164. // debug(local.result.fileContent);
  165. fail("Result was not JSON");
  166. return local.result.fileContent;
  167. }
  168. local.result = deserializeJSON(local.result.fileContent);
  169. assertTrue(structKeyExists(local.result, "foo") && local.result.foo == "bar", "Missing or incorrect value for key `foo`.");
  170. assertTrue(structKeyExists(local.result, "check") && local.result.check == "mate", "Missing or incorrect value for key `check`.");
  171. }
  172. function test_properly_decodes_json_put_request_body(){
  173. local.result = apiCall("put", "/echo/99.json", '{"foo":"bar"}');
  174. // debug(local.result);
  175. if (!isJson(local.result.fileContent)){
  176. fail("Result was not JSON");
  177. return;
  178. }
  179. local.result = deserializeJSON(local.result.fileContent);
  180. // debug(local.result);
  181. assertTrue(structKeyExists(local.result, "foo") && local.result.foo == "bar", "Missing or incorrect value for key `foo`.");
  182. }
  183. function test_properly_decodes_json_post_request_body(){
  184. local.result = apiCall("post", "/echo/99.json", '{"foo":"bar"}');
  185. // debug(local.result);
  186. if (!isJson(local.result.fileContent)){
  187. fail("Result was not JSON");
  188. return;
  189. }
  190. local.result = deserializeJSON(local.result.fileContent);
  191. // debug(local.result);
  192. assertTrue(structKeyExists(local.result, "foo") && local.result.foo == "bar", "Missing or incorrect value for key `foo`.");
  193. }
  194. function test_returns_error_when_requested_mime_not_supported(){
  195. local.h = structNew();
  196. local.h['Accept'] = "application/NOPE";
  197. local.result = apiCall ("get","/echo/2","foo=bar", local.h);
  198. // debug(local.result);
  199. assertEquals(400, local.result.responseHeader.status_code);
  200. assertEquals("Requested mime type is not supported (application/NOPE)", local.result.responseHeader.explanation);
  201. }
  202. function test_extension_takes_precedence_over_accept_header(){
  203. local.headers = structNew();
  204. local.headers["Accept"] = "text/xml";
  205. local.result = apiCall("get","/echo/2.json","foo=bar",local.headers);
  206. // debug(local.result);
  207. assertEquals(999, local.result.responseHeader.status_code);
  208. assertTrue(isJson(local.result.fileContent));
  209. }
  210. function test_allows_regex_as_final_url_value(){
  211. makePublic(variables.taffy, "buildRequestArguments");
  212. local.headers = structNew();
  213. local.headers.Accept = "application/json";
  214. local.tokenArray = arrayNew(1);
  215. arrayAppend(local.tokenArray, "id");
  216. local.result = variables.taffy.buildRequestArguments(
  217. "^/echo/([a-zA-Z0-9_\-\.\+]+@[a-zA-Z0-9_\-\.]+\.?[a-zA-Z]+)((?:\.)[^\.\?]+)?$",
  218. local.tokenArray,
  219. "/echo/foo@bar.com",
  220. "",
  221. local.headers
  222. );
  223. // debug(local.result);
  224. assertTrue(local.result._taffy_mime eq "json", "Did not detect desired return format correctly.");
  225. //full integration test for a@b.c.json
  226. local.result = apiCall("get", "/echo_regex/12345.json", "", {});
  227. // debug(local.result);
  228. assert(isJson(local.result.fileContent), "response was not json");
  229. local.response = deserializeJSON(local.result.fileContent);
  230. assertEquals("12345", local.response.id);
  231. //full integration test for a@b.c (no .json, but with headers)
  232. local.result = apiCall("get", "/echo_regex/12345", "", local.headers);
  233. // debug(local.result);
  234. assert(isJson(local.result.fileContent), "response was not json");
  235. local.response = deserializeJSON(local.result.fileContent);
  236. assertEquals("12345", local.response.id);
  237. }
  238. function test_returns_405_for_unimplemented_verbs(){
  239. local.result = apiCall("delete", "/echo/2.json", "foo=bar");
  240. // debug(local.result);
  241. assertEquals(405, local.result.responseHeader.status_code);
  242. }
  243. function test_test_onTaffyRequest_allow(){
  244. local.result = apiCall("get","/echo/12.json","refuse=false");
  245. // debug(local.result);
  246. assertEquals(999,local.result.responseHeader.status_code);
  247. }
  248. function test_onTaffyRequest_deny(){
  249. local.result = apiCall("get","/echo/12.json","refuse=true");
  250. // debug(local.result);
  251. assertEquals(405,local.result.responseHeader.status_code);
  252. }
  253. function test_getCacheKey_customBehavior() {
  254. local.result = variables.taffy.getCacheKey(
  255. "EchoMember",
  256. { "foo": "bar" },
  257. "/echo/12.json"
  258. );
  259. assertEquals("echomember_foo", local.result);
  260. }
  261. function test_getCacheKey_defaultBehavior() {
  262. local.args = {
  263. cfc: "EchoMember",
  264. requestArguments: { "default": true },
  265. matchedURI: "/echo/12.json"
  266. };
  267. local.result = variables.taffy.getCacheKey(argumentCollection = local.args);
  268. // ACF and Lucee generate hash code differently
  269. assertEquals("/echo/12.json_#local.args.requestArguments.hashCode()#", local.result);
  270. }
  271. function test_external_file_request_passes_through(){
  272. local.result = getUrl('http://#CGI.SERVER_NAME#:#CGI.SERVER_PORT##replace(cgi.script_name, "/tests/tests/run.cfm", "/tests/someFolder/someOtherFile.cfm")#');
  273. debug(local.result);
  274. assertTrue(findNoCase('woot', local.result.fileContent), "Was not able to get the DMZ file.");
  275. }
  276. function test_tunnel_PUT_through_POST(){
  277. var local = {};
  278. local.headers["X-HTTP-Method-Override"] = "PUT";
  279. local.result = apiCall("post","/echo/tunnel/12.json","", local.headers);
  280. // debug(local.result);
  281. assertEquals(200,local.result.responseHeader.status_code);
  282. local.deserializedContent = deserializeJSON( local.result.fileContent );
  283. // debug( local.deserializedContent );
  284. assertEquals("put", local.deserializedContent.actualMethod);
  285. }
  286. function test_tunnel_DELETE_through_POST(){
  287. var local = {};
  288. local.headers["X-HTTP-Method-Override"] = "DELETE";
  289. local.result = apiCall("post","/echo/tunnel/12.json","", local.headers);
  290. // debug(local.result);
  291. assertEquals(200,local.result.responseHeader.status_code);
  292. local.deserializedContent = deserializeJSON( local.result.fileContent );
  293. // debug( local.deserializedContent );
  294. assertEquals("delete", local.deserializedContent.actualMethod);
  295. }
  296. function test_put_body_is_mime_content(){
  297. var local = {};
  298. local.result = apiCall(
  299. "put",
  300. "/echo/12.json",
  301. '{"foo":"The quick brown fox jumped over the lazy dog."}'
  302. );
  303. // debug(local.result);
  304. assertEquals(200,local.result.responseHeader.status_code);
  305. local.deserializedContent = deserializeJSON( local.result.fileContent );
  306. // debug( local.deserializedContent );
  307. // The service response should contain only the ID parameter, and not anything parsed from the body
  308. assertEquals("foo,id,password,username", listSort(structKeylist(local.deserializedContent), "textnocase"));
  309. assertEquals(12, local.deserializedContent["id"]);
  310. assertEquals("The quick brown fox jumped over the lazy dog.", local.deserializedContent["foo"]);
  311. }
  312. function test_put_body_is_url_encoded_params(){
  313. var local = {};
  314. local.result = apiCall(
  315. "put",
  316. "/echo/12.json",
  317. "foo=yankee&bar=hotel&baz=foxtrot"
  318. );
  319. // debug(local.result);
  320. assertEquals(200,local.result.responseHeader.status_code);
  321. local.deserializedContent = deserializeJSON( local.result.fileContent );
  322. // debug( local.deserializedContent );
  323. // The service response should contain the ID parameter and all parsed form fields from the body
  324. local.sortedKeys = listSort(structKeylist(local.deserializedContent), "textnocase");
  325. //because apparently railo includes fieldnames when ACF doesn't...
  326. assertTrue("bar,baz,foo,id,password,username" eq local.sortedKeys or "bar,baz,fieldnames,foo,id,password,username" eq local.sortedKeys);
  327. assertEquals(12, local.deserializedContent["id"]);
  328. assertEquals("yankee", local.deserializedContent["foo"]);
  329. assertEquals("hotel", local.deserializedContent["bar"]);
  330. assertEquals("foxtrot", local.deserializedContent["baz"]);
  331. }
  332. function test_get_queryString_keys_without_values_returns_empty_string() {
  333. makePublic(variables.taffy, "buildRequestArguments");
  334. var returnedArguments = variables.taffy.buildRequestArguments(
  335. regex = "^/testResource/$",
  336. tokenNamesArray = [],
  337. uri = "/testResource/",
  338. queryString = "keyOne=valueOne&keyTwo=&keyThree=valueThree",
  339. headers = {}
  340. );
  341. assertEquals("", returnedArguments["keyTwo"]);
  342. }
  343. function test_returns_allow_header_for_405(){
  344. local.result = apiCall("delete","/echo/12.json","");
  345. // debug(local.result);
  346. assertEquals(405,local.result.responseHeader.status_code);
  347. assertTrue(structKeyExists(local.result.responseHeader, "allow"),"Expected ALLOW header, but couldn't find it");
  348. }
  349. function test_returns_allow_header_for_get_200(){
  350. local.result = apiCall("get","/echo/tunnel/12.json","");
  351. // debug(local.result);
  352. assertEquals(200,local.result.responseHeader.status_code);
  353. assertTrue(structKeyExists(local.result.responseHeader, "allow"),"Expected ALLOW header, but couldn't find it");
  354. }
  355. function test_returns_allow_header_for_post_201(){
  356. local.result = apiCall("post","/echo/tunnel/12.json","");
  357. // debug(local.result);
  358. assertEquals(201,local.result.responseHeader.status_code);
  359. assertTrue(structKeyExists(local.result.responseHeader, "allow"),"Expected ALLOW header, but couldn't find it");
  360. }
  361. function test_returns_allow_header_for_put_200(){
  362. local.result = apiCall("put","/echo/tunnel/12.json","");
  363. // debug(local.result);
  364. assertEquals(200,local.result.responseHeader.status_code);
  365. assertTrue(structKeyExists(local.result.responseHeader, "allow"),"Expected ALLOW header, but couldn't find it");
  366. }
  367. function test_returns_allow_header_for_delete_200(){
  368. local.result = apiCall("delete","/echo/tunnel/12.json","");
  369. // debug(local.result);
  370. assertEquals(200,local.result.responseHeader.status_code);
  371. assertTrue(structKeyExists(local.result.responseHeader, "allow"),"Expected ALLOW header, but couldn't find it");
  372. }
  373. function test_can_pass_data_from_onTaffyRequest_to_resource(){
  374. local.result = apiCall("get", "/echo/dude.json", "hulk=smash");
  375. // debug(local.result);
  376. local.body = deserializeJSON(local.result.fileContent);
  377. assertTrue(structKeyExists(local.body, "dataFromOTR"));
  378. assertTrue(local.body.dataFromOTR eq "who let the hulk out?!");
  379. }
  380. function test_reload_on_every_request_setting_works(){
  381. application._taffy.settings.reloadOnEveryRequest = false;
  382. local.result = apiCall("get", "/echo/dude.json", "");
  383. // debug(local.result);
  384. assertFalse(structKeyExists(local.result.responseheader, "X-TAFFY-RELOADED"), "Expected reload header to be missing, but it was sent.");
  385. application._taffy.settings.reloadOnEveryRequest = true;
  386. local.result2 = apiCall("get", "/echo/dude.json", "");
  387. // debug(local.result2);
  388. assertTrue(structKeyExists(local.result2.responseheader, "X-TAFFY-RELOADED"), "Expected reload header to be sent, but it was missing.");
  389. }
  390. function test_returns_error_when_resource_throws_exception(){
  391. local.result = apiCall("get", "/throwException.json", "");
  392. // debug(local.result);
  393. assertEquals(500, local.result.responseHeader.status_code);
  394. assertTrue( isJson( local.result.fileContent ), "Response body was not json" );
  395. }
  396. function test_basic_auth_credentials_found(){
  397. local.result = apiCall("get", "/basicauth.json", "", {}, "Towel:42");
  398. // debug(local.result);
  399. assertTrue(isJson(local.result.fileContent));
  400. local.data = deserializeJSON(local.result.fileContent);
  401. assertTrue(structKeyExists(local.data, "username"));
  402. assertEquals("Towel", local.data.username);
  403. assertTrue(structKeyExists(local.data, "password"));
  404. assertEquals("42", local.data.password);
  405. }
  406. function test_getHostname_returns_not_blank(){
  407. local.hostname = variables.taffy.getHostname();
  408. // debug(local.hostname);
  409. assertNotEquals( "", local.hostname );
  410. }
  411. function test_envConfig_is_applied(){
  412. // debug( application._taffy.settings.reloadPassword );
  413. assertEquals( "dontpanic", application._taffy.settings.reloadPassword );
  414. }
  415. function test_use_endpointURLParam_in_GET(){
  416. local.result = apiCall('get','?#application._taffy.settings.endpointURLParam#=/echo/2606.json','');
  417. // debug(local.result);
  418. assertEquals(999,val(local.result.statusCode));
  419. }
  420. function test_use_endpointURLParam_in_POST(){
  421. local.result = apiCall('post','?#application._taffy.settings.endpointURLParam#=/echo/2606.json','bar=foo');
  422. // debug(local.result);
  423. assertEquals(200,val(local.result.statusCode));
  424. }
  425. function test_use_endpointURLParam_in_PUT(){
  426. local.result = apiCall('put','?#application._taffy.settings.endpointURLParam#=/echo/2606.json','bar=foo');
  427. // debug(local.result);
  428. assertEquals(200,val(local.result.statusCode));
  429. }
  430. function test_use_endpointURLParam_in_DELETE(){
  431. local.result = apiCall('delete','?#application._taffy.settings.endpointURLParam#=/echo/tunnel/2606.json','');
  432. // debug(local.result);
  433. assertEquals(200,val(local.result.statusCode));
  434. }
  435. function test_allows_dashboard_when_enabled(){
  436. var restore = application._taffy.settings.disableDashboard;
  437. application._taffy.settings.disableDashboard = false;
  438. local.result = apiCall("get", "/", "");
  439. // debug(local.result);
  440. assertEquals(200, val(local.result.statusCode));
  441. application._taffy.settings.disableDashboard = restore;
  442. }
  443. function test_returns_403_at_root_when_dashboard_disabled_with_no_redirect(){
  444. var restore = application._taffy.settings.disableDashboard;
  445. application._taffy.settings.disableDashboard = true;
  446. local.result = apiCall("get", "/", "");
  447. // debug(local.result);
  448. assertEquals(403, val(local.result.statusCode));
  449. application._taffy.settings.disableDashboard = restore;
  450. }
  451. function test_returns_302_at_root_when_dashboard_disabled_with_redirect(){
  452. var restore1 = application._taffy.settings.disableDashboard;
  453. var restore2 = application._taffy.settings.disabledDashboardRedirect;
  454. application._taffy.settings.disableDashboard = true;
  455. application._taffy.settings.disabledDashboardRedirect = 'https://google.com';
  456. local.result = apiCall("get", "/", "");
  457. // debug(local.result);
  458. assertEquals(302, val(local.result.statusCode));
  459. assertTrue(structKeyExists(local.result.responseHEader, "location"));
  460. assertEquals(application._taffy.settings.disabledDashboardRedirect, local.result.responseHeader.location);
  461. application._taffy.settings.disableDashboard = restore1;
  462. application._taffy.settings.disabledDashboardRedirect = restore2;
  463. }
  464. function test_properly_returns_wrapped_jsonp(){
  465. application._taffy.settings.jsonp = "callback";
  466. local.result = apiCall("get", "/echo/dude.json?callback=zomg", '');
  467. // debug(local.result);
  468. assertEquals('zomg(', left(local.result.fileContent, 5), "Does not begin with call to jsonp callback");
  469. assertEquals(");", right(local.result.fileContent, 2), "Does not end with `);`");
  470. }
  471. function test_properly_handles_arbitrary_cors_headers(){
  472. //see: https://github.com/atuttle/Taffy/issues/144
  473. application._taffy.settings.allowCrossDomain = true;
  474. local.h = { "Access-Control-Request-Headers" = "goat, pigeon, man-bear-pig", "Origin":"http://#cgi.server_name#/"};
  475. local.result = apiCall("get", "/echo/dude.json", "", local.h);
  476. //debug(local.result);
  477. assertTrue(local.result.responseHeader["Access-Control-Allow-Headers"] contains "goat");
  478. assertTrue(local.result.responseHeader["Access-Control-Allow-Headers"] contains "pigeon");
  479. assertTrue(local.result.responseHeader["Access-Control-Allow-Headers"] contains "man-bear-pig");
  480. }
  481. function test_properly_handles_arbitrary_cors_headers_on_error(){
  482. //see: https://github.com/atuttle/Taffy/issues/159
  483. application._taffy.settings.allowCrossDomain = true;
  484. local.h = { "Access-Control-Request-Headers" = "goat, pigeon, man-bear-pig", "Origin":"http://#cgi.server_name#/"};
  485. local.result = apiCall("get", "/throwException.json", "", local.h);
  486. // debug(local.result);
  487. assertTrue(structKeyExists(local.result.responseHeader, "Access-Control-Allow-Origin"));
  488. assertTrue(structKeyExists(local.result.responseHeader, "Access-Control-Allow-Methods"));
  489. assertTrue(structKeyExists(local.result.responseHeader, "Access-Control-Allow-Headers"));
  490. assertTrue(local.result.responseHeader["Access-Control-Allow-Headers"] contains "goat");
  491. assertTrue(local.result.responseHeader["Access-Control-Allow-Headers"] contains "pigeon");
  492. assertTrue(local.result.responseHeader["Access-Control-Allow-Headers"] contains "man-bear-pig");
  493. }
  494. function test_non_struct_json_body_sent_to_resource_as_underscore_body(){
  495. //see: https://github.com/atuttle/Taffy/issues/169
  496. local.result = apiCall("post", "/echo/5", "[1,2,3]");
  497. // debug(local.result);
  498. local.response = deserializeJSON(local.result.fileContent);
  499. assertTrue(structKeyExists(local.response, "_body"));
  500. assertTrue(isArray(local.response._body));
  501. assertTrue(arrayLen(local.response._body) == 3);
  502. }
  503. function test_comma_delim_list_of_uris_for_alias(){
  504. //works with /echo_alias/{ID}
  505. local.result = apiCall("get", "/echo_alias/4", "");
  506. //debug(local.result);
  507. assertEquals(200, val(local.result.statusCode));
  508. assertEquals(serializeJSON({ID="4"}), local.result.fileContent);
  509. //works with /echo_alias
  510. local.result = apiCall("get", "/echo_alias", "");
  511. // debug(local.result);
  512. assertEquals(200, val(local.result.statusCode));
  513. assertEquals(serializeJSON({ID="0"}), local.result.fileContent);
  514. //works with /echo_alias/ (trailing slash)
  515. local.result = apiCall("get", "/echo_alias/", "");
  516. // debug(local.result);
  517. assertEquals(200, val(local.result.statusCode));
  518. assertEquals(serializeJSON({ID="0"}), local.result.fileContent);
  519. //works with /echo_alias?ID=x
  520. local.result = apiCall("get", "/echo_alias", "ID=2");
  521. // debug(local.result);
  522. assertEquals(200, val(local.result.statusCode));
  523. assertEquals(serializeJSON({ID="2"}), local.result.fileContent);
  524. }
  525. </cfscript>
  526. <cffunction name="test_can_upload_a_file">
  527. <cfset var local = structNew() />
  528. <cfset local.apiRootURL = getDirectoryFromPath(cgi.script_name) />
  529. <cfset local.apiRootURL = listDeleteAt(local.apiRootURL,listLen(local.apiRootURL,'/'),'/') />
  530. <cfhttp
  531. url="http://#cgi.server_name#:#cgi.server_port##local.apiRootURL#/index.cfm/upload"
  532. method="post"
  533. result="local.uploadResult">
  534. <cfhttpparam type="file" name="img" file="#expandPath('/taffy/tests/tests/upload.png')#" />
  535. </cfhttp>
  536. <!--- <cfset debug(local.uploadResult) /> --->
  537. <cfset assertTrue(local.uploadResult.statusCode eq "200 OK", "Did not return status 200") />
  538. </cffunction>
  539. <cffunction name="test_throws_exception_when_ressource_uri_doesnt_begin_with_forward_slash">
  540. <cfset assertTrue(checkIfOneSkippedRessourceContainsExpectedException("detail", "The URI (uriWithoutForwardSlash) for `uriDoesntBeginWithForwardSlash` should begin with a forward slash."), "Uri without forward slash not showing in errors")>
  541. </cffunction>
  542. <cffunction name="test_throws_exception_when_alias_ressource_uri_doesnt_begin_with_forward_slash">
  543. <cfset assertTrue(checkIfOneSkippedRessourceContainsExpectedException("detail", "The URI (uriAliasWithoutFowardSlash) for `uriAliasDoesntBeginWithForwardSlash` should begin with a forward slash."), "Uri alias without forward slash not showing in errors")>
  544. </cffunction>
  545. </cfcomponent>