Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rest

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rest - npm Package Compare versions

Comparing version 0.8.1 to 0.8.2

interceptor/retry.js

82

client/jsonp.js

@@ -1,6 +0,11 @@

(function (global, document, define) {
(function (define, global, document) {
define(['when', '../UrlBuilder'], function (when, UrlBuilder) {
define(function (require) {
"use strict";
var when, UrlBuilder;
when = require('when');
UrlBuilder = require('../UrlBuilder');
// consider abstracting this into a util module

@@ -19,3 +24,9 @@ function clearProperty(scope, propertyName) {

function registerCallback(prefix, resolver) {
function cleanupScriptNode(response) {
if (response.raw && response.raw.parentNode) {
response.raw.parentNode.removeChild(response.raw);
}
}
function registerCallback(prefix, resolver, response) {
var name;

@@ -29,4 +40,8 @@

global[name] = function jsonpCallback(data) {
resolver.resolve({ entity: data });
response.entity = data;
clearProperty(global, name);
cleanupScriptNode(response);
if (!response.request.canceled) {
resolver.resolve(response);
}
};

@@ -48,22 +63,45 @@

function jsonp(request) {
var d, callbackParams, script, firstScript;
var d, callbackName, callbackParams, script, firstScript, response;
response = {
request: request
};
if (request.canceled) {
response.error = 'precanceled';
return when.reject(response);
}
d = when.defer();
request.callback = request.callback || {};
callbackName = registerCallback(request.callback.prefix || 'jsonp', d.resolver, response);
callbackParams = {};
callbackParams[request.callback.param || 'callback'] = callbackName;
try {
request.callback = request.callback || {};
callbackParams = {};
callbackParams[request.callback.param || 'callback'] = registerCallback(request.callback.prefix || 'jsonp', d.resolver);
request.canceled = false;
request.cancel = function cancel() {
request.canceled = true;
cleanupScriptNode(response);
d.reject(response);
};
script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = new UrlBuilder(request.path, request.params).build(callbackParams);
firstScript = document.getElementsByTagName('script')[0];
firstScript.parentNode.insertBefore(script, firstScript);
}
catch (e) {
d.reject(e);
}
script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = new UrlBuilder(request.path, request.params).build(callbackParams);
script.onload = script.onerror = script.onreadystatechange = function (e) {
// script tag load callbacks are completely non-standard
if ((e && (e.type === 'load' || e.type === 'error')) || script.readyState === 'loaded') {
if (global[callbackName]) {
response.error = 'loaderror';
d.reject(response);
}
}
};
response.raw = script;
firstScript = document.getElementsByTagName('script')[0];
firstScript.parentNode.insertBefore(script, firstScript);
return d.promise;

@@ -77,8 +115,6 @@ }

}(
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); },
this,
this.document,
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
this.document
// Boilerplate for AMD and Node
));

@@ -1,73 +0,95 @@

var parser, http, https, when, UrlBuilder, normalizeHeaderName, httpsExp;
(function (define) {
parser = require('url');
http = require('http');
https = require('https');
when = require('when');
UrlBuilder = require('../UrlBuilder');
normalizeHeaderName = require('../util/normalizeHeaderName');
httpsExp = /^https/i;
define(function (require) {
"use strict";
function node(request) {
"use strict";
var parser, http, https, when, UrlBuilder, normalizeHeaderName, httpsExp;
var d, options, clientRequest, client, url, headers, entity;
parser = require('url');
http = require('http');
https = require('https');
when = require('when');
UrlBuilder = require('../UrlBuilder');
normalizeHeaderName = require('../util/normalizeHeaderName');
d = when.defer();
httpsExp = /^https/i;
url = new UrlBuilder(request.path || '', request.params).build();
client = url.match(httpsExp) ? https : http;
function node(request) {
options = parser.parse(url);
entity = request.entity;
request.method = request.method || (entity ? 'POST' : 'GET');
options.method = request.method;
headers = options.headers = {};
Object.keys(request.headers || {}).forEach(function (name) {
headers[normalizeHeaderName(name)] = request.headers[name];
});
if (!headers['Content-Length']) {
headers['Content-Length'] = entity ? Buffer.byteLength(entity, 'utf8') : 0;
}
var d, options, clientRequest, client, url, headers, entity, response;
try {
clientRequest = client.request(options, function (clientResponse) {
var response;
response = {};
response.request = request;
response.raw = clientResponse;
response.status = {
code: clientResponse.statusCode
// node doesn't provide access to the status text
};
response.headers = {};
Object.keys(clientResponse.headers).forEach(function (name) {
response.headers[normalizeHeaderName(name)] = clientResponse.headers[name];
if (request.canceled) {
response.error = 'precanceled';
return when.reject(response);
}
d = when.defer();
url = new UrlBuilder(request.path || '', request.params).build();
client = url.match(httpsExp) ? https : http;
options = parser.parse(url);
entity = request.entity;
request.method = request.method || (entity ? 'POST' : 'GET');
options.method = request.method;
headers = options.headers = {};
Object.keys(request.headers || {}).forEach(function (name) {
headers[normalizeHeaderName(name)] = request.headers[name];
});
if (!headers['Content-Length']) {
headers['Content-Length'] = entity ? Buffer.byteLength(entity, 'utf8') : 0;
}
clientResponse.on('data', function (data) {
if (!('entity' in response)) {
response.entity = '';
}
// normalize Buffer to a String
response.entity += data.toString();
request.canceled = false;
request.cancel = function cancel() {
request.canceled = true;
clientRequest.abort();
};
clientRequest = client.request(options, function (clientResponse) {
response.raw = clientResponse;
response.status = {
code: clientResponse.statusCode
// node doesn't provide access to the status text
};
response.headers = {};
Object.keys(clientResponse.headers).forEach(function (name) {
response.headers[normalizeHeaderName(name)] = clientResponse.headers[name];
});
clientResponse.on('data', function (data) {
if (!('entity' in response)) {
response.entity = '';
}
// normalize Buffer to a String
response.entity += data.toString();
});
clientResponse.on('end', function () {
d.resolve(response);
});
});
clientResponse.on('end', function () {
d.resolve(response);
clientRequest.on('error', function (e) {
response.error = e;
d.reject(response);
});
});
if (entity) {
clientRequest.write(entity);
if (entity) {
clientRequest.write(entity);
}
clientRequest.end();
return d.promise;
}
clientRequest.end();
}
catch (e) {
d.reject(e);
}
return d.promise;
}
return node;
module.exports = node;
});
}(
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));

@@ -1,8 +0,12 @@

(function (XMLHttpRequest, define) {
(function (define, XMLHttpRequest) {
define(["when", "../UrlBuilder", "../util/normalizeHeaderName"], function (when, UrlBuilder, normalizeHeaderName) {
define(function (require) {
"use strict";
var headerSplitRE;
var when, UrlBuilder, normalizeHeaderName, headerSplitRE;
when = require('when');
UrlBuilder = require('../UrlBuilder');
normalizeHeaderName = require('../util/normalizeHeaderName');
// according to the spec, the line break is '\r\n', but doesn't hold true in practice

@@ -42,13 +46,22 @@ headerSplitRE = /[\r|\n]+/;

function xhr(request) {
var d, client, method, url, headers, entity, headerName;
var d, client, method, url, headers, entity, headerName, response;
response = {};
response.request = request;
if (request.canceled) {
response.error = 'precanceled';
return when.reject(response);
}
d = when.defer();
client = response.raw = new XMLHttpRequest();
entity = request.entity;
request.method = request.method || (entity ? 'POST' : 'GET');
method = request.method;
url = new UrlBuilder(request.path || '', request.params).build();
try {
client = new XMLHttpRequest();
entity = request.entity;
request.method = request.method || (entity ? 'POST' : 'GET');
method = request.method;
url = new UrlBuilder(request.path || '', request.params).build();
client.open(method, url, true);

@@ -61,9 +74,12 @@

request.canceled = false;
request.cancel = function cancel() {
request.canceled = true;
client.abort();
d.reject(response);
};
client.onreadystatechange = function (e) {
var response;
if (request.canceled) { return; }
if (client.readyState === (XMLHttpRequest.DONE || 4)) {
response = {};
response.request = request;
response.raw = client;
response.status = {

@@ -76,10 +92,24 @@ code: client.status,

d.resolve(response);
if (response.status.code > 0) {
// check status code as readystatechange fires before error event
d.resolve(response);
}
}
};
try {
client.onerror = function (e) {
response.error = 'loaderror';
d.reject(response);
};
}
catch (e) {
// IE 6 will not support error handling
}
client.send(entity);
}
catch (e) {
d.reject(e);
response.error = 'loaderror';
d.resolver.reject(response);
}

@@ -95,7 +125,5 @@

}(
this.XMLHttpRequest,
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); },
this.XMLHttpRequest
// Boilerplate for AMD and Node
));
(function (define) {
define(['../../rest', '../util/mixin', 'dojo/store/util/QueryResults'], function (defaultClient, mixin, queryResults) {
define(function (require) {
"use strict";
var defaultClient, mixin, queryResults;
defaultClient = require('../../rest');
mixin = require('../util/mixin');
queryResults = require('dojo/store/util/QueryResults');
/**

@@ -145,6 +152,4 @@ * A REST based object store.

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define(['./RestStore', '../wire', '../util/mixin', 'when'], function (RestStore, clientPlugin, mixin, when) {
define(function (require) {
"use strict";
var RestStore, clientPlugin, mixin, when;
RestStore = require('./RestStore');
clientPlugin = require('../wire');
mixin = require('../util/mixin');
when = require('when');
/**

@@ -88,6 +96,4 @@ * If wait === true, waits for dataPromise to complete and resolves

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define(['../rest', 'when'], function (defaultClient, when) {
define(function (require) {
"use strict";
var defaultClient, when;
defaultClient = require('../rest');
when = require('when');
/**

@@ -29,6 +34,15 @@ * Interceptors have the ability to intercept the request and/org response

function defaultResponseHandler(response, config, client) {
function defaultResponseHandler(response, config, interceptor) {
return response;
}
function whenFirst(promisesOrValues) {
// TODO this concept is likely to be added to when in the near future
var d = when.defer();
promisesOrValues.forEach(function (promiseOrValue) {
when.chain(promiseOrValue, d.resolver);
});
return d.promise;
}
/**

@@ -40,3 +54,3 @@ * Create a new interceptor for the provided handlers.

* @param {Function} [handlers.success] response handler when the request is not in error
* @param {Function} [handlers.error] response handler when the request is in error
* @param {Function} [handlers.error] response handler when the request is in error, may be used to 'unreject' an error state
* @param {Function} [handlers.client] the client to use if otherwise not specified, defaults to platform default client

@@ -54,3 +68,6 @@ *

successResponseHandler = handlers.success || handlers.response || defaultResponseHandler;
errorResponseHandler = handlers.error || handlers.response || defaultResponseHandler;
errorResponseHandler   = handlers.error   || function () {
// Propagate the rejection, with the result of the handler
return when.reject((handlers.response || defaultResponseHandler).apply(this, arguments));
};

@@ -71,12 +88,20 @@ return function (client, config) {

return when(requestHandler(request, config)).then(function (request) {
return when(client(request)).then(
function (response) {
return successResponseHandler(response, config, client);
},
function (response) {
// Propagate the rejection, but with the result of the
// registered error response handler
return when.reject(errorResponseHandler(response, config, client));
}
);
var response, abort, d;
if (request instanceof Array) {
// unpack compound value
abort = request[1];
request = request[0];
}
response = when(request, function (request) {
return when(
client(request),
function (response) {
return successResponseHandler(response, config, interceptor);
},
function (response) {
return errorResponseHandler(response, config, interceptor);
}
);
});
return abort ? whenFirst([response, abort]) : response;
});

@@ -95,6 +120,4 @@ };

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define(['../interceptor', '../util/base64'], function (interceptor, base64) {
define(function (require) {
"use strict";
var interceptor, base64;
interceptor = require('../interceptor');
base64 = require('../util/base64');
/**

@@ -34,6 +39,4 @@ * Authenticates the request using HTTP Basic Authentication (rfc2617)

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define(['../interceptor'], function (interceptor) {
define(function (require) {
"use strict";
var interceptor;
interceptor = require('../interceptor');
/**

@@ -26,6 +30,4 @@ * Returns the response entity as the response, discarding other response

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define(['../interceptor', 'when'], function (interceptor, when) {
define(function (require) {
"use strict";
var interceptor, when;
interceptor = require('../interceptor');
when = require('when');
/**

@@ -30,6 +35,4 @@ * Rejects the response promise based on the status code.

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define(['../interceptor', './pathPrefix', '../../rest'], function (interceptor, pathPrefix, defaultClient) {
define(function (require) {
"use strict";
var hateoas, cycleFlag;
var interceptor, pathPrefix, cycleFlag;
interceptor = require('../interceptor');
pathPrefix = require('./pathPrefix');
cycleFlag = '__rest_hateoas_seen__';

@@ -35,11 +38,11 @@

* @param {String} [config.target='_links'] property to create on the entity and parse links into. If present and falsey, the response entity is used directly.
* @param {Client} [config.client] the default parent client to use when creating clients for a linked resources
* @param {Client} [config.client] the parent client to use when creating clients for a linked resources. Defaults to the current interceptor's client
*
* @returns {Client}
*/
hateoas = interceptor({
response: function (response, config) {
return interceptor({
response: function (response, config, hateoas) {
var targetName, client;
client = config.client || defaultClient;
client = config.client || hateoas;
targetName = 'target' in config ? config.target || '' : '_links';

@@ -56,3 +59,3 @@

get: function () {
return hateoas(client, config)({ path: link.href });
return client({ path: link.href });
}

@@ -111,11 +114,7 @@ });

return hateoas;
});
}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define(['../interceptor', '../client/jsonp'], function (interceptor, jsonpClient) {
define(function (require) {
"use strict";
var interceptor, jsonpClient;
interceptor = require('../interceptor');
jsonpClient = require('../client/jsonp');
/**

@@ -35,6 +40,4 @@ * Allows common configuration of JSONP clients.

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define(['../interceptor'], function (interceptor) {
define(function (require) {
"use strict";
var interceptor;
interceptor = require('../interceptor');
/**

@@ -25,3 +29,3 @@ * Follows the Location header in a response, if present. The response

if (response.headers && response.headers.Location) {
return (config.client || client)({
return (config.client || client.skip())({
method: 'GET',

@@ -38,6 +42,4 @@ path: response.headers.Location

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define(['../interceptor', '../mime/registry', 'when'], function (interceptor, registry, when) {
define(function (require) {
"use strict";
var interceptor, registry, when;
interceptor = require('../interceptor');
registry = require('../mime/registry');
when = require('when');
/**

@@ -68,6 +74,4 @@ * MIME type support for request and response entities. Entities are

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));

@@ -1,6 +0,13 @@

(function (global, define) {
(function (define, global) {
define(['../../rest', 'when', '../UrlBuilder', '../util/pubsub'], function (defaultClient, when, UrlBuilder, pubsub) {
define(function (require) {
"use strict";
var defaultClient, when, UrlBuilder, pubsub;
defaultClient = require('../../rest');
when = require('when');
UrlBuilder = require('../UrlBuilder');
pubsub = require('../util/pubsub');
function defaultOAuthCallback(hash) {

@@ -160,7 +167,5 @@ var params, queryString, regex, m;

}(
typeof global === 'undefined' ? this : global,
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); },
typeof global === 'undefined' ? this : global
// Boilerplate for AMD and Node
));
(function (define) {
define(['../interceptor'], function (interceptor) {
define(function (require) {
"use strict";
var interceptor;
interceptor = require('../interceptor');
function startsWith(str, prefix) {

@@ -45,6 +49,4 @@ return str.indexOf(prefix) === 0;

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define, global, process) {
// include ./type/text/plain and ./type/application/json as hints to a build tool
define(['when', 'require', './type/text/plain', './type/application/json'], function (when, require) {
define(function (require) {
"use strict";
var load, registry;
var when, load, registry;
registry = {};
when = require('when');
// include text/plain and application/json by default
registry = {
'text/plain': require('./type/text/plain'),
'application/json': require('./type/application/json')
};
/**

@@ -76,5 +82,3 @@ * Lookup the converter for a MIME type

*/
load = (
typeof require === 'function' && require.amd ? load_amd : load_node
);
load = typeof require === 'function' && require.amd ? load_amd : load_node;

@@ -89,5 +93,3 @@ return {

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(function (dep) { return dep === 'require' ? require : require(dep); }));
},
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); },
typeof global === 'undefined' ? this : global,

@@ -94,0 +96,0 @@ typeof process === 'undefined' ? undefined : process

(function (define) {
define(['../text/plain'], function (plain) {
define(function (require) {
"use strict";
//TODO: handle as HTML instead of text/plain
return plain;
return require('../text/plain');
});
}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define([], function () {
define(function (require) {
"use strict";

@@ -20,6 +20,4 @@

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define([], function () {
define(function (require) {
"use strict";

@@ -81,6 +81,4 @@

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define(['../application/html'], function (html) {
define(function (require) {
"use strict";
return html;
return require('../application/html');
});
}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define([], function () {
define(function (require) {
"use strict";

@@ -20,6 +20,4 @@

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
{
"name": "rest",
"version": "0.8.1",
"version": "0.8.2",
"description": "HTTP client library inspired by the Spring Framework's RestTemplate",

@@ -25,2 +25,8 @@ "keywords": ["rest", "http", "client", "rest-template", "spring"],

],
"contributors": [
{
"name": "Jeremy Grelle",
"web": "https://github.com/jeremyg484"
}
],
"dependencies": {

@@ -30,3 +36,3 @@ "when": "~1"

"devDependencies": {
"curl": "https://github.com/cujojs/curl/tarball/dev",
"curl": "https://github.com/cujojs/curl/tarball/0.7.2",
"dojo": "https://github.com/dojo/dojo/tarball/1.7.2",

@@ -33,0 +39,0 @@ "poly": "https://github.com/cujojs/poly/tarball/0.4",

@@ -162,2 +162,13 @@ Rest Template

0.8.2
- requests may be canceled
- timeout incerceptor that cancels the request unless it finishes before the timeout
- retry interceptor handles error respones by retrying the request after an elapsed period
- error interceptor handlers may recover from errors, a rejected promise must be returned in order to preserve the error state
- response objects, with an error property, are used for client errors instead of the thrown value
- interceptor response handlers recieve the interceptor's client rather then the next client in the chain
- interceptor request handlers may provide a response
- convert modules to UMD format; no functional impact
- replaced rest/util/base64 with an MIT licenced impl; no functional impact
0.8.1

@@ -164,0 +175,0 @@ - fixed bug where http method may be overwritten

(function (define, process) {
define(['./client/xhr'], function (client) {
define(function (require) {
"use strict";
var moduleId;
/**
* Plain JS Object containing properties that represent an HTTP request
* Plain JS Object containing properties that represent an HTTP request.
*
* Depending on the capabilities of the underlying client, a request
* may be cancelable. If a request may be canceled, the client will add
* a canceled flag and cancel function to the request object. Canceling
* the request will put the response into an error state.
*
* @field {String} [method='GET'] HTTP method, commonly GET, POST, PUT, DELETE or HEAD

@@ -14,2 +21,4 @@ * @field {String|UrlBuilder} [path=''] path template with optional path variables

* @field {*} [entity] the HTTP entity, common for POST or PUT requests
* @field {Boolean} [canceled] true if the request has been canceled, set by the client
* @field {Function} [cancel] cancels the request if invoked, provided by the client
*

@@ -42,14 +51,14 @@ * @class Request

if (process && process.versions && process.versions.node) {
return require('./client/node');
// avaid build tools
moduleId = './client/node';
return require(moduleId);
}
return client;
return require('./client/xhr');
});
}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
},
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); },
typeof process === 'undefined' ? undefined : process
// Boilerplate for AMD and Node
));

@@ -14,2 +14,3 @@ var config = exports;

environment: 'browser',
autoRun: false,
rootPath: '../',

@@ -28,4 +29,5 @@ resources: [

'test/**/*-test.js',
'test/**/*-test-browser.js'
'test/**/*-test-browser.js',
'test/run.js'
]
};
(function (buster, define) {
var jsonp, jsonpInterceptor, rest, assert, refute;
var assert, refute, fail, undef;
assert = buster.assert;
refute = buster.refute;
buster.testRunner.timeout = 500;
buster.testCase('rest/client/jsonp', {
setUp: function (done) {
if (jsonp) { return done(); }
define('rest/client/jsonp-test', ['rest/client/jsonp', 'rest/interceptor/jsonp', 'rest'], function (jpc, jpi, r) {
jsonp = jpc;
jsonpInterceptor = jpi;
rest = r;
done();
});
},
fail = function () {
buster.assertions.fail('should never be called');
};
'should make a GET by default': function (done) {
var request = { path: 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0', params: { q: 'javascript' } };
jsonp(request).then(
function (response) {
assert(response.entity.responseData);
}
).always(done);
},
'should use the jsonp client from the jsonp interceptor by default': function (done) {
var request = { path: 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0', params: { q: 'html5' } };
jsonpInterceptor()(request).then(
function (response) {
assert(response.entity.responseData);
}
).always(done);
},
'should not be the default client': function () {
refute.same(jsonp, rest);
}
define('rest/client/jsonp-test', function (require) {
var client, jsonpInterceptor, rest;
client = require('rest/client/jsonp');
jsonpInterceptor = require('rest/interceptor/jsonp');
rest = require('rest');
buster.testCase('rest/client/jsonp', {
'should make a GET by default': function (done) {
var request = { path: 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0', params: { q: 'javascript' } };
client(request).then(
function (response) {
assert(response.entity.responseData);
assert.same(request, response.request);
refute(request.canceled);
refute(response.raw.parentNode);
}
).always(done);
},
'should use the jsonp client from the jsonp interceptor by default': function (done) {
var request = { path: 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0', params: { q: 'html5' } };
jsonpInterceptor()(request).then(
function (response) {
assert(response.entity.responseData);
assert.same(request, response.request);
refute(request.canceled);
refute(response.raw.parentNode);
}
).always(done);
},
'should abort the request if canceled': function (done) {
var request = { path: 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0', params: { q: 'html5' } };
client(request).then(
fail,
function (response) {
assert.same(request, response.request);
assert(request.canceled);
refute(response.raw.parentNode);
}
).always(done);
refute(request.canceled);
request.cancel();
},
'should propogate request errors': function (done) {
var request = { path: 'http://localhost:1234' };
client(request).then(
fail,
function (response) {
assert.same('loaderror', response.error);
}
).always(done);
},
'should not make a request that has already been canceled': function (done) {
var request = { canceled: true, path: 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0', params: { q: 'javascript' } };
client(request).then(
fail,
function (response) {
assert.same(request, response.request);
assert(request.canceled);
assert.same('precanceled', response.error);
}
).always(done);
},
'should not be the default client': function () {
refute.same(client, rest);
}
});
});

@@ -43,6 +85,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../rest/client/jsonp'), require('../rest/interceptor/jsonp'), require('../rest'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));

@@ -1,94 +0,154 @@

var buster, rest, client, server, assert, refute;
(function (buster, define) {
buster = require('buster');
rest = require('../../rest');
client = require('../../client/node');
server = require('http').createServer();
assert = buster.assert;
refute = buster.refute;
var assert, refute, fail, undef;
server.on('request', function (request, response) {
var requestBody = '';
request.on('data', function (chunk) {
requestBody += chunk;
});
request.on('end', function () {
var responseBody = requestBody ? requestBody : 'hello world';
response.writeHead(200, 'OK', {
'content-length': responseBody.length,
'content-type': 'text/plain'
assert = buster.assert;
refute = buster.refute;
fail = function () {
buster.assertions.fail('should never be called');
};
define('rest/client/jsonp-test', function (require) {
var rest, client, server;
rest = require('rest');
client = require('rest/client/node');
server = require('http').createServer();
buster.testCase('rest/client/node', {
setUp: function () {
server.on('request', function (request, response) {
var requestBody = '';
request.on('data', function (chunk) {
requestBody += chunk;
});
request.on('end', function () {
var responseBody = requestBody ? requestBody : 'hello world';
response.writeHead(200, 'OK', {
'content-length': responseBody.length,
'content-type': 'text/plain'
});
response.write(responseBody);
response.end();
});
request.on('error', function () { console.log("server error"); });
});
// TODO handle port conflicts
server.listen(8080);
},
tearDown: function () {
server.close();
},
'should make a GET by default': function (done) {
var request = { path: 'http://localhost:8080/' };
client(request).then(
function (response) {
assert(response.raw);
assert.same(request, response.request);
assert.equals(response.request.method, 'GET');
assert.equals(response.entity, 'hello world');
assert.equals(response.status.code, 200);
assert.equals('text/plain', response.headers['Content-Type']);
assert.equals(response.entity.length, parseInt(response.headers['Content-Length'], 10));
refute(request.canceled);
}
).always(done);
},
'should make an explicit GET': function (done) {
var request = { path: 'http://localhost:8080/', method: 'GET' };
client(request).then(
function (response) {
assert(response.raw);
assert.same(request, response.request);
assert.equals(response.request.method, 'GET');
assert.equals(response.entity, 'hello world');
assert.equals(response.status.code, 200);
assert.equals('text/plain', response.headers['Content-Type']);
assert.equals(response.entity.length, parseInt(response.headers['Content-Length'], 10));
refute(request.canceled);
}
).always(done);
},
'should make a POST with an entity': function (done) {
var request = { path: 'http://localhost:8080/', entity: 'echo' };
client(request).then(
function (response) {
assert(response.raw);
assert.same(request, response.request);
assert.equals(response.request.method, 'POST');
assert.equals(response.entity, 'echo');
assert.equals(response.status.code, 200);
assert.equals('text/plain', response.headers['Content-Type']);
assert.equals(response.entity.length, parseInt(response.headers['Content-Length'], 10));
refute(request.canceled);
}
).always(done);
},
'should make an explicit POST with an entity': function (done) {
var request = { path: 'http://localhost:8080/', entity: 'echo', method: 'POST' };
client(request).then(
function (response) {
assert(response.raw);
assert.same(request, response.request);
assert.equals(response.request.method, 'POST');
assert.equals(response.entity, 'echo');
assert.equals(response.status.code, 200);
assert.equals('text/plain', response.headers['Content-Type']);
assert.equals(response.entity.length, parseInt(response.headers['Content-Length'], 10));
refute(request.canceled);
}
).always(done);
},
'should abort the request if canceled': function (done) {
var request = { path: 'http://localhost:8080/' };
client(request).then(
fail,
function (response) {
assert(request.canceled);
}
).always(done);
refute(request.canceled);
request.cancel();
},
'should propogate request errors': function (done) {
var request = { path: 'http://localhost:1234' };
client(request).then(
fail,
function (response) {
assert(response.error);
}
).always(done);
},
'should not make a request that has already been canceled': function (done) {
var request = { canceled: true, path: 'http://localhost:1234' };
client(request).then(
fail,
function (response) {
assert.same(request, response.request);
assert(request.canceled);
assert.same('precanceled', response.error);
}
).always(done);
},
'should be the default client': function () {
assert.same(client, rest);
}
});
response.write(responseBody);
response.end();
});
});
buster.testCase('rest/client/node', {
setUp: function () {
// TODO check that port is free
server.listen(8080);
},
tearDown: function () {
server.close();
},
'should make a GET by default': function (done) {
var request = { path: 'http://localhost:8080/' };
client(request).then(
function (response) {
assert(response.raw);
assert.same(request, response.request);
assert.equals(response.request.method, 'GET');
assert.equals(response.entity, 'hello world');
assert.equals(response.status.code, 200);
assert.equals('text/plain', response.headers['Content-Type']);
assert.equals(response.entity.length, parseInt(response.headers['Content-Length'], 10));
}
).always(done);
},
'should make an explicit GET': function (done) {
var request = { path: 'http://localhost:8080/', method: 'GET' };
client(request).then(
function (response) {
assert(response.raw);
assert.same(request, response.request);
assert.equals(response.request.method, 'GET');
assert.equals(response.entity, 'hello world');
assert.equals(response.status.code, 200);
assert.equals('text/plain', response.headers['Content-Type']);
assert.equals(response.entity.length, parseInt(response.headers['Content-Length'], 10));
}
).always(done);
},
'should make a POST with an entity': function (done) {
var request = { path: 'http://localhost:8080/', entity: 'echo' };
client(request).then(
function (response) {
assert(response.raw);
assert.same(request, response.request);
assert.equals(response.request.method, 'POST');
assert.equals(response.entity, 'echo');
assert.equals(response.status.code, 200);
assert.equals('text/plain', response.headers['Content-Type']);
assert.equals(response.entity.length, parseInt(response.headers['Content-Length'], 10));
}
).always(done);
},
'should make an explicit POST with an entity': function (done) {
var request = { path: 'http://localhost:8080/', entity: 'echo', method: 'POST' };
client(request).then(
function (response) {
assert(response.raw);
assert.same(request, response.request);
assert.equals(response.request.method, 'POST');
assert.equals(response.entity, 'echo');
assert.equals(response.status.code, 200);
assert.equals('text/plain', response.headers['Content-Type']);
assert.equals(response.entity.length, parseInt(response.headers['Content-Length'], 10));
}
).always(done);
},
'should be the default client': function () {
assert.same(client, rest);
}(
this.buster || require('buster'),
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
});
// Boilerplate for AMD and Node
));
(function (buster, define) {
var xhr, rest, assert, refute;
var assert, refute, fail, undef;

@@ -8,84 +8,129 @@ assert = buster.assert;

buster.testCase('rest/client/xhr', {
setUp: function (done) {
if (xhr) { return done(); }
define('rest/client/xhr-test', ['rest/client/xhr', 'rest'], function (x, r) {
xhr = x;
rest = r;
done();
});
},
fail = function () {
buster.assertions.fail('should never be called');
};
'should make a GET by default': function (done) {
var request = { path: '/' };
xhr(request).then(
function (response) {
var xhr, name;
xhr = response.raw;
assert.same(request, response.request);
assert.equals(response.request.method, 'GET');
assert.equals(xhr.responseText, response.entity);
assert.equals(xhr.status, response.status.code);
assert.equals(xhr.statusText, response.status.text);
for (name in response.headers) {
assert.equals(xhr.getResponseHeader(name), response.headers[name]);
define('rest/client/xhr-test', function (require) {
var client, rest;
client = require('rest/client/xhr');
rest = require('rest');
buster.testCase('rest/client/xhr', {
'should make a GET by default': function (done) {
var request = { path: '/' };
client(request).then(
function (response) {
var xhr, name;
xhr = response.raw;
assert.same(request, response.request);
assert.equals(response.request.method, 'GET');
assert.equals(xhr.responseText, response.entity);
assert.equals(xhr.status, response.status.code);
assert.equals(xhr.statusText, response.status.text);
for (name in response.headers) {
assert.equals(xhr.getResponseHeader(name), response.headers[name]);
}
refute(request.canceled);
}
}
).always(done);
},
'should make an explicit GET': function (done) {
var request = { path: '/', method: 'GET' };
xhr(request).then(
function (response) {
var xhr, name;
xhr = response.raw;
assert.same(request, response.request);
assert.equals(response.request.method, 'GET');
assert.equals(xhr.responseText, response.entity);
assert.equals(xhr.status, response.status.code);
assert.equals(xhr.statusText, response.status.text);
for (name in response.headers) {
assert.equals(xhr.getResponseHeader(name), response.headers[name]);
).always(done);
},
'should make an explicit GET': function (done) {
var request = { path: '/', method: 'GET' };
client(request).then(
function (response) {
var xhr, name;
xhr = response.raw;
assert.same(request, response.request);
assert.equals(response.request.method, 'GET');
assert.equals(xhr.responseText, response.entity);
assert.equals(xhr.status, response.status.code);
assert.equals(xhr.statusText, response.status.text);
for (name in response.headers) {
assert.equals(xhr.getResponseHeader(name), response.headers[name]);
}
refute(request.canceled);
}
}
).always(done);
},
'should make a POST with an entity': function (done) {
var request = { path: '/', entity: 'hello world' };
xhr(request).then(
function (response) {
var xhr, name;
xhr = response.raw;
assert.same(request, response.request);
assert.equals(response.request.method, 'POST');
assert.equals(xhr.responseText, response.entity);
assert.equals(xhr.status, response.status.code);
assert.equals(xhr.statusText, response.status.text);
for (name in response.headers) {
assert.equals(xhr.getResponseHeader(name), response.headers[name]);
).always(done);
},
'should make a POST with an entity': function (done) {
var request = { path: '/', entity: 'hello world' };
client(request).then(
function (response) {
var xhr, name;
xhr = response.raw;
assert.same(request, response.request);
assert.equals(response.request.method, 'POST');
assert.equals(xhr.responseText, response.entity);
assert.equals(xhr.status, response.status.code);
assert.equals(xhr.statusText, response.status.text);
for (name in response.headers) {
assert.equals(xhr.getResponseHeader(name), response.headers[name]);
}
refute(request.canceled);
}
}
).always(done);
},
'should make an explicit POST with an entity': function (done) {
var request = { path: '/', entity: 'hello world', method: 'POST' };
xhr(request).then(
function (response) {
var xhr, name;
xhr = response.raw;
assert.same(request, response.request);
assert.equals(response.request.method, 'POST');
assert.equals(xhr.responseText, response.entity);
assert.equals(xhr.status, response.status.code);
assert.equals(xhr.statusText, response.status.text);
for (name in response.headers) {
assert.equals(xhr.getResponseHeader(name), response.headers[name]);
).always(done);
},
'should make an explicit POST with an entity': function (done) {
var request = { path: '/', entity: 'hello world', method: 'POST' };
client(request).then(
function (response) {
var xhr, name;
xhr = response.raw;
assert.same(request, response.request);
assert.equals(response.request.method, 'POST');
assert.equals(xhr.responseText, response.entity);
assert.equals(xhr.status, response.status.code);
assert.equals(xhr.statusText, response.status.text);
for (name in response.headers) {
assert.equals(xhr.getResponseHeader(name), response.headers[name]);
}
refute(request.canceled);
}
}
).always(done);
},
'should be the default client': function () {
assert.same(xhr, rest);
}
).always(done);
},
'should abort the request if canceled': function (done) {
// TDOO find an endpoint that takes a bit to respond, cached files may return synchronously
var request = { path: '/wait/' + new Date().getTime() };
client(request).then(
fail,
function (response) {
assert(request.canceled);
assert.same(0, response.raw.status);
// this assertion is true in every browser except for IE 6
// assert.same(XMLHttpRequest.UNSENT || 0, response.raw.readyState);
assert(response.raw.readyState <= 3);
}
).always(done);
refute(request.canceled);
request.cancel();
},
'should propogate request errors': function (done) {
var request = { path: 'http://localhost:1234' };
client(request).then(
fail,
function (response) {
assert.same('loaderror', response.error);
}
).always(done);
},
'should not make a request that has already been canceled': function (done) {
var request = { canceled: true, path: '/' };
client(request).then(
fail,
function (response) {
assert.same(request, response.request);
assert(request.canceled);
assert.same('precanceled', response.error);
}
).always(done);
},
'should be the default client': function () {
assert.same(client, rest);
}
});
// TODO spy XmlHttpRequest
});

@@ -95,6 +140,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../client/xhr'), require('../../rest'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
));
(function (buster, define) {
var RestStore, when, client, assert, refute, undef;
var assert, refute, undef;

@@ -8,128 +8,132 @@ assert = buster.assert;

client = function (request) { return when({ request: request }); };
define('rest/dojo/RestStore-test', function (require) {
buster.testCase('rest/dojo/RestStore', {
setUp: function (done) {
if (RestStore) { return done(); }
define('rest/dojo/RestStore-test', ['rest/dojo/RestStore', 'when'], function (RS, w) {
RestStore = RS;
when = w;
done();
var RestStore, when;
RestStore = require('rest/dojo/RestStore');
when = require('when');
function client(request) {
return when({
request: request
});
},
}
'should use "id" as the default idProperty': function () {
var store = new RestStore();
assert.equals('id', store.idProperty);
assert.equals(42, store.getIdentity({ id: 42 }));
},
'should work with custom idProperty': function () {
var store = new RestStore({ idProperty: 'key'});
assert.equals('key', store.idProperty);
assert.equals(42, store.getIdentity({ key: 42 }));
},
'should apply query params to the query string': function (done) {
var store = new RestStore({ client: client });
store.query({ q: 'what is the meaning of life?' }).then(
function (response) {
assert.equals('what is the meaning of life?', response.request.params.q);
}
).always(done);
},
'should get based on the id': function (done) {
var store = new RestStore({ client: client });
store.get(42).then(
function (response) {
assert.equals('42', response.request.path);
refute(response.request.method);
}
).always(done);
},
'should remove based on the id': function (done) {
var store = new RestStore({ client: client });
store.remove(42).then(
function (response) {
assert.equals('42', response.request.path);
assert.equals('delete', response.request.method);
}
).always(done);
},
'should add a record without an ID': function (done) {
var store = new RestStore({ client: client });
store.add({ foo: 'bar' }).then(
function (response) {
assert.equals('', response.request.path);
assert.equals('post', response.request.method);
assert.equals('*', response.request.headers['If-None-Match']);
assert.equals('bar', response.request.entity.foo);
}
).always(done);
},
'should add a record with an explicit ID': function (done) {
var store = new RestStore({ client: client });
store.add({ foo: 'bar' }, { id: 42 }).then(
function (response) {
assert.equals('42', response.request.path);
assert.equals('put', response.request.method);
assert.equals('*', response.request.headers['If-None-Match']);
assert.equals('bar', response.request.entity.foo);
refute.equals('42', response.request.entity.id);
}
).always(done);
},
'should add a record with an implicit ID': function (done) {
var store = new RestStore({ client: client });
store.add({ foo: 'bar', id: 42 }).then(
function (response) {
assert.equals('42', response.request.path);
assert.equals('put', response.request.method);
assert.equals('*', response.request.headers['If-None-Match']);
assert.equals('bar', response.request.entity.foo);
assert.equals('42', response.request.entity.id);
}
).always(done);
},
'should add a record ignoring the ID': function (done) {
var store = new RestStore({ client: client, ignoreId: true });
store.add({ foo: 'bar', id: 42 }).then(
function (response) {
assert.equals('', response.request.path);
assert.equals('post', response.request.method);
assert.equals('*', response.request.headers['If-None-Match']);
assert.equals('bar', response.request.entity.foo);
assert.equals('42', response.request.entity.id);
}
).always(done);
},
'should put overwriting target': function (done) {
var store = new RestStore({ client: client });
store.put({ foo: 'bar', id: 42 }, { overwrite: true }).then(
function (response) {
assert.equals('42', response.request.path);
assert.equals('put', response.request.method);
assert.equals('*', response.request.headers['If-Match']);
}
).always(done);
},
'should put without overwriting target': function (done) {
var store = new RestStore({ client: client });
store.put({ foo: 'bar', id: 42 }, { overwrite: false }).then(
function (response) {
assert.equals('42', response.request.path);
assert.equals('put', response.request.method);
assert.equals('*', response.request.headers['If-None-Match']);
}
).always(done);
},
'should put with default config': function (done) {
var store = new RestStore({ client: client });
store.put({ foo: 'bar', id: 42 }).then(
function (response) {
assert.equals('42', response.request.path);
assert.equals('put', response.request.method);
refute(response.request.headers['If-None-Match']);
refute(response.request.headers['If-Match']);
}
).always(done);
}
buster.testCase('rest/dojo/RestStore', {
'should use "id" as the default idProperty': function () {
var store = new RestStore();
assert.equals('id', store.idProperty);
assert.equals(42, store.getIdentity({ id: 42 }));
},
'should work with custom idProperty': function () {
var store = new RestStore({ idProperty: 'key'});
assert.equals('key', store.idProperty);
assert.equals(42, store.getIdentity({ key: 42 }));
},
'should apply query params to the query string': function (done) {
var store = new RestStore({ client: client });
store.query({ q: 'what is the meaning of life?' }).then(
function (response) {
assert.equals('what is the meaning of life?', response.request.params.q);
}
).always(done);
},
'should get based on the id': function (done) {
var store = new RestStore({ client: client });
store.get(42).then(
function (response) {
assert.equals('42', response.request.path);
refute(response.request.method);
}
).always(done);
},
'should remove based on the id': function (done) {
var store = new RestStore({ client: client });
store.remove(42).then(
function (response) {
assert.equals('42', response.request.path);
assert.equals('delete', response.request.method);
}
).always(done);
},
'should add a record without an ID': function (done) {
var store = new RestStore({ client: client });
store.add({ foo: 'bar' }).then(
function (response) {
assert.equals('', response.request.path);
assert.equals('post', response.request.method);
assert.equals('*', response.request.headers['If-None-Match']);
assert.equals('bar', response.request.entity.foo);
}
).always(done);
},
'should add a record with an explicit ID': function (done) {
var store = new RestStore({ client: client });
store.add({ foo: 'bar' }, { id: 42 }).then(
function (response) {
assert.equals('42', response.request.path);
assert.equals('put', response.request.method);
assert.equals('*', response.request.headers['If-None-Match']);
assert.equals('bar', response.request.entity.foo);
refute.equals('42', response.request.entity.id);
}
).always(done);
},
'should add a record with an implicit ID': function (done) {
var store = new RestStore({ client: client });
store.add({ foo: 'bar', id: 42 }).then(
function (response) {
assert.equals('42', response.request.path);
assert.equals('put', response.request.method);
assert.equals('*', response.request.headers['If-None-Match']);
assert.equals('bar', response.request.entity.foo);
assert.equals('42', response.request.entity.id);
}
).always(done);
},
'should add a record ignoring the ID': function (done) {
var store = new RestStore({ client: client, ignoreId: true });
store.add({ foo: 'bar', id: 42 }).then(
function (response) {
assert.equals('', response.request.path);
assert.equals('post', response.request.method);
assert.equals('*', response.request.headers['If-None-Match']);
assert.equals('bar', response.request.entity.foo);
assert.equals('42', response.request.entity.id);
}
).always(done);
},
'should put overwriting target': function (done) {
var store = new RestStore({ client: client });
store.put({ foo: 'bar', id: 42 }, { overwrite: true }).then(
function (response) {
assert.equals('42', response.request.path);
assert.equals('put', response.request.method);
assert.equals('*', response.request.headers['If-Match']);
}
).always(done);
},
'should put without overwriting target': function (done) {
var store = new RestStore({ client: client });
store.put({ foo: 'bar', id: 42 }, { overwrite: false }).then(
function (response) {
assert.equals('42', response.request.path);
assert.equals('put', response.request.method);
assert.equals('*', response.request.headers['If-None-Match']);
}
).always(done);
},
'should put with default config': function (done) {
var store = new RestStore({ client: client });
store.put({ foo: 'bar', id: 42 }).then(
function (response) {
assert.equals('42', response.request.path);
assert.equals('put', response.request.method);
refute(response.request.headers['If-None-Match']);
refute(response.request.headers['If-Match']);
}
).always(done);
}
});
});

@@ -139,6 +143,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../rest/dojo/RestStore'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var resourcePlugin, RestStore, wire, when, assert, refute, undef;
var assert, refute, undef;

@@ -8,66 +8,73 @@ assert = buster.assert;

function client(request) {
return when({ request: request, status: { code: 200 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' });
}
define('rest/dojo/wire-test', function (require) {
buster.testCase('rest/dojo/wire', {
setUp: function (done) {
if (resourcePlugin) { return done(); }
define('rest/dojo/wire-test', ['rest/dojo/wire', 'rest/dojo/RestStore', 'wire', 'when'], function (rp, RS, wi, wh) {
resourcePlugin = rp;
RestStore = RS;
wire = wi;
when = wh;
done();
var resourcePlugin, RestStore, wire, when;
resourcePlugin = require('rest/dojo/wire');
RestStore = require('rest/dojo/RestStore');
wire = require('wire');
when = require('when');
function client(request) {
return when({
request: request,
status: { code: 200 },
headers: {
'Content-Type': 'application/json'
},
entity: '{"foo":"bar"}'
});
},
}
'should create a RestStore for resource!': function (done) {
var spec;
spec = {
store: { $ref: 'resource!', client: client },
plugins: [{ module: 'rest/dojo/wire' }]
};
wire(spec).then(function (spec) {
assert(spec.store instanceof RestStore);
}).always(done);
},
'should get with resource! returning a promise': function (done) {
var spec;
spec = {
resource: { $ref: 'resource!test/dojo', get: 'hello.json', entity: false, client: client },
plugins: [{ module: 'rest/dojo/wire' }]
};
wire(spec).then(function (spec) {
spec.resource.then(function (resource) {
assert.equals('bar', resource.entity.foo);
assert.equals('test/dojo/hello.json', resource.request.path);
});
}).always(done);
},
'should get with resource! returning data': function (done) {
var spec;
spec = {
resource: { $ref: 'resource!test/dojo', get: 'hello.json', entity: false, wait: true, client: client },
plugins: [{ module: 'rest/dojo/wire' }]
};
wire(spec).then(function (spec) {
assert.equals('bar', spec.resource.entity.foo);
assert.equals('test/dojo/hello.json', spec.resource.request.path);
}).always(done);
},
'should support client!': function (done) {
var spec;
spec = {
client: { $ref: 'client!', client: client },
plugins: [{ module: 'rest/dojo/wire' }]
};
wire(spec).then(function (spec) {
spec.client({}).then(
function (response) {
assert.equals('bar', response.foo);
}
);
}).always(done);
}
buster.testCase('rest/dojo/wire', {
'should create a RestStore for resource!': function (done) {
var spec;
spec = {
store: { $ref: 'resource!', client: client },
plugins: [{ module: 'rest/dojo/wire' }]
};
wire(spec).then(function (spec) {
assert(spec.store instanceof RestStore);
}).always(done);
},
'should get with resource! returning a promise': function (done) {
var spec;
spec = {
resource: { $ref: 'resource!test/dojo', get: 'hello.json', entity: false, client: client },
plugins: [{ module: 'rest/dojo/wire' }]
};
wire(spec).then(function (spec) {
spec.resource.then(function (resource) {
assert.equals('bar', resource.entity.foo);
assert.equals('test/dojo/hello.json', resource.request.path);
});
}).always(done);
},
'should get with resource! returning data': function (done) {
var spec;
spec = {
resource: { $ref: 'resource!test/dojo', get: 'hello.json', entity: false, wait: true, client: client },
plugins: [{ module: 'rest/dojo/wire' }]
};
wire(spec).then(function (spec) {
assert.equals('bar', spec.resource.entity.foo);
assert.equals('test/dojo/hello.json', spec.resource.request.path);
}).always(done);
},
'should support client!': function (done) {
var spec;
spec = {
client: { $ref: 'client!', client: client },
plugins: [{ module: 'rest/dojo/wire' }]
};
wire(spec).then(function (spec) {
spec.client({}).then(
function (response) {
assert.equals('bar', response.foo);
}
);
}).always(done);
}
});
});

@@ -77,6 +84,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('./rest/dojo/wire'), require('wire'), require('when'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var basicAuth, rest, assert, refute;
var assert, refute, undef;

@@ -8,40 +8,40 @@ assert = buster.assertions.assert;

buster.testCase('rest/interceptor/basicAuth', {
setUp: function (done) {
if (basicAuth) { return done(); }
define('rest/interceptor/basicAuth-test', ['rest/interceptor/basicAuth', 'rest'], function (ba, r) {
basicAuth = ba;
rest = r;
done();
});
},
define('rest/interceptor/basicAuth-test', function (require) {
'should authenticate the requst from the config': function (done) {
var client = basicAuth(
function (request) { return { request: request }; },
{ username: 'user', password: 'pass'}
);
client({}).then(function (response) {
assert.equals('dXNlcjpwYXNz', response.request.headers.Authorization);
}).always(done);
},
'should authenticate the requst from the request': function (done) {
var client = basicAuth(
function (request) { return { request: request }; }
);
client({ username: 'user', password: 'pass'}).then(function (response) {
assert.equals('dXNlcjpwYXNz', response.request.headers.Authorization);
}).always(done);
},
'should not authenticate without a username': function (done) {
var client = basicAuth(
function (request) { return { request: request }; }
);
client({}).then(function (response) {
refute.defined(response.request.headers.Authorization);
}).always(done);
},
'should have the default client as the parent by default': function () {
assert.same(rest, basicAuth().skip());
}
var basicAuth, rest;
basicAuth = require('rest/interceptor/basicAuth');
rest = require('rest');
buster.testCase('rest/interceptor/basicAuth', {
'should authenticate the requst from the config': function (done) {
var client = basicAuth(
function (request) { return { request: request }; },
{ username: 'user', password: 'pass'}
);
client({}).then(function (response) {
assert.equals('dXNlcjpwYXNz', response.request.headers.Authorization);
}).always(done);
},
'should authenticate the requst from the request': function (done) {
var client = basicAuth(
function (request) { return { request: request }; }
);
client({ username: 'user', password: 'pass'}).then(function (response) {
assert.equals('dXNlcjpwYXNz', response.request.headers.Authorization);
}).always(done);
},
'should not authenticate without a username': function (done) {
var client = basicAuth(
function (request) { return { request: request }; }
);
client({}).then(function (response) {
refute.defined(response.request.headers.Authorization);
}).always(done);
},
'should have the default client as the parent by default': function () {
assert.same(rest, basicAuth().skip());
}
});
});

@@ -51,6 +51,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../interceptor/basicAuth'), require('../../rest'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var entity, rest, assert, refute;
var assert, refute, undef;

@@ -8,35 +8,35 @@ assert = buster.assertions.assert;

buster.testCase('rest/interceptor/entity', {
setUp: function (done) {
if (entity) { return done(); }
define('rest/interceptor/entity-test', ['rest/interceptor/entity', 'rest'], function (e, r) {
entity = e;
rest = r;
done();
});
},
define('rest/interceptor/entity-test', function (require) {
'should return the response entity': function (done) {
var client, body;
var entity, rest;
body = {};
client = entity(function () { return { entity: body }; });
entity = require('rest/interceptor/entity');
rest = require('rest');
client().then(function (response) {
assert.same(body, response);
}).always(done);
},
'should return the whole response if there is no entity': function (done) {
var client, response;
buster.testCase('rest/interceptor/entity', {
'should return the response entity': function (done) {
var client, body;
response = {};
client = entity(function () { return response; });
body = {};
client = entity(function () { return { entity: body }; });
client().then(function (r) {
assert.same(response, r);
}).always(done);
},
'should have the default client as the parent by default': function () {
assert.same(rest, entity().skip());
}
client().then(function (response) {
assert.same(body, response);
}).always(done);
},
'should return the whole response if there is no entity': function (done) {
var client, response;
response = {};
client = entity(function () { return response; });
client().then(function (r) {
assert.same(response, r);
}).always(done);
},
'should have the default client as the parent by default': function () {
assert.same(rest, entity().skip());
}
});
});

@@ -46,6 +46,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../interceptor/entity'), require('../../rest'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
));
(function (buster, define) {
var errorCode, rest, assert, refute;
var assert, refute;

@@ -8,48 +8,48 @@ assert = buster.assertions.assert;

buster.testCase('rest/interceptor/errorCode', {
setUp: function (done) {
if (errorCode) { return done(); }
define('rest/interceptor/errorCode-test', ['rest/interceptor/errorCode', 'rest'], function (ec, r) {
errorCode = ec;
rest = r;
done();
});
},
define('rest/interceptor/errorCode-test', function (require) {
'should resolve for less than 400 by default': function (done) {
var client = errorCode(
function () { return { status: { code: 399 } }; }
);
client({}).then(
function (response) {
assert.equals(399, response.status.code);
}
).always(done);
},
'should reject for 400 or greater by default': function (done) {
var client = errorCode(
function () { return { status: { code: 400 } }; }
);
client({}).then(
undefined,
function (response) {
assert.equals(400, response.status.code);
}
).always(done);
},
'should reject lower then 400 with a custom code': function (done) {
var client = errorCode(
function () { return { status: { code: 300 } }; },
{ code: 300 }
);
client({}).then(
undefined,
function (response) {
assert.equals(300, response.status.code);
}
).always(done);
},
'should have the default client as the parent by default': function () {
assert.same(rest, errorCode().skip());
}
var errorCode, rest;
errorCode = require('rest/interceptor/errorCode');
rest = require('rest');
buster.testCase('rest/interceptor/errorCode', {
'should resolve for less than 400 by default': function (done) {
var client = errorCode(
function () { return { status: { code: 399 } }; }
);
client({}).then(
function (response) {
assert.equals(399, response.status.code);
}
).always(done);
},
'should reject for 400 or greater by default': function (done) {
var client = errorCode(
function () { return { status: { code: 400 } }; }
);
client({}).then(
undefined,
function (response) {
assert.equals(400, response.status.code);
}
).always(done);
},
'should reject lower then 400 with a custom code': function (done) {
var client = errorCode(
function () { return { status: { code: 300 } }; },
{ code: 300 }
);
client({}).then(
undefined,
function (response) {
assert.equals(300, response.status.code);
}
).always(done);
},
'should have the default client as the parent by default': function () {
assert.same(rest, errorCode().skip());
}
});
});

@@ -59,6 +59,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../interceptor/errorCode'), require('../../rest'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var hateoas, rest, when, assert, refute;
var assert, refute, undef;

@@ -8,90 +8,91 @@ assert = buster.assertions.assert;

buster.testCase('rest/interceptor/hateoas', {
requiresSupportFor: {
'Object.defineProperty': function () {
try {
var obj = {};
Object.defineProperty(obj, 'test', { enumerable: false, configurable: true, value: true });
return obj.test;
}
catch (e) {
return false;
}
}
},
setUp: function (done) {
if (hateoas) { return done(); }
define('rest/interceptor/hateoas-test', ['rest/interceptor/hateoas', 'rest', 'when'], function (h, r, w) {
hateoas = h;
rest = r;
when = w;
done();
});
},
define('rest/interceptor/hateoas-test', function (require) {
'should parse links in the entity': function (done) {
var client, body, parent, self;
var hateoas, rest, when;
parent = { rel: 'parent', href: '/' };
self = { rel: 'self', href: '/resource' };
hateoas = require('rest/interceptor/hateoas');
rest = require('rest');
when = require('when');
body = { links: [ parent, self ]};
client = hateoas(function () { return { entity: body }; });
buster.testCase('rest/interceptor/hateoas', {
requiresSupportFor: {
'Object.defineProperty': function () {
try {
var obj = {};
Object.defineProperty(obj, 'test', { enumerable: false, configurable: true, value: true });
return obj.test;
}
catch (e) {
return false;
}
}
},
client().then(function (response) {
assert.same(parent, response.entity._links.parentLink);
assert.same(self, response.entity._links.selfLink);
}).always(done);
},
'should parse links in the entity into the entity': function (done) {
var client, body, parent, self;
'should parse links in the entity': function (done) {
var client, body, parent, self;
parent = { rel: 'parent', href: '/' };
self = { rel: 'self', href: '/resource' };
parent = { rel: 'parent', href: '/' };
self = { rel: 'self', href: '/resource' };
body = { links: [ parent, self ]};
client = hateoas(function () { return { entity: body }; }, { target: '' });
body = { links: [ parent, self ]};
client = hateoas(function () { return { entity: body }; });
client().then(function (response) {
assert.same(parent, response.entity.parentLink);
assert.same(self, response.entity.selfLink);
}).always(done);
},
'should create a client for the related resource': function (done) {
var client, body, parent, self;
client().then(function (response) {
assert.same(parent, response.entity._links.parentLink);
assert.same(self, response.entity._links.selfLink);
}).always(done);
},
'should parse links in the entity into the entity': function (done) {
var client, body, parent, self;
parent = { rel: 'parent', href: '/' };
self = { rel: 'self', href: '/resource' };
parent = { rel: 'parent', href: '/' };
self = { rel: 'self', href: '/resource' };
body = { links: [ parent, self ]};
client = hateoas(function () { return { entity: body }; });
body = { links: [ parent, self ]};
client = hateoas(function () { return { entity: body }; }, { target: '' });
client().then(function (response) {
var parentClient = response.entity._links.clientFor('parent', function (request) { return { request: request }; });
parentClient().then(function (response) {
assert.same(parent.href, response.request.path);
client().then(function (response) {
assert.same(parent, response.entity.parentLink);
assert.same(self, response.entity.selfLink);
}).always(done);
});
},
'should fetch a related resource': function (done) {
// NOTE this functionality requires a native implementation of Object.defineProperty
var client, parentClient, body, parent, self;
},
'should create a client for the related resource': function (done) {
var client, body, parent, self;
parent = { rel: 'parent', href: '/' };
self = { rel: 'self', href: '/resource' };
parent = { rel: 'parent', href: '/' };
self = { rel: 'self', href: '/resource' };
body = { links: [ parent, self ]};
parentClient = function (request) { return { request: request, entity: { links: [ parent, self ] } }; };
client = hateoas(function () { return { entity: body }; }, { client: parentClient });
body = { links: [ parent, self ]};
client = hateoas(function () { return { entity: body }; });
client().then(function (response) {
when(response.entity._links.parent, function (response) {
assert.same(parent.href, response.request.path);
assert.same(self, response.entity._links.selfLink);
}).always(done);
});
},
'should have the default client as the parent by default': function () {
assert.same(rest, hateoas().skip());
}
client().then(function (response) {
var parentClient = response.entity._links.clientFor('parent', function (request) { return { request: request }; });
parentClient().then(function (response) {
assert.same(parent.href, response.request.path);
}).always(done);
});
},
'should fetch a related resource': function (done) {
// NOTE this functionality requires a native implementation of Object.defineProperty
var client, parentClient, body, parent, self;
parent = { rel: 'parent', href: '/' };
self = { rel: 'self', href: '/resource' };
body = { links: [ parent, self ]};
parentClient = function (request) { return { request: request, entity: { links: [ parent, self ] } }; };
client = hateoas(function () { return { entity: body }; }, { client: parentClient });
client().then(function (response) {
when(response.entity._links.parent, function (response) {
assert.same(parent.href, response.request.path);
assert.same(self, response.entity._links.selfLink);
}).always(done);
});
},
'should have the default client as the parent by default': function () {
assert.same(rest, hateoas().skip());
}
});
});

@@ -101,6 +102,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../interceptor/hateoas'), require('../../rest'), require('when'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
));
(function (buster, define) {
var jsonp, rest, jsonpClient, when, assert, refute;
var assert, refute, undef;

@@ -8,42 +8,42 @@ assert = buster.assertions.assert;

buster.testCase('rest/interceptor/jsonp', {
setUp: function (done) {
if (jsonp) { return done(); }
define('rest/interceptor/jsonp-test', ['rest/interceptor/jsonp', 'rest/client/jsonp', 'rest', 'when'], function (jpi, jpc, r, w) {
jsonp = jpi;
jsonpClient = jpc;
rest = r;
when = w;
done();
});
},
define('rest/interceptor/jsonp-test', function (require) {
'should include callback info from config in request by default': function (done) {
var client = jsonp(
function (request) { return when({ request: request }); },
{ callback: { param: 'callback', prefix: 'jsonp' } }
);
client({}).then(
function (response) {
assert.equals('callback', response.request.callback.param);
assert.equals('jsonp', response.request.callback.prefix);
}
).always(done);
},
'should include callback info from request overridding config values': function (done) {
var client = jsonp(
function (request) { return when({ request: request }); },
{ callback: { param: 'callback', prefix: 'jsonp' } }
);
client({ callback: { param: 'customCallback', prefix: 'customPrefix' } }).then(
function (response) {
assert.equals('customCallback', response.request.callback.param);
assert.equals('customPrefix', response.request.callback.prefix);
}
).always(done);
},
'should have the jsonp client as the parent by default': function () {
refute.same(rest, jsonp().skip());
assert.same(jsonpClient, jsonp().skip());
}
var jsonp, rest, jsonpClient, when;
jsonp = require('rest/interceptor/jsonp');
jsonpClient = require('rest/client/jsonp');
rest = require('rest');
when = require('when');
buster.testCase('rest/interceptor/jsonp', {
'should include callback info from config in request by default': function (done) {
var client = jsonp(
function (request) { return when({ request: request }); },
{ callback: { param: 'callback', prefix: 'jsonp' } }
);
client({}).then(
function (response) {
assert.equals('callback', response.request.callback.param);
assert.equals('jsonp', response.request.callback.prefix);
}
).always(done);
},
'should include callback info from request overridding config values': function (done) {
var client = jsonp(
function (request) { return when({ request: request }); },
{ callback: { param: 'callback', prefix: 'jsonp' } }
);
client({ callback: { param: 'customCallback', prefix: 'customPrefix' } }).then(
function (response) {
assert.equals('customCallback', response.request.callback.param);
assert.equals('customPrefix', response.request.callback.prefix);
}
).always(done);
},
'should have the jsonp client as the parent by default': function () {
refute.same(rest, jsonp().skip());
assert.same(jsonpClient, jsonp().skip());
}
});
});

@@ -53,6 +53,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../interceptor/jsonp'), require('../../client/jsonp'), require('../../rest'), require('when'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var location, rest, assert, refute;
var assert, refute, undef;

@@ -8,34 +8,34 @@ assert = buster.assertions.assert;

buster.testCase('rest/interceptor/location', {
setUp: function (done) {
if (location) { return done(); }
define('rest/interceptor/location-test', ['rest/interceptor/location', 'rest'], function (l, r) {
location = l;
rest = r;
done();
});
},
define('rest/interceptor/location-test', function (require) {
'should follow the location header, once': function (done) {
var client, spy;
spy = this.spy(function (request) { return { headers: { Location: '/foo/' + spy.callCount } }; });
client = location(spy);
client({}).then(
function (response) {
assert.equals('/foo/2', response.headers.Location);
assert.same(2, spy.callCount);
}
).always(done);
},
'should return the response if there is no location header': function (done) {
var client, spy;
spy = this.spy(function () { return { status: { code: 200 } }; });
client = location(spy);
client({}).then(
function (response) {
assert.equals(200, response.status.code);
assert.same(1, spy.callCount);
}
).always(done);
}
var location, rest;
location = require('rest/interceptor/location');
rest = require('rest');
buster.testCase('rest/interceptor/location', {
'should follow the location header, once': function (done) {
var client, spy;
spy = this.spy(function (request) { return { headers: { Location: '/foo/' + spy.callCount } }; });
client = location(spy);
client({}).then(
function (response) {
assert.equals('/foo/2', response.headers.Location);
assert.same(2, spy.callCount);
}
).always(done);
},
'should return the response if there is no location header': function (done) {
var client, spy;
spy = this.spy(function () { return { status: { code: 200 } }; });
client = location(spy);
client({}).then(
function (response) {
assert.equals(200, response.status.code);
assert.same(1, spy.callCount);
}
).always(done);
}
});
});

@@ -45,6 +45,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../interceptor/location'), require('../../rest'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var mime, rest, assert, refute;
var assert, refute, undef;

@@ -8,72 +8,72 @@ assert = buster.assertions.assert;

buster.testCase('rest/interceptor/mime', {
setUp: function (done) {
if (mime) { return done(); }
define('rest/interceptor/mime-test', ['rest/interceptor/mime', 'rest'], function (m, r) {
mime = m;
rest = r;
done();
});
},
define('rest/interceptor/mime-test', function (require) {
'should return the response entity decoded': function (done) {
var client;
var mime, rest;
client = mime(function () {
return { entity: '{}', headers: { 'Content-Type': 'application/json' } };
});
mime = require('rest/interceptor/mime');
rest = require('rest');
client({}).then(function (response) {
assert.equals({}, response.entity);
}).always(done);
},
'should encode the request entity': function (done) {
var client;
buster.testCase('rest/interceptor/mime', {
'should return the response entity decoded': function (done) {
var client;
client = mime(
function (request) {
return { request: request, headers: {} };
},
{ mime: 'application/json' }
);
client = mime(function () {
return { entity: '{}', headers: { 'Content-Type': 'application/json' } };
});
client({ entity: {} }).then(function (response) {
assert.equals('{}', response.request.entity);
}).always(done);
},
'should encode the request entity from the Content-Type of the request, ignoring the filter config': function (done) {
var client;
client({}).then(function (response) {
assert.equals({}, response.entity);
}).always(done);
},
'should encode the request entity': function (done) {
var client;
client = mime(
function (request) {
return { request: request, headers: {} };
},
{ mime: 'text/plain' }
);
client = mime(
function (request) {
return { request: request, headers: {} };
},
{ mime: 'application/json' }
);
client({ entity: {}, headers: { 'Content-Type': 'application/json' } }).then(function (response) {
assert.equals('{}', response.request.entity);
assert.equals('application/json', response.request.headers['Content-Type']);
assert.equals(0, response.request.headers.Accept.indexOf('application/json'));
}).always(done);
},
'should not overwrite the requests Accept header': function (done) {
var client;
client({ entity: {} }).then(function (response) {
assert.equals('{}', response.request.entity);
}).always(done);
},
'should encode the request entity from the Content-Type of the request, ignoring the filter config': function (done) {
var client;
client = mime(
function (request) {
return { request: request, headers: {} };
},
{ mime: 'application/json' }
);
client = mime(
function (request) {
return { request: request, headers: {} };
},
{ mime: 'text/plain' }
);
client({ entity: {}, headers: { Accept: 'foo' } }).then(function (response) {
assert.equals('{}', response.request.entity);
assert.equals('application/json', response.request.headers['Content-Type']);
assert.equals('foo', response.request.headers.Accept);
}).always(done);
},
'should have the default client as the parent by default': function () {
assert.same(rest, mime().skip());
}
client({ entity: {}, headers: { 'Content-Type': 'application/json' } }).then(function (response) {
assert.equals('{}', response.request.entity);
assert.equals('application/json', response.request.headers['Content-Type']);
assert.equals(0, response.request.headers.Accept.indexOf('application/json'));
}).always(done);
},
'should not overwrite the requests Accept header': function (done) {
var client;
client = mime(
function (request) {
return { request: request, headers: {} };
},
{ mime: 'application/json' }
);
client({ entity: {}, headers: { Accept: 'foo' } }).then(function (response) {
assert.equals('{}', response.request.entity);
assert.equals('application/json', response.request.headers['Content-Type']);
assert.equals('foo', response.request.headers.Accept);
}).always(done);
},
'should have the default client as the parent by default': function () {
assert.same(rest, mime().skip());
}
});
});

@@ -83,6 +83,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../interceptor/mime'), require('../../rest'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));

@@ -1,4 +0,4 @@

(function (global, buster, define) {
(function (buster, define, global) {
var oAuth, rest, pubsub, assert, refute;
var assert, refute, undef;

@@ -8,71 +8,75 @@ assert = buster.assertions.assert;

buster.testCase('rest/interceptor/oAuth', {
setUp: function (done) {
if (oAuth) { return done(); }
define('rest/interceptor/oAuth-test', ['rest/interceptor/oAuth', 'rest/util/pubsub', 'rest'], function (oa, ps, r) {
oAuth = oa;
pubsub = ps;
rest = r;
done();
});
},
define('rest/interceptor/oAuth-test', function (require) {
'should authenticate the request for a known token': function (done) {
var client;
var oAuth, rest, pubsub;
client = oAuth(
function (request) { return { request: request, status: { code: 200 } }; },
{ token: 'bearer abcxyz' }
);
oAuth = require('rest/interceptor/oAuth');
pubsub = require('rest/util/pubsub');
rest = require('rest');
client({}).then(function (response) {
assert.equals('bearer abcxyz', response.request.headers.Authorization);
}).always(done);
},
'should use implicit flow to authenticate the request': function (done) {
var client, windowStrategy, windowStrategyClose, oAuthCallbackName;
buster.testCase('rest/interceptor/oAuth', {
'should authenticate the request for a known token': function (done) {
var client;
oAuthCallbackName = 'oAuthCallback' + Math.round(Math.random() * 100000);
windowStrategyClose = this.spy(function () {});
windowStrategy = function (url) {
var state;
assert(url.indexOf('https://www.example.com/auth?response_type=token&redirect_uri=http%3A%2F%2Flocalhost%2FimplicitHandler&client_id=user&scope=openid&state=') === 0);
state = url.substring(url.lastIndexOf('=') + 1);
setTimeout(function () {
global[oAuthCallbackName]('#state=' + state + '&=token_type=bearer&access_token=abcxyz');
}, 10);
return windowStrategyClose;
};
client = oAuth(
function (request) { return { request: request, status: { code: 200 } }; },
{ token: 'bearer abcxyz' }
);
client = oAuth(
function (request) {
return { request: request, status: { code: 200 } };
},
{
clientId: 'user',
authorizationUrlBase: 'https://www.example.com/auth',
redirectUrl: 'http://localhost/implicitHandler',
scope: 'openid',
windowStrategy: windowStrategy,
oAuthCallbackName: oAuthCallbackName
}
);
client({}).then(function (response) {
assert.equals('bearer abcxyz', response.request.headers.Authorization);
}).always(done);
},
'should use implicit flow to authenticate the request': function (done) {
var client, windowStrategy, windowStrategyClose, oAuthCallbackName;
client({}).then(function (response) {
assert.equals('bearer abcxyz', response.request.headers.Authorization);
assert.called(windowStrategyClose);
}).always(done);
},
'should have the default client as the parent by default': function () {
assert.same(rest, oAuth({ token: 'bearer abcxyz' }).skip());
}
oAuthCallbackName = 'oAuthCallback' + Math.round(Math.random() * 100000);
windowStrategyClose = this.spy(function () {});
windowStrategy = function (url) {
var state;
assert(url.indexOf('https://www.example.com/auth?response_type=token&redirect_uri=http%3A%2F%2Flocalhost%2FimplicitHandler&client_id=user&scope=openid&state=') === 0);
state = url.substring(url.lastIndexOf('=') + 1);
setTimeout(function () {
global[oAuthCallbackName]('#state=' + state + '&=token_type=bearer&access_token=abcxyz');
}, 10);
return windowStrategyClose;
};
client = oAuth(
function (request) {
return { request: request, status: { code: 200 } };
},
{
clientId: 'user',
authorizationUrlBase: 'https://www.example.com/auth',
redirectUrl: 'http://localhost/implicitHandler',
scope: 'openid',
windowStrategy: windowStrategy,
oAuthCallbackName: oAuthCallbackName
}
);
client({}).then(function (response) {
assert.equals('bearer abcxyz', response.request.headers.Authorization);
assert.called(windowStrategyClose);
}).always(done);
},
'should have the default client as the parent by default': function () {
assert.same(rest, oAuth({ token: 'bearer abcxyz' }).skip());
}
});
});
}(
typeof global === 'undefined' ? this : global,
this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../interceptor/oAuth'), require('../../util/pubsub'), require('../../rest'));
}
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
},
typeof global === 'undefined' ? this : global
// Boilerplate for AMD and Node
));
));
(function (buster, define) {
var pathPrefix, rest, assert, refute;
var assert, refute, undef;

@@ -8,42 +8,42 @@ assert = buster.assertions.assert;

buster.testCase('rest/interceptor/pathPrefix', {
setUp: function (done) {
if (pathPrefix) { return done(); }
define('rest/interceptor/pathPrefix-test', ['rest/interceptor/pathPrefix', 'rest'], function (pp, r) {
pathPrefix = pp;
rest = r;
done();
});
},
define('rest/interceptor/pathPrefix-test', function (require) {
'should prepend prefix before path': function (done) {
var client = pathPrefix(
function (request) { return { request: request }; },
{ prefix: '/foo' }
);
client({ path: '/bar' }).then(function (response) {
assert.equals('/foo/bar', response.request.path);
}).always(done);
},
'should prepend prefix before path, adding slash between path segments': function (done) {
var client = pathPrefix(
function (request) { return { request: request }; },
{ prefix: '/foo' }
);
client({ path: 'bar' }).then(function (response) {
assert.equals('/foo/bar', response.request.path);
}).always(done);
},
'should prepend prefix before path, not adding extra slash between path segments': function (done) {
var client = pathPrefix(
function (request) { return { request: request }; },
{ prefix: '/foo/' }
);
client({ path: 'bar' }).then(function (response) {
assert.equals('/foo/bar', response.request.path);
}).always(done);
},
'should have the default client as the parent by default': function () {
assert.same(rest, pathPrefix().skip());
}
var pathPrefix, rest;
pathPrefix = require('rest/interceptor/pathPrefix');
rest = require('rest');
buster.testCase('rest/interceptor/pathPrefix', {
'should prepend prefix before path': function (done) {
var client = pathPrefix(
function (request) { return { request: request }; },
{ prefix: '/foo' }
);
client({ path: '/bar' }).then(function (response) {
assert.equals('/foo/bar', response.request.path);
}).always(done);
},
'should prepend prefix before path, adding slash between path segments': function (done) {
var client = pathPrefix(
function (request) { return { request: request }; },
{ prefix: '/foo' }
);
client({ path: 'bar' }).then(function (response) {
assert.equals('/foo/bar', response.request.path);
}).always(done);
},
'should prepend prefix before path, not adding extra slash between path segments': function (done) {
var client = pathPrefix(
function (request) { return { request: request }; },
{ prefix: '/foo/' }
);
client({ path: 'bar' }).then(function (response) {
assert.equals('/foo/bar', response.request.path);
}).always(done);
},
'should have the default client as the parent by default': function () {
assert.same(rest, pathPrefix().skip());
}
});
});

@@ -53,6 +53,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../interceptor/pathPrefix'), require('../../rest'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var registry, when, assert, refute;
var assert, refute, undef;

@@ -8,40 +8,40 @@ assert = buster.assertions.assert;

buster.testCase('rest/mime/registry', {
setUp: function (done) {
if (registry) { return done(); }
define('rest/mime/registry-test', ['rest/mime/registry', 'when'], function (r, w) {
registry = r;
when = w;
done();
});
},
define('rest/mime/registry-test', function (require) {
'should discover unregisted serializers': function (done) {
when(
registry.lookup('text/plain'),
function (serializer) {
assert.isFunction(serializer.read);
assert.isFunction(serializer.write);
}
).always(done);
},
'should return registed serializer': function (done) {
var serializer = {};
registry.register('application/vnd.com.foo', serializer);
when(
registry.lookup('application/vnd.com.foo'),
function (s) {
assert.same(serializer, s);
}
).always(done);
},
'should reject for non-existant serializer': function (done) {
when(
registry.lookup('application/bogus'),
undefined,
function () {
assert(true);
}
).always(done);
}
var registry, when;
registry = require('rest/mime/registry');
when = require('when');
buster.testCase('rest/mime/registry', {
'should discover unregisted serializers': function (done) {
when(
registry.lookup('text/plain'),
function (serializer) {
assert.isFunction(serializer.read);
assert.isFunction(serializer.write);
}
).always(done);
},
'should return registed serializer': function (done) {
var serializer = {};
registry.register('application/vnd.com.foo', serializer);
when(
registry.lookup('application/vnd.com.foo'),
function (s) {
assert.same(serializer, s);
}
).always(done);
},
'should reject for non-existant serializer': function (done) {
when(
registry.lookup('application/bogus'),
undefined,
function () {
assert(true);
}
).always(done);
}
});
});

@@ -51,6 +51,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../mime/registry'), require('when'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var json, assert, refute;
var assert, refute, undef;

@@ -8,17 +8,15 @@ assert = buster.assert;

buster.testCase('rest/mime/type/application/json', {
setUp: function (done) {
if (json) { return done(); }
define('rest/mime/type/application/json-test', ['rest/mime/type/application/json'], function (j) {
json = j;
done();
});
},
define('rest/mime/type/application/json-test', function (require) {
'should read json': function () {
assert.equals({ foo: 'bar' }, json.read('{"foo":"bar"}'));
},
'should stringify json': function () {
assert.equals('{"foo":"bar"}', json.write({ foo: 'bar' }));
}
var json = require('rest/mime/type/application/json');
buster.testCase('rest/mime/type/application/json', {
'should read json': function () {
assert.equals({ foo: 'bar' }, json.read('{"foo":"bar"}'));
},
'should stringify json': function () {
assert.equals('{"foo":"bar"}', json.write({ foo: 'bar' }));
}
});
});

@@ -28,6 +26,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../../../mime/type/application/json'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
));
(function (buster, define) {
var encodeder, assert, refute;
var assert, refute, undef;

@@ -8,51 +8,47 @@ assert = buster.assert;

buster.testCase('rest/mime/type/application/x-www-form-urlencoded', {
setUp: function (done) {
if (encodeder) { return done(); }
define('rest/mime/type/application/x-www-form-urlencoded-test', ['rest/mime/type/application/x-www-form-urlencoded'], function (e) {
encodeder = e;
done();
});
},
define('rest/mime/type/application/x-www-form-urlencoded-test', function (require) {
'should place an eqauls sign between value pairs': function () {
assert.equals('foo=bar&bleep=bloop', encodeder.write({ foo: 'bar', bleep: 'bloop' }));
},
'should treat array as multiple values with the same name': function () {
assert.equals('foo=bar&foo=bloop', encodeder.write({ foo: [ 'bar', 'bloop'] }));
},
'should url encode names and values': function () {
assert.equals('fo%3Do=b%26ar', encodeder.write({ 'fo=o': 'b&ar' }));
},
'should encode spaces as plus': function () {
assert.equals('fo+o=b+ar', encodeder.write({ 'fo o': 'b ar' }));
},
'should not include an equals if their is no value': function () {
assert.equals('foo', encodeder.write({ 'foo': undefined }));
assert.equals('foo', encodeder.write({ 'foo': null }));
assert.equals('foo=', encodeder.write({ 'foo': '' }));
},
var encodeder = require('rest/mime/type/application/x-www-form-urlencoded');
'should parse an eqauls sign between value pairs': function () {
var obj = encodeder.read('foo=bar&bleep=bloop');
assert.equals('bar', obj.foo);
assert.equals('bloop', obj.bleep);
},
'should parse multiple values with the same name as an array': function () {
var obj = encodeder.read('foo=bar&foo=bloop');
assert.equals('bar', obj.foo[0]);
assert.equals('bloop', obj.foo[1]);
},
'should url decode names and values': function () {
var obj = encodeder.read('fo%3Do=b%26ar');
assert.equals('b&ar', obj['fo=o']);
},
'should decode a plus as a space': function () {
var obj = encodeder.read('fo+o=b+ar');
assert.equals('b ar', obj['fo o']);
},
'should parse missing value as null': function () {
var obj = encodeder.read('foo');
assert.same(null, obj.foo);
}
buster.testCase('rest/mime/type/application/x-www-form-urlencoded', {
'should place an eqauls sign between value pairs': function () {
assert.equals('foo=bar&bleep=bloop', encodeder.write({ foo: 'bar', bleep: 'bloop' }));
},
'should treat array as multiple values with the same name': function () {
assert.equals('foo=bar&foo=bloop', encodeder.write({ foo: [ 'bar', 'bloop'] }));
},
'should url encode names and values': function () {
assert.equals('fo%3Do=b%26ar', encodeder.write({ 'fo=o': 'b&ar' }));
},
'should encode spaces as plus': function () {
assert.equals('fo+o=b+ar', encodeder.write({ 'fo o': 'b ar' }));
},
'should not include an equals if their is no value': function () {
assert.equals('foo', encodeder.write({ 'foo': undefined }));
assert.equals('foo', encodeder.write({ 'foo': null }));
assert.equals('foo=', encodeder.write({ 'foo': '' }));
},
'should parse an eqauls sign between value pairs': function () {
var obj = encodeder.read('foo=bar&bleep=bloop');
assert.equals('bar', obj.foo);
assert.equals('bloop', obj.bleep);
},
'should parse multiple values with the same name as an array': function () {
var obj = encodeder.read('foo=bar&foo=bloop');
assert.equals('bar', obj.foo[0]);
assert.equals('bloop', obj.foo[1]);
},
'should url decode names and values': function () {
var obj = encodeder.read('fo%3Do=b%26ar');
assert.equals('b&ar', obj['fo=o']);
},
'should decode a plus as a space': function () {
var obj = encodeder.read('fo+o=b+ar');
assert.equals('b ar', obj['fo o']);
},
'should parse missing value as null': function () {
var obj = encodeder.read('foo');
assert.same(null, obj.foo);
}
});

@@ -63,6 +59,10 @@ });

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../../../mime/type/application/x-www-form-urlencoded'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
));
(function (buster, define) {
var plain, assert, refute;
var assert, refute, undef;

@@ -8,20 +8,18 @@ assert = buster.assert;

buster.testCase('rest/mime/type/text/plain', {
setUp: function (done) {
if (plain) { return done(); }
define('rest/mime/type/text/plain-test', ['rest/mime/type/text/plain'], function (p) {
plain = p;
done();
});
},
define('rest/mime/type/text/plain-test', function (require) {
'should not change when writing string values': function () {
assert.equals('7', plain.write('7'));
},
'should use the string representation for reading non-string values': function () {
assert.equals('7', plain.write(7));
},
'should not change when reading string values': function () {
assert.equals('7', plain.read('7'));
}
var plain = require('rest/mime/type/text/plain');
buster.testCase('rest/mime/type/text/plain', {
'should not change when writing string values': function () {
assert.equals('7', plain.write('7'));
},
'should use the string representation for reading non-string values': function () {
assert.equals('7', plain.write(7));
},
'should not change when reading string values': function () {
assert.equals('7', plain.read('7'));
}
});
});

@@ -31,6 +29,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../../../mime/type/text/plain'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
));
(function (buster, define) {
var UrlBuilder, assert, refute, undef;
var assert, refute, undef;

@@ -8,52 +8,50 @@ assert = buster.assert;

buster.testCase('rest/UrlBuilder', {
setUp: function (done) {
if (UrlBuilder) { return done(); }
define('rest/UrlBuilder-test', ['rest/UrlBuilder'], function (UB) {
UrlBuilder = UB;
done();
});
},
define('rest/UrlBuilder-test', function (require) {
'should use the provided template': function () {
assert.equals('/foo/bar', new UrlBuilder('/foo/bar').build());
},
'should replace values in the provided template': function () {
assert.equals('/foo/bar', new UrlBuilder('/foo/{foo}', { foo: 'bar' }).build());
},
'should add unused params to the query string': function () {
assert.equals('/foo/bar?foo=bar', new UrlBuilder('/foo/bar', { foo: 'bar' }).build());
},
'should add only name of unused param to query string if value is null': function () {
assert.equals('/foo/bar?foo', new UrlBuilder('/foo/bar', { foo: null }).build());
},
'should add only name of unused param to query string if value is undefined': function () {
assert.equals('/foo/bar?foo', new UrlBuilder('/foo/bar', { foo: undef }).build());
},
'should add unused params to an exsisting query string': function () {
assert.equals('/foo/bar?bleep=bloop', new UrlBuilder('/foo/{foo}', { foo: 'bar', bleep: 'bloop' }).build());
},
'should url encode all param names and values added to the url': function () {
assert.equals('/foo/bar?bl%25eep=bl%20oop', new UrlBuilder('/foo/bar', { 'bl%eep': 'bl oop' }).build());
},
'should return a built url for string concatination': function () {
assert.equals('/prefix/foo/bar', '/prefix' + new UrlBuilder('/foo/bar'));
},
'should append additional template to the current template': function () {
var foo, bar;
foo = new UrlBuilder('/foo');
bar = foo.append('/bar');
refute.same(foo, bar);
assert.equals('/foo', foo.build());
assert.equals('/foo/bar', bar.build());
},
'should add or override praram with appended values': function () {
var foo, bar;
foo = new UrlBuilder('/{foo}', { foo: '' });
bar = foo.append('/bar', { foo: 'foo', bleep: 'bloop' });
refute.same(foo, bar);
assert.equals('/', foo.build());
assert.equals('/foo/bar?bleep=bloop', bar.build());
}
// TODO test .absolute()
var UrlBuilder = require('rest/UrlBuilder');
buster.testCase('rest/UrlBuilder', {
'should use the provided template': function () {
assert.equals('/foo/bar', new UrlBuilder('/foo/bar').build());
},
'should replace values in the provided template': function () {
assert.equals('/foo/bar', new UrlBuilder('/foo/{foo}', { foo: 'bar' }).build());
},
'should add unused params to the query string': function () {
assert.equals('/foo/bar?foo=bar', new UrlBuilder('/foo/bar', { foo: 'bar' }).build());
},
'should add only name of unused param to query string if value is null': function () {
assert.equals('/foo/bar?foo', new UrlBuilder('/foo/bar', { foo: null }).build());
},
'should add only name of unused param to query string if value is undefined': function () {
assert.equals('/foo/bar?foo', new UrlBuilder('/foo/bar', { foo: undef }).build());
},
'should add unused params to an exsisting query string': function () {
assert.equals('/foo/bar?bleep=bloop', new UrlBuilder('/foo/{foo}', { foo: 'bar', bleep: 'bloop' }).build());
},
'should url encode all param names and values added to the url': function () {
assert.equals('/foo/bar?bl%25eep=bl%20oop', new UrlBuilder('/foo/bar', { 'bl%eep': 'bl oop' }).build());
},
'should return a built url for string concatination': function () {
assert.equals('/prefix/foo/bar', '/prefix' + new UrlBuilder('/foo/bar'));
},
'should append additional template to the current template': function () {
var foo, bar;
foo = new UrlBuilder('/foo');
bar = foo.append('/bar');
refute.same(foo, bar);
assert.equals('/foo', foo.build());
assert.equals('/foo/bar', bar.build());
},
'should add or override praram with appended values': function () {
var foo, bar;
foo = new UrlBuilder('/{foo}', { foo: '' });
bar = foo.append('/bar', { foo: 'foo', bleep: 'bloop' });
refute.same(foo, bar);
assert.equals('/', foo.build());
assert.equals('/foo/bar?bleep=bloop', bar.build());
}
// TODO test .absolute()
});
});

@@ -63,6 +61,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../UrlBuilder'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var base64, assert, refute;
var assert, refute;

@@ -8,17 +8,15 @@ assert = buster.assert;

buster.testCase('rest/util/base64', {
setUp: function (done) {
if (base64) { return done(); }
define('rest/util/base64-test', ['rest/util/base64'], function (b64) {
base64 = b64;
done();
});
},
define('rest/util/base64-test', function (require) {
'should base64 encode strings': function () {
assert.equals('Zm9v', base64.encode('foo'));
},
'should base64 decode strings': function () {
assert.equals('foo', base64.decode('Zm9v'));
}
var base64 = require('rest/util/base64');
buster.testCase('rest/util/base64', {
'should base64 encode strings': function () {
assert.equals('Zm9v', base64.encode('foo'));
},
'should base64 decode strings': function () {
assert.equals('foo', base64.decode('Zm9v'));
}
});
});

@@ -28,6 +26,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../util/base64'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var beget, assert, refute;
var assert, refute, undef;

@@ -8,38 +8,35 @@ assert = buster.assert;

buster.testCase('rest/util/beget', {
setUp: function (done) {
if (beget) { return done(); }
define('rest/util/beget-test', ['rest/util/beget'], function (b) {
beget = b;
done();
});
},
define('rest/util/beget-test', function (require) {
'should return an emtpy object for no args': function () {
var result, prop;
result = beget();
assert(result);
for (prop in result) {
refute(result.hasOwnProperty(prop));
var beget = require('rest/util/beget');
buster.testCase('rest/util/beget', {
'should return an emtpy object for no args': function () {
var result, prop;
result = beget();
assert(result);
for (prop in result) {
refute(result.hasOwnProperty(prop));
}
},
'should return new object with same properties': function () {
var orig, suppliment, result;
orig = {};
suppliment = { foo: 'bar' };
result = beget(orig, suppliment);
refute.same(orig, result);
assert.equals(suppliment, result);
},
'should return new object, suplimented': function () {
var orig, suppliment, result;
orig = { foo: 'bar', 'proto': 'protoValue' };
suppliment = { foo: 'foo', mine: 'mineValue' };
result = beget(orig, suppliment);
assert.equals('foo', result.foo);
assert.equals('protoValue', result.proto);
refute(result.hasOwnProperty('proto'));
assert.equals('mineValue', result.mine);
assert(result.hasOwnProperty('mine'));
}
},
'should return new object with same properties': function () {
var orig, suppliment, result;
orig = {};
suppliment = { foo: 'bar' };
result = beget(orig, suppliment);
refute.same(orig, result);
assert.equals(suppliment, result);
},
'should return new object, suplimented': function () {
var orig, suppliment, result;
orig = { foo: 'bar', 'proto': 'protoValue' };
suppliment = { foo: 'foo', mine: 'mineValue' };
result = beget(orig, suppliment);
assert.equals('foo', result.foo);
assert.equals('protoValue', result.proto);
refute(result.hasOwnProperty('proto'));
assert.equals('mineValue', result.mine);
assert(result.hasOwnProperty('mine'));
}
});
});

@@ -49,6 +46,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../util/beget'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var mixin, assert, refute;
var assert, refute, undef;

@@ -8,33 +8,31 @@ assert = buster.assert;

buster.testCase('rest/util/mixin', {
setUp: function (done) {
if (mixin) { return done(); }
define('rest/util/mixin-test', ['rest/util/mixin'], function (mi) {
mixin = mi;
done();
});
},
define('rest/util/mixin-test', function (require) {
'should return an emtpy object for no args': function () {
var mixed, prop;
mixed = mixin();
assert(mixed);
for (prop in mixed) {
refute(mixed.hasOwnProperty(prop));
var mixin = require('rest/util/mixin');
buster.testCase('rest/util/mixin', {
'should return an emtpy object for no args': function () {
var mixed, prop;
mixed = mixin();
assert(mixed);
for (prop in mixed) {
refute(mixed.hasOwnProperty(prop));
}
},
'should return original object': function () {
var orig, mixed;
orig = { foo: 'bar' };
mixed = mixin(orig);
assert.same(orig, mixed);
},
'should return original object, supplemented': function () {
var orig, supplemented, mixed;
orig = { foo: 'bar' };
supplemented = { foo: 'foo' };
mixed = mixin(orig, supplemented);
assert.same(orig, mixed);
assert.equals('foo', mixed.foo);
}
},
'should return original object': function () {
var orig, mixed;
orig = { foo: 'bar' };
mixed = mixin(orig);
assert.same(orig, mixed);
},
'should return original object, supplemented': function () {
var orig, supplemented, mixed;
orig = { foo: 'bar' };
supplemented = { foo: 'foo' };
mixed = mixin(orig, supplemented);
assert.same(orig, mixed);
assert.equals('foo', mixed.foo);
}
});
});

@@ -44,6 +42,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../util/mixin'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var normalizeHeaderName, assert, refute;
var assert, refute, undef;

@@ -8,17 +8,15 @@ assert = buster.assert;

buster.testCase('rest/util/normalizeHeaderName', {
setUp: function (done) {
if (normalizeHeaderName) { return done(); }
define('rest/util/normalizeHeaderName-test', ['rest/util/normalizeHeaderName'], function (nhn) {
normalizeHeaderName = nhn;
done();
});
},
define('rest/util/normalizeHeaderName-test', function (require) {
'should normalize header names': function () {
assert.equals('Accept', normalizeHeaderName('accept'));
assert.equals('Accept', normalizeHeaderName('ACCEPT'));
assert.equals('Content-Length', normalizeHeaderName('content-length'));
assert.equals('X-Some-Custom-Header', normalizeHeaderName('x-some-custom-header'));
}
var normalizeHeaderName = require('rest/util/normalizeHeaderName');
buster.testCase('rest/util/normalizeHeaderName', {
'should normalize header names': function () {
assert.equals('Accept', normalizeHeaderName('accept'));
assert.equals('Accept', normalizeHeaderName('ACCEPT'));
assert.equals('Content-Length', normalizeHeaderName('content-length'));
assert.equals('X-Some-Custom-Header', normalizeHeaderName('x-some-custom-header'));
}
});
});

@@ -28,6 +26,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../util/normalizeHeaderName'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var pubsub, assert, refute;
var assert, refute, undef;

@@ -8,44 +8,42 @@ assert = buster.assert;

buster.testCase('rest/util/pubsub', {
setUp: function (done) {
if (pubsub) { return done(); }
define('rest/util/pubsub-test', ['rest/util/pubsub'], function (ps) {
pubsub = ps;
done();
});
},
define('rest/util/pubsub-test', function (require) {
'should pass arguments to subscribed listener': function () {
var callback = this.spy(function (value) {
assert.equals('result', value);
});
pubsub.subscribe('topic', callback);
pubsub.publish('topic', 'result');
assert.called(callback);
},
'should ignore publish with no listeners': function () {
pubsub.publish('topic', 'result');
assert(true);
},
'should unsubscribe listener after publish': function () {
var callback = this.spy(function (value) {
assert.equals('result', value);
});
pubsub.subscribe('topic', callback);
pubsub.publish('topic', 'result');
pubsub.publish('topic', 'result2');
assert.calledOnce(callback);
},
'should only call most recent listener': function () {
var callback1, callback2;
callback1 = this.spy();
callback2 = this.spy(function (value) {
assert.equals('result', value);
});
pubsub.subscribe('topic', callback1);
pubsub.subscribe('topic', callback2);
pubsub.publish('topic', 'result');
assert.calledOnce(callback2);
refute.called(callback1);
}
var pubsub = require('rest/util/pubsub');
buster.testCase('rest/util/pubsub', {
'should pass arguments to subscribed listener': function () {
var callback = this.spy(function (value) {
assert.equals('result', value);
});
pubsub.subscribe('topic', callback);
pubsub.publish('topic', 'result');
assert.called(callback);
},
'should ignore publish with no listeners': function () {
pubsub.publish('topic', 'result');
assert(true);
},
'should unsubscribe listener after publish': function () {
var callback = this.spy(function (value) {
assert.equals('result', value);
});
pubsub.subscribe('topic', callback);
pubsub.publish('topic', 'result');
pubsub.publish('topic', 'result2');
assert.calledOnce(callback);
},
'should only call most recent listener': function () {
var callback1, callback2;
callback1 = this.spy();
callback2 = this.spy(function (value) {
assert.equals('result', value);
});
pubsub.subscribe('topic', callback1);
pubsub.subscribe('topic', callback2);
pubsub.publish('topic', 'result');
assert.calledOnce(callback2);
refute.called(callback1);
}
});
});

@@ -55,6 +53,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('../../util/pubsub'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));
(function (buster, define) {
var clientPlugin, wire, when, assert, refute, undef;
var assert, refute, undef;

@@ -8,110 +8,110 @@ assert = buster.assert;

buster.testCase('rest/wire', {
setUp: function (done) {
if (clientPlugin) { return done(); }
define('rest/wire-test', ['rest/wire', 'wire', 'when'], function (cp, wi, wh) {
clientPlugin = cp;
wire = wi;
when = wh;
done();
});
},
define('rest/wire-test', function (require) {
'should use default client! config': function (done) {
var spec, client;
client = function (request) {
return { request: request, status: { code: 200 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' };
};
spec = {
client: { $ref: 'client!', client: client },
plugins: [{ module: 'rest/wire' }]
};
wire(spec).then(function (spec) {
spec.client({}).then(function (response) {
assert.equals('bar', response.foo);
});
}).always(done);
},
'should use client! config with entity interceptor disabled': function (done) {
var spec, client;
client = function (request) {
return { request: request, status: { code: 200 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' };
};
spec = {
client: { $ref: 'client!path', client: client, accept: 'text/plain', entity: false },
plugins: [{ module: 'rest/wire' }]
};
wire(spec).then(function (spec) {
spec.client({ path: 'to/somewhere' }).then(function (response) {
assert.equals('path/to/somewhere', response.request.path);
assert.equals('text/plain', response.request.headers.Accept);
assert.equals('bar', response.entity.foo);
});
}).always(done);
},
'should be rejected for a server error status code': function (done) {
var spec, client;
client = function (request) {
return { request: request, status: { code: 500 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' };
};
spec = {
client: { $ref: 'client!', client: client },
plugins: [{ module: 'rest/wire' }]
};
wire(spec).then(function (spec) {
spec.client({}).then(
undefined,
function (response) {
var clientPlugin, wire, when;
clientPlugin = require('rest/wire');
wire = require('wire');
when = require('when');
buster.testCase('rest/wire', {
'should use default client! config': function (done) {
var spec, client;
client = function (request) {
return { request: request, status: { code: 200 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' };
};
spec = {
client: { $ref: 'client!', client: client },
plugins: [{ module: 'rest/wire' }]
};
wire(spec).then(function (spec) {
spec.client({}).then(function (response) {
assert.equals('bar', response.foo);
}
);
}).always(done);
},
'should ignore status code when errorCode interceptor is disabled': function (done) {
var spec, client;
client = function (request) {
return { request: request, status: { code: 500 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' };
};
spec = {
client: { $ref: 'client!', client: client, errorCode: false },
plugins: [{ module: 'rest/wire' }]
};
wire(spec).then(function (spec) {
spec.client({}).then(function (response) {
assert.equals('bar', response.foo);
});
}).always(done);
},
'should ignore Content-Type and entity when mime interceptor is disabled': function (done) {
var spec, client;
client = function (request) {
return { request: request, status: { code: 200 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' };
};
spec = {
client: { $ref: 'client!', client: client, mime: false },
plugins: [{ module: 'rest/wire' }]
};
wire(spec).then(function (spec) {
spec.client({}).then(function (response) {
assert.isString(response);
});
}).always(done);
},
'should use x-www-form-urlencoded as the default Content-Type for POSTs': function (done) {
var spec, client;
client = function (request) {
return { request: request, status: { code: 200 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' };
};
spec = {
client: { $ref: 'client!', client: client, entity: false },
plugins: [{ module: 'rest/wire' }]
};
wire(spec).then(function (spec) {
spec.client({ method: 'post', entity: { bleep: 'bloop' } }).then(function (response) {
assert.equals('bleep=bloop', response.request.entity);
assert.equals(0, response.request.headers.Accept.indexOf('application/x-www-form-urlencoded'));
assert.equals('application/x-www-form-urlencoded', response.request.headers['Content-Type']);
});
}).always(done);
}
});
}).always(done);
},
'should use client! config with entity interceptor disabled': function (done) {
var spec, client;
client = function (request) {
return { request: request, status: { code: 200 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' };
};
spec = {
client: { $ref: 'client!path', client: client, accept: 'text/plain', entity: false },
plugins: [{ module: 'rest/wire' }]
};
wire(spec).then(function (spec) {
spec.client({ path: 'to/somewhere' }).then(function (response) {
assert.equals('path/to/somewhere', response.request.path);
assert.equals('text/plain', response.request.headers.Accept);
assert.equals('bar', response.entity.foo);
});
}).always(done);
},
'should be rejected for a server error status code': function (done) {
var spec, client;
client = function (request) {
return { request: request, status: { code: 500 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' };
};
spec = {
client: { $ref: 'client!', client: client },
plugins: [{ module: 'rest/wire' }]
};
wire(spec).then(function (spec) {
spec.client({}).then(
undefined,
function (response) {
assert.equals('bar', response.foo);
}
);
}).always(done);
},
'should ignore status code when errorCode interceptor is disabled': function (done) {
var spec, client;
client = function (request) {
return { request: request, status: { code: 500 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' };
};
spec = {
client: { $ref: 'client!', client: client, errorCode: false },
plugins: [{ module: 'rest/wire' }]
};
wire(spec).then(function (spec) {
spec.client({}).then(function (response) {
assert.equals('bar', response.foo);
});
}).always(done);
},
'should ignore Content-Type and entity when mime interceptor is disabled': function (done) {
var spec, client;
client = function (request) {
return { request: request, status: { code: 200 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' };
};
spec = {
client: { $ref: 'client!', client: client, mime: false },
plugins: [{ module: 'rest/wire' }]
};
wire(spec).then(function (spec) {
spec.client({}).then(function (response) {
assert.isString(response);
});
}).always(done);
},
'should use x-www-form-urlencoded as the default Content-Type for POSTs': function (done) {
var spec, client;
client = function (request) {
return { request: request, status: { code: 200 }, headers: { 'Content-Type': 'application/json' }, entity: '{"foo":"bar"}' };
};
spec = {
client: { $ref: 'client!', client: client, entity: false },
plugins: [{ module: 'rest/wire' }]
};
wire(spec).then(function (spec) {
spec.client({ method: 'post', entity: { bleep: 'bloop' } }).then(function (response) {
assert.equals('bleep=bloop', response.request.entity);
assert.equals(0, response.request.headers.Accept.indexOf('application/x-www-form-urlencoded'));
assert.equals('application/x-www-form-urlencoded', response.request.headers['Content-Type']);
});
}).always(done);
}
});
});

@@ -121,6 +121,10 @@

this.buster || require('buster'),
typeof define === 'function' ? define : function (id, deps, factory) {
factory(require('./rest/wire'), require('wire'), require('when'));
typeof define === 'function' && define.amd ? define : function (id, factory) {
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..');
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot;
factory(function (moduleId) {
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId);
});
}
// Boilerplate for AMD and Node
));

@@ -1,8 +0,10 @@

(function (doc, define) {
(function (define, document) {
define(['./util/beget'], function (beget) {
define(function (require) {
"use strict";
var absoluteUrlRE, urlEncodedBraceOpenRE, urlEncodedBraceCloseRE;
var beget, absoluteUrlRE, urlEncodedBraceOpenRE, urlEncodedBraceCloseRE;
beget = require('./util/beget');
absoluteUrlRE = /^https?:\/\//i;

@@ -95,7 +97,7 @@ urlEncodedBraceOpenRE = /%7b/i;

absolute: function () {
if (!doc || absoluteUrlRE.test(this._template)) { return this; }
if (!document || absoluteUrlRE.test(this._template)) { return this; }
var a, template;
a = doc.createElement('a');
a = document.createElement('a');
a.href = this._template;

@@ -123,2 +125,3 @@ template = a.href.replace(urlEncodedBraceOpenRE, '{').replace(urlEncodedBraceCloseRE, '}');

}
};

@@ -130,7 +133,5 @@

}(
this.document,
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); },
this.document
// Boilerplate for AMD and Node
));

@@ -1,165 +0,146 @@

/*jshint bitwise: false */
/*
* Base 64 implementation in JavaScript
* Copyright (c) 2009 Nicholas C. Zakas. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* Original source available at https://raw.github.com/nzakas/computer-science-in-javascript/02a2745b4aa8214f2cae1bf0b15b447ca1a91b23/encodings/base64/base64.js
*
* Converted to AMD and linter refinement by Scott Andrews
*/
(function (define) {
/*
* Base64 encode / decode
* http://www.webtoolkit.info/
*
* Converted to AMD
*/
define([], function () {
/*jshint bitwise: false */
define(function (require) {
"use strict";
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
/**
* Base64 encode input
*
* @param {String} input value to encode
* @return {String} the encoded value
* Base64-encodes a string of text.
* @param {String} text The text to encode.
* @return {String} The base64-encoded string.
*/
function encode(input) {
var output, chr1, chr2, chr3, enc1, enc2, enc3, enc4, i;
function base64Encode(text) {
output = "";
i = 0;
input = _utf8_encode(input);
if (/([^\u0000-\u00ff])/.test(text)) {
throw new Error("Can't base64 encode non-ASCII characters.");
}
while (i < input.length) {
var i = 0,
cur, prev, byteNum,
result = [];
chr1 = input.charCodeAt(i);
i += 1;
chr2 = input.charCodeAt(i);
i += 1;
chr3 = input.charCodeAt(i);
i += 1;
while (i < text.length) {
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
cur = text.charCodeAt(i);
byteNum = i % 3;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
switch (byteNum) {
case 0: //first byte
result.push(digits.charAt(cur >> 2));
break;
case 1: //second byte
result.push(digits.charAt((prev & 3) << 4 | (cur >> 4)));
break;
case 2: //third byte
result.push(digits.charAt((prev & 0x0f) << 2 | (cur >> 6)));
result.push(digits.charAt(cur & 0x3f));
break;
}
output = output +
chars.charAt(enc1) + chars.charAt(enc2) +
chars.charAt(enc3) + chars.charAt(enc4);
prev = cur;
i += 1;
}
if (byteNum === 0) {
result.push(digits.charAt((prev & 3) << 4));
result.push('==');
} else if (byteNum === 1) {
result.push(digits.charAt((prev & 0x0f) << 2));
result.push('=');
}
return output;
return result.join('');
}
/**
* Base64 decode input
*
* @param {String} input the value to decode
* @return {String} the decoded value
* Base64-decodes a string of text.
* @param {String} text The text to decode.
* @return {String} The base64-decoded string.
*/
function decode(input) {
var output, chr1, chr2, chr3, enc1, enc2, enc3, enc4, i;
function base64Decode(text) {
output = "";
i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
//ignore white space
text = text.replace(/\s/g, '');
while (i < input.length) {
//first check for any unexpected input
if (!(/^[a-z0-9\+\/\s]+\={0,2}$/i.test(text)) || text.length % 4 > 0) {
throw new Error("Not a base64-encoded string.");
}
enc1 = chars.indexOf(input.charAt(i));
i += 1;
enc2 = chars.indexOf(input.charAt(i));
i += 1;
enc3 = chars.indexOf(input.charAt(i));
i += 1;
enc4 = chars.indexOf(input.charAt(i));
i += 1;
//local variables
var cur, prev, digitNum,
i = 0,
result = [];
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
//remove any equals signs
text = text.replace(/\=/g, '');
output = output + String.fromCharCode(chr1);
//loop over each character
while (i < text.length) {
if (enc3 !== 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 !== 64) {
output = output + String.fromCharCode(chr3);
}
cur = digits.indexOf(text.charAt(i));
digitNum = i % 4;
}
switch (digitNum) {
output = _utf8_decode(output);
//case 0: first digit - do nothing, not enough info to work with
return output;
case 1: //second digit
result.push(String.fromCharCode(prev << 2 | cur >> 4));
break;
}
case 2: //third digit
result.push(String.fromCharCode((prev & 0x0f) << 4 | cur >> 2));
break;
function _utf8_encode(string) {
var utftext, n, c;
utftext = "";
string = string.replace(/\r\n/g, '\n');
for (n = 0; n < string.length; n += 1) {
c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
case 3: //fourth digit
result.push(String.fromCharCode((prev & 3) << 6 | cur));
break;
}
else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
prev = cur;
i += 1;
}
return utftext;
}
//return a string
return result.join('');
function _utf8_decode(utftext) {
var string, i, c, c1, c2, c3;
string = "";
i = 0;
c = c1 = c2 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i += 1;
}
else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
return {
encode: encode,
decode: decode
encode: base64Encode,
decode: base64Decode
};

@@ -170,6 +151,4 @@

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define(['./mixin'], function (mixin) {
// derived from dojo.delegate
define(function (require) {
"use strict";
// derived from dojo.delegate
var mixin;
mixin = require('./mixin');
function Beget() {}

@@ -33,6 +36,4 @@

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define([], function () {
// derived from dojo.mixin
define(function (require) {
"use strict";
// derived from dojo.mixin
var empty = {};

@@ -49,6 +48,4 @@

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define([], function () {
define(function (require) {
"use strict";

@@ -31,6 +31,4 @@

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define([], function () {
define(function (require) {
"use strict";

@@ -46,6 +46,4 @@

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
(function (define) {
define(['../rest', './interceptor/errorCode', './interceptor/mime', './interceptor/entity', './interceptor/pathPrefix', 'when'], function (client, errorCode, mime, entity, pathPrefix, when) {
define(function (require) {
var plugin;
var client, errorCode, mime, entity, pathPrefix, when, plugin;
client = require('../rest');
errorCode = require('./interceptor/errorCode');
mime = require('./interceptor/mime');
entity = require('./interceptor/entity');
pathPrefix = require('./interceptor/pathPrefix');
when = require('when');
function parseConfig(name, refObj) {

@@ -74,6 +82,4 @@ return {

}(
typeof define === 'function' ? define : function (deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc