PageRenderTime 27ms CodeModel.GetById 20ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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