Skip to content

Added the "endpointURLParam" option #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions core/api.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</cfif>
<cfif !isUnhandledPathRequest(arguments.targetPath)>
<!--- if browsing to root of api, redirect to dashboard --->
<cfif len(cgi.path_info) lte 1 and len(cgi.query_string) eq 0 and listLast(cgi.script_name, "/") eq "index.cfm" and not application._taffy.settings.disableDashboard>
<cfif NOT application._taffy.settings.disableDashboard AND NOT structKeyExists(url,application._taffy.settings.endpointURLParam) AND NOT structKeyExists(form,application._taffy.settings.endpointURLParam) AND len(cgi.path_info) lte 1 and len(cgi.query_string) eq 0 and listLast(cgi.script_name, "/") eq "index.cfm">
<cfset local.basePath = listDeleteAt(cgi.script_name,listLen(cgi.script_name,"/"),"/") />
<cflocation url="#local.basePath#/?#application._taffy.settings.dashboardKey#" addtoken="false" />
</cfif>
Expand Down Expand Up @@ -161,8 +161,8 @@
</cfif>

<cfset local.allowVerbs = uCase(structKeyList(_taffyRequest.matchDetails.methods)) />
<cfif application._taffy.settings.allowCrossDomain
AND listFindNoCase('PUT,DELETE,OPTIONS',_taffyRequest.verb)
<cfif application._taffy.settings.allowCrossDomain
AND listFindNoCase('PUT,DELETE,OPTIONS',_taffyRequest.verb)
AND NOT listFind(local.allowVerbs,'OPTIONS')>
<cfset local.allowVerbs = listAppend(local.allowVerbs,'OPTIONS') />
</cfif>
Expand Down Expand Up @@ -280,6 +280,7 @@
<cfset local.defaultConfig.reloadKey = "reload" />
<cfset local.defaultConfig.reloadPassword = "true" />
<cfset local.defaultConfig.reloadOnEveryRequest = false />
<cfset local.defaultConfig.endpointURLParam = 'endpoint' />
<cfset local.defaultConfig.representationClass = "taffy.core.nativeJsonRepresentation" />
<cfset local.defaultConfig.dashboardKey = "dashboard" />
<cfset local.defaultConfig.disableDashboard = false />
Expand Down Expand Up @@ -344,11 +345,11 @@

<cfelse>
<h1>Taffy is up and running!</h1>
<p>It looks like you don't have any resources defined. Get started by creating the folder
<p>It looks like you don't have any resources defined. Get started by creating the folder
<code style="background-color: #F5DA81"><cfoutput>#guessResourcesFullPath()#</cfoutput></code> in which you should place your
Resource CFC's.</p>
<p>Or you could set up a bean factory, like <a href="http://www.coldspringframework.org/">ColdSpring</a>
or <a href="https://github.com/seancorfield/di1">DI/1</a>. Want to know more about using bean factories with Taffy?
<p>Or you could set up a bean factory, like <a href="http://www.coldspringframework.org/">ColdSpring</a>
or <a href="https://github.com/seancorfield/di1">DI/1</a>. Want to know more about using bean factories with Taffy?
<a href="https://github.com/atuttle/Taffy/wiki/So-you-want-to:-use-an-external-bean-factory-like-coldspring-to-completely-manage-resources"
>Check out the wiki!</a></p>
<p>If all else fails, I recommend starting with <a href="https://github.com/atuttle/Taffy/wiki/Getting-Started">Getting Started</a>.</p>
Expand All @@ -373,8 +374,17 @@
Actual desired method will be contained in a special header --->
<cfset var httpMethodOverride = GetPageContext().getRequest().getHeader("X-HTTP-Method-Override") />

<cfset requestObj.uri = getPath() />
<cfif NOT len(requestObj.uri)>
<cfif structKeyExists(url,application._taffy.settings.endpointURLParam)>
<cfset requestObj.uri = url[application._taffy.settings.endpointURLParam] />

<cfelseif structKeyExists(form,application._taffy.settings.endpointURLParam)>
<cfset requestObj.uri = form[application._taffy.settings.endpointURLParam] />
</cfif>
</cfif>

<!--- check for format in the URI --->
<cfset requestObj.uri = getPath() />
<cfset requestObj.uriFormat = formatFromURI(requestObj.uri) />

<!--- attempt to find the cfc for the requested uri --->
Expand Down
12 changes: 6 additions & 6 deletions core/factory.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@
<cfif len(basepath) eq 0>
<cfset arguments.basePath = "!@$%^&*()" />
</cfif>
<cfset var beanPath =
<cfset var beanPath =
"resources."
&
replace(
replace(path, basepath, ""),
replace(path, basepath, ""),
"/",
".",
"ALL"
)
&
&
"."
&
&
replace(
filename,
".cfc",
Expand All @@ -102,9 +102,9 @@
<cfif len(basepath) eq 0>
<cfset arguments.basePath = "!@$%^&*()" />
</cfif>
<cfreturn
<cfreturn
replace(
replace(path, basepath, ""),
replace(path, basepath, ""),
"/",
"",
"ALL"
Expand Down
28 changes: 20 additions & 8 deletions core/mocker.cfm
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,35 @@

<script type="text/javascript">
function submitRequest( verb, resource, representation ){
var endpoint = window.location.protocol + '//<cfoutput>#cgi.server_name#</cfoutput>';
var url = window.location.protocol + '//<cfoutput>#cgi.server_name#</cfoutput>';
var endpointURLParam = '<cfoutput>#jsStringFormat(application._taffy.settings.endpointURLParam)#</cfoutput>';
var endpoint = resource.split('?')[0];
var args = '';

if (window.location.port != 80){
endpoint += ':' + window.location.port;
url += ':' + window.location.port;
}
url += '<cfoutput>#cgi.SCRIPT_NAME#</cfoutput>';

if( resource.indexOf('?') && resource.split('?')[1] ){
args = resource.split('?')[1];
}
endpoint += '<cfoutput>#cgi.SCRIPT_NAME#</cfoutput>';
var url = endpoint + resource;
var dType = null;
if (representation && representation.indexOf("{") == 0){
dType = "application/json";

if( representation && representation.indexOf('{') == 0 ){
representation = $.parseJSON(representation);
representation[endpointURLParam] = endpoint;
} else {
representation = endpointURLParam + '=' + encodeURIComponent(endpoint);

if( args ){ representation += '&' + args; }
}

$("#rest_body").hide();
$.ajax({
type: verb,
url: url,
cache: false,
data: representation,
contentType: dType,
success: function(data, status, xhr){
$("#headers").val(xhr.getAllResponseHeaders());
$("#statuscode").val(xhr.status + " " + xhr.statusText).removeClass("statusError").addClass("statusSuccess");
Expand Down
2 changes: 2 additions & 0 deletions tests/Application.cfc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<cfcomponent extends="taffy.core.api" output="false">
<cfscript>
this.name = "Taffy_testSuite";
this.mappings['/resources'] = '#getDirectoryFromPath(getCurrentTemplatePath())#resources';
this.mappings['/resourcesError'] = '#getDirectoryFromPath(getCurrentTemplatePath())#resourcesError';

variables.framework = {};
variables.framework.disableDashboard = false;
Expand Down
17 changes: 17 additions & 0 deletions tests/tests/Application.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<cfcomponent>

<cfset this.name = 'Taffy_testSuite' /> <!---uses the same application name as the api Application.cfc for scope sharing --->
<cfset this.mappings['/resources'] = '#expandPath(replaceNoCase(cgi.script_name,'/tests/index.cfm',''))#/resources' />
<cfset this.mappings['/resourcesError'] = '#expandPath(replaceNoCase(cgi.script_name,'/tests/index.cfm',''))#/resourcesError' />


<cffunction name="onRequestStart" returnType="void" access="public" output="false">
<cfif NOT isDefined('application._taffy')>
<cfset local.apiRootURL = getDirectoryFromPath(cgi.script_name) />
<cfset local.apiRootURL = listDeleteAt(local.apiRootURL,listLen(local.apiRootURL,'/'),'/') />

<cfhttp method="GET" url="http://#CGI.SERVER_NAME#:#CGI.SERVER_PORT##local.apiRootURL#/index.cfm" />
</cfif>
</cffunction>

</cfcomponent>
28 changes: 27 additions & 1 deletion tests/tests/TestCore.cfc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<cfcomponent extends="base">

<cfscript>

function beforeTests(){
variables.taffy = createObject("component","taffy.tests.Application");
variables.factory = variables.taffy.getBeanFactory();
Expand Down Expand Up @@ -405,6 +404,33 @@
assertEquals( "dontpanic", application._taffy.settings.reloadPassword );
}

function use_endpointURLParam_in_GET(){
local.result = apiCall('get','?#application._taffy.settings.endpointURLParam#=/echo/2606.json','');

debug(local.result);
assertEquals(999,val(local.result.statusCode));
}

function use_endpointURLParam_in_POST(){
local.result = apiCall('post','?#application._taffy.settings.endpointURLParam#=/echo/2606.json','bar=foo');

debug(local.result);
assertEquals(200,val(local.result.statusCode));
}

function use_endpointURLParam_in_PUT(){
local.result = apiCall('put','?#application._taffy.settings.endpointURLParam#=/echo/2606.json','bar=foo');

debug(local.result);
assertEquals(200,val(local.result.statusCode));
}

function use_endpointURLParam_in_DELETE(){
local.result = apiCall('delete','?#application._taffy.settings.endpointURLParam#=/echo/tunnel/2606.json','');

debug(local.result);
assertEquals(200,val(local.result.statusCode));
}
</cfscript>


Expand Down
2 changes: 1 addition & 1 deletion tests/tests/TestFactory.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
assertTrue(structKeyExists(application._taffy.status, "skippedResources"));
assertTrue( arrayLen(application._taffy.status.skippedResources) gt 0 );

variables.factory.loadBeansFromPath( '/taffy/tests/resources', 'taffy.tests.resources', expandPath('taffy/tests/resources'), true );
variables.factory.loadBeansFromPath( expandPath('/taffy/tests/resources'), 'taffy.tests.resources', expandPath('/taffy/tests/resources'), true );
debug(application._taffy.status);
assertTrue(structKeyExists(application._taffy.status, "skippedResources"));
assertTrue( ArrayLen(application._taffy.status.skippedResources) eq 0, "Expected skipped resources array to be empty but it wasn't" );
Expand Down
15 changes: 11 additions & 4 deletions tests/tests/base.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
<cfargument name="basicauth" type="string" default="" />

<cfset var local = structNew() />
<cfset local.apiRootURL = getDirectoryFromPath(cgi.script_name) />
<cfset local.apiRootURL = listDeleteAt(local.apiRootURL,listLen(local.apiRootURL,'/'),'/') />

<cfif lcase(arguments.method) eq "put" or lcase(arguments.method) eq "post">
<!--- always reload the api before making a request --->
<cfhttp method="GET" url="http://#CGI.SERVER_NAME#:#CGI.SERVER_PORT#/taffy/tests/index.cfm/dashboard?reload=#application._taffy.settings.reloadPassword#" />
<cfhttp method="GET" url="http://#CGI.SERVER_NAME#:#CGI.SERVER_PORT##local.apiRootURL#/index.cfm?dashboard&reload=#application._taffy.settings.reloadPassword#" />
<cfset local.args = {} />
<cfset local.args.method = arguments.method />
<cfset local.args.url = "http://#CGI.SERVER_NAME#:#CGI.SERVER_PORT#/taffy/tests/index.cfm#arguments.uri#" />
<cfset local.args.url = "http://#CGI.SERVER_NAME#:#CGI.SERVER_PORT##local.apiRootURL#/index.cfm#arguments.uri#" />
<cfset local.args.result = "local.result" />
<cfset local.args.charset = "utf-8" />
<cfif len(arguments.basicauth)>
Expand All @@ -28,7 +30,7 @@
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded" />
</cfif>
<cfhttpparam type="body" value="#arguments.query#" />

<!--- Add arbitrary headers to request --->
<cfloop item="local.header" collection="#arguments.headers#">
<cfhttpparam type="header" name="#local.header#" value="#arguments.headers[local.header]#" />
Expand All @@ -37,7 +39,12 @@
<cfelse>
<cfset local.args = {} />
<cfset local.args.method = arguments.method />
<cfset local.args.url = "http://#CGI.SERVER_NAME#:#CGI.SERVER_PORT#/taffy/tests/index.cfm#arguments.uri#?#arguments.query#" />
<cfset local.args.url = "http://#CGI.SERVER_NAME#:#CGI.SERVER_PORT##local.apiRootURL#/index.cfm#arguments.uri#" />
<cfif find('?',local.args.url)>
<cfset local.args.url = '#local.args.url##arguments.query#' />
<cfelse>
<cfset local.args.url = '#local.args.url#?#arguments.query#' />
</cfif>
<cfset local.args.result = "local.result" />
<cfset local.args.charset = "utf-8" />
<cfif len(arguments.basicauth)>
Expand Down