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

be-hippo

Package Overview
Dependencies
Maintainers
4
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

be-hippo - npm Package Compare versions

Comparing version 6.0.1 to 7.0.0

.babelrc

11

index.js

@@ -1,9 +0,2 @@

define([
'./lib/client'
], function(Client) {
'use strict';
return {
Client: Client
};
});
import Client from './src/client';
export default { Client };

@@ -7,6 +7,68 @@ /* eslint-env node */

var commonConfig = require('./karma-common-config');
var webpackConfig = require('./webpack.config.js');
webpackConfig.devtool = 'eval';
// Only run coverage report during `npm test`
if (process.env.npm_lifecycle_event === 'test') {
webpackConfig.module.postLoaders = webpackConfig.module.postLoaders || [];
webpackConfig.module.postLoaders.push({
test: /\.js$/,
exclude: /(test|node_modules)\//,
loader: 'istanbul-instrumenter'
});
}
module.exports = function(config) {
config.set(commonConfig);
config.set({
basePath: '',
frameworks: ['jasmine', 'sinon'],
files: [
'node_modules/babel-polyfill/dist/polyfill.js',
require.resolve('whatwg-fetch'),
'test/index.js'
],
coverageReporter: {
reporters: [
{
type: 'text-summary'
},
{
type: 'html',
dir: 'coverage/'
}
]
},
preprocessors: {
'test/index.js': ['webpack']
},
reporters: ['progress', 'coverage'],
mochaReporter: {
ignoreSkipped: true
},
webpack: webpackConfig,
webpackMiddleware: {
noInfo: true
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['PhantomJS'],
singleRun: false
});
};
{
"name": "be-hippo",
"version": "6.0.1",
"version": "7.0.0",
"description": "A PropositionH hypermedia consumer",
"main": "index.js",
"main": "./index.js",
"scripts": {
"start": "gulp",
"test": "eslint . && gulp test",
"spec": "gulp test",
"start": "./node_modules/karma/bin/karma start",
"test": "eslint . && ./node_modules/karma/bin/karma start --singleRun",
"spec": "./node_modules/karma/bin/karma start --singleRun",
"lint": "eslint ."

@@ -20,31 +20,32 @@ },

"devDependencies": {
"bower": "^1.3.8",
"chai": "^1.9.1",
"chai-as-promised": "^4.1.1",
"es6-promise": "^2.3.0",
"babel-core": "^6.11.4",
"babel-loader": "^6.2.4",
"babel-polyfill": "^6.9.1",
"babel-preset-behance": "^1.0.0",
"eslint": "^3.1.1",
"eslint-preset-behance": "^3.0.1",
"gulp": "^3.8.6",
"gulp-debug": "^1.0.0",
"gulp-karma": "0.0.4",
"karma": "^0.12.17",
"istanbul": "^0.4.4",
"istanbul-instrumenter-loader": "^0.2.0",
"jasmine-core": "^2.4.1",
"karma": "^1.1.2",
"karma-chrome-launcher": "^0.1.4",
"karma-coverage": "^0.2.5",
"karma-mocha": "^0.1.6",
"karma-osx-reporter": "^0.1.0",
"karma-phantomjs-launcher": "^1.0.1",
"karma-requirejs": "^0.2.2",
"karma-sinon-chai": "^0.2.0",
"lodash": "^2.4.1",
"mocha": "^2.5.3",
"karma-jasmine": "^1.0.2",
"karma-mocha-reporter": "^2.1.0",
"karma-notify-reporter": "^1.0.1",
"karma-sinon": "^1.0.5",
"karma-webpack": "^1.7.0",
"mocha": "^3.0.0",
"phantomjs-prebuilt": "^2.1.7",
"pretender": "^0.1.0",
"requirejs": "~2.1.10",
"sinon": "^1.17.5",
"webpack": "^1.13.1",
"webpack-notifier": "^1.3.0",
"whatwg-fetch": "^1.0.0"
},
"peerDependencies": {
"es6-promise": "^2.3.0",
"whatwg-fetch": "^1.0.0"
},
"dependencies": {
"lodash.merge": "^4.5.1"
}
}

@@ -1,112 +0,143 @@

define(['chai-as-promised', 'lib/client', 'lib/resource'], function(chaiAsPromised, Client, Resource) {
chai.use(chaiAsPromised);
import Client from 'src/client';
import Resource from 'src/resource';
describe('Client', function() {
var server;
describe('Client', function() {
var server;
beforeEach(function() {
server = sinon.fakeServer.create();
server.autoRespond = true;
});
beforeEach(function() {
server = sinon.fakeServer.create();
server.autoRespond = true;
});
afterEach(function() {
server.restore();
server = null;
});
afterEach(function() {
server.restore();
server = null;
});
it('can be instantiated', function() {
expect(new Client("")).to.be.an.instanceOf(Client);
});
it('can be instantiated', function() {
expect(new Client('') instanceof Client).toBeTruthy();
});
describe('#constructor', function() {
describe('when not given an api root', function() {
it('throws an error', function() {
expect(function() {
new Client();
}).to.throw(/Client must be initialized with an API Root/);
});
describe('#constructor', function() {
describe('when not given an api root', function() {
it('throws an error', function() {
expect(function() {
new Client();
}).toThrowError(/Client must be initialized with an API Root/);
});
});
});
describe('#walk', function() {
describe('when given an api root', function() {
var client = new Client('/v1/foo');
describe('#walk', function() {
describe('when given an api root', function() {
var client = new Client('/v1/foo');
afterEach(function() {
client.clearDescriptorCache();
afterEach(function() {
client.clearDescriptorCache();
});
describe('that does not conform to the hypermedia format', function() {
var response = [200, { 'Content-Type': 'application/json' }, JSON.stringify({})];
it('returns a rejected promise', function(done) {
server.respondWith('OPTIONS', '/v1/foo', response);
client.walk()
.catch((e) => {
expect(e.message).toMatch(/API does not conform to expected hypermedia format/);
})
.then(done, done.fail);
});
});
describe('that does not conform to the hypermedia format', function() {
var response = [200, { "Content-Type": "application/json" }, JSON.stringify({})];
describe('that does conform to the hypermedia format', function() {
var links = {
_links: {
self: { href: '/v1/foo' }
}
};
var response = [200, { 'Content-Type': 'application/json' }, JSON.stringify(links)];
it('returns a rejected promise', function() {
server.respondWith('OPTIONS', '/v1/foo', response);
return expect(client.walk()).to.be.rejectedWith(/API does not conform to expected hypermedia format/);
});
it('returns a resolved promise ', function(done) {
server.respondWith('OPTIONS', '/v1/foo', response);
client.walk()
.then((res) => {
expect(res).toEqual(new Resource(links));
})
.then(done, done.fail);
});
describe('that does conform to the hypermedia format', function() {
var links = {
_links: {
self: { href: '/v1/foo' }
}
};
var response = [200, { "Content-Type": "application/json" }, JSON.stringify(links)];
it('passes walk options to the walk request', function(done) {
var options = { walkOptions: { headers: { foo: 'hello' } } };
var client = new Client('/v1/foo', options);
it('returns a resolved promise ', function() {
server.respondWith('OPTIONS', '/v1/foo', response);
return expect(client.walk()).to.become(new Resource(links));
server.respondWith('OPTIONS', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('hello');
req.respond.apply(req, response);
done();
});
it('passes walk options to the walk request', function() {
var options = { walkOptions: { headers: { foo: 'hello' } } };
var client = new Client('/v1/foo', options);
client.walk();
});
server.respondWith('OPTIONS', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.eql('hello');
req.respond.apply(req, response);
});
it('passes request options to created resources', function(done) {
var options = { requestOptions: { headers: { foo: 'hello' } } };
var client = new Client('/v1/foo', options);
return client.walk();
});
server.respondWith('OPTIONS', '/v1/foo', response);
client.walk()
.then((res) => {
expect(res).toEqual(new Resource(links, options.requestOptions));
})
.then(done, done.fail);
});
});
});
it('passes request options to created resources', function() {
var options = { requestOptions: { headers: { foo: 'hello' } } };
var client = new Client('/v1/foo', options);
describe('when given a shortname', function() {
var links = {
root: {
_links: {
self: { href: '/v1' },
foo: { href: '/v1/foo' },
templated: { href: '/v1/temp{?var}' }
}
},
foo: {
_links: {
self: { href: '/v1/foo' }
}
},
};
var responses = {
root: [200, { 'Content-Type': 'application/json' }, JSON.stringify(links.root)],
foo: [200, { 'Content-Type': 'application/json' }, JSON.stringify(links.foo)]
};
server.respondWith('OPTIONS', '/v1/foo', response);
return expect(client.walk()).to.become(new Resource(links, options.requestOptions));
});
describe('that is falsey', function() {
it('returns a rejected promise', function(done) {
var client = new Client('/v1');
server.respondWith('OPTIONS', '/v1', responses.root);
client.walk('')
.catch((e) => {
expect(e.message).toMatch(/Client walk: A shortname must be provided/);
})
.then(done, done.fail);
});
});
describe('when given a shortname', function() {
var links = {
root: {
_links: {
self: { href: '/v1' },
foo: { href: '/v1/foo' },
templated: { href: '/v1/temp{?var}' }
}
},
foo: {
_links: {
self: { href: '/v1/foo' }
}
},
};
var responses = {
root: [200, { "Content-Type": "application/json" }, JSON.stringify(links.root)],
foo: [200, { "Content-Type": "application/json" }, JSON.stringify(links.foo)]
};
it('returns a resolved promise of a resource', function(done) {
var client = new Client('/v1');
describe('that is falsey', function() {
it('returns a rejected promise', function() {
var client = new Client('/v1');
server.respondWith('OPTIONS', '/v1', responses.root);
return expect(client.walk('')).to.eventually.be.rejectedWith(/Client walk: A shortname must be provided/);
});
});
server.respondWith('OPTIONS', '/v1', responses.root);
server.respondWith('OPTIONS', '/v1/foo', responses.foo);
it('returns a resolved promise of a resouce', function() {
client.walk('foo')
.then((res) => {
expect(res).toEqual(new Resource(links.foo));
})
.then(done, done.fail);
});
describe('that does not exist', function() {
it('returns a promise that rejects', function(done) {
var client = new Client('/v1');

@@ -117,185 +148,191 @@

return expect(client.walk('foo')).to.become(new Resource(links.foo));
client.walk('bar')
.catch((e) => {
expect(e.message).toMatch(/Unknown connection/);
})
.then(done, done.fail);
});
});
describe('that does not exist', function() {
it('returns a promise that rejects', function() {
var client = new Client('/v1');
describe('that has not already been traversed', function() {
it('traverses from the root', function(done) {
var client = new Client('/v1');
var xhrCalls = jasmine.createSpy('xhr');
server.respondWith('OPTIONS', '/v1', responses.root);
server.respondWith('OPTIONS', '/v1/foo', responses.foo);
server.respondWith('OPTIONS', '/v1', function(req) {
xhrCalls();
req.respond.apply(req, responses.root);
});
server.respondWith('OPTIONS', '/v1/foo', function(req) {
xhrCalls();
req.respond.apply(req, responses.foo);
});
return expect(client.walk('bar')).to.eventually.be.rejectedWith(/Unknown connection/);
});
client.walk()
.then(function() {
expect(xhrCalls.calls.count()).toEqual(1);
return client.walk('foo');
})
.then(function() {
expect(xhrCalls.calls.count()).toEqual(2);
})
.then(done, done.fail);
});
});
describe('that has not already been traversed', function() {
it('traverses from the root', function() {
var client = new Client('/v1');
var xhrCalls = sinon.spy();
describe('that has already been traversed', function() {
it('does not make duplicate requests', function(done) {
var client = new Client('/v1');
var xhrCalls = jasmine.createSpy('xhr');
server.respondWith('OPTIONS', '/v1', function(req) {
xhrCalls();
req.respond.apply(req, responses.root);
});
server.respondWith('OPTIONS', '/v1/foo', function(req) {
xhrCalls();
req.respond.apply(req, responses.foo);
});
return client.walk()
.then(function() {
expect(xhrCalls).to.have.been.calledOnce;
return client.walk('foo');
})
.then(function() {
expect(xhrCalls).to.have.been.calledTwice;
});
server.respondWith('OPTIONS', '/v1', function(req) {
xhrCalls();
req.respond.apply(req, responses.root);
});
});
server.respondWith('OPTIONS', '/v1/foo', function(req) {
xhrCalls();
req.respond.apply(req, responses.foo);
});
describe('that has already been traversed', function() {
it('does not make duplicate requests', function() {
var client = new Client('/v1');
var xhrCalls = sinon.spy();
server.respondWith('OPTIONS', '/v1', function(req) {
xhrCalls();
req.respond.apply(req, responses.root);
});
server.respondWith('OPTIONS', '/v1/foo', function(req) {
xhrCalls();
req.respond.apply(req, responses.foo);
});
return client.walk('foo')
.then(function() {
expect(xhrCalls).to.have.been.calledTwice;
return client.walk('foo');
})
.then(function() {
expect(xhrCalls).to.have.been.calledTwice;
});
});
client.walk('foo')
.then(function() {
expect(xhrCalls.calls.count()).toEqual(2);
return client.walk('foo');
})
.then(function() {
expect(xhrCalls.calls.count()).toEqual(2);
})
.then(done, done.fail);
});
});
it('returns a resolved promise', function() {
var client = new Client('/v1');
var node = new Resource(links.foo);
it('returns a resolved promise', function(done) {
var client = new Client('/v1');
var node = new Resource(links.foo);
server.respondWith('OPTIONS', '/v1', responses.root);
server.respondWith('OPTIONS', '/v1/foo', responses.foo);
server.respondWith('OPTIONS', '/v1', responses.root);
server.respondWith('OPTIONS', '/v1/foo', responses.foo);
return client.walk('foo')
.then(function(resource) {
expect(resource).to.deep.equal(node);
return client.walk('foo')
.then(function(resource) {
expect(resource).to.deep.equal(node);
});
});
});
client.walk('foo')
.then(function(resource) {
expect(resource).toEqual(node);
})
.then(() => {
return client.walk('foo');
})
.then(function(resource) {
expect(resource).toEqual(node);
})
.then(done, done.fail);
});
});
describe('when given a shortname object', function() {
var links = {
root: {
_links: {
self: { href: '/v1' },
templated: { href: '/v1/temp{?var}' }
}
},
templated: {
_links: {
self: { href: '/v1/temp{?var}' }
}
describe('when given a shortname object', function() {
var links = {
root: {
_links: {
self: { href: '/v1' },
templated: { href: '/v1/temp{?var}' }
}
};
var responses = {
root: [200, { "Content-Type": "application/json" }, JSON.stringify(links.root)],
templated: [200, { "Content-Type": "application/json" }, JSON.stringify(links.templated)]
};
},
templated: {
_links: {
self: { href: '/v1/temp{?var}' }
}
}
};
var responses = {
root: [200, { 'Content-Type': 'application/json' }, JSON.stringify(links.root)],
templated: [200, { 'Content-Type': 'application/json' }, JSON.stringify(links.templated)]
};
it('uses the object to template out the uri', function() {
var client = new Client('/v1');
var uri;
it('uses the object to template out the uri', function(done) {
var client = new Client('/v1');
var uri;
server.respondWith('OPTIONS', '/v1', responses.root);
server.respondWith('OPTIONS', /\/v1\/temp/, function(req) {
uri = req.url;
req.respond.apply(req, responses.templated);
});
server.respondWith('OPTIONS', '/v1', responses.root);
server.respondWith('OPTIONS', /\/v1\/temp/, function(req) {
uri = req.url;
req.respond.apply(req, responses.templated);
});
return client.walk({ name: 'templated', data: { var: 5 } })
.then(function() {
expect(uri).to.equal('/v1/temp?var=5');
});
});
client.walk({ name: 'templated', data: { var: 5 } })
.then(function() {
expect(uri).toEqual('/v1/temp?var=5');
})
.then(done, done.fail);
});
});
describe('when given a combination of shortnames and shortname objects', function() {
var links = {
root: {
_links: {
self: { href: '/v1' },
foo: { href: '/v1/foo{?user}' }
}
},
foo: {
_links: {
self: { href: '/v1/foo{?user}' },
templated: { href: '/v1/temp{?var}' }
}
},
templated: {
_links: {
self: { href: '/v1/temp{?var}' }
}
describe('when given a combination of shortnames and shortname objects', function() {
var links = {
root: {
_links: {
self: { href: '/v1' },
foo: { href: '/v1/foo{?user}' }
}
};
var responses = {
root: [200, { "Content-Type": "application/json" }, JSON.stringify(links.root)],
foo: [200, { "Content-Type": "application/json" }, JSON.stringify(links.foo)],
templated: [200, { "Content-Type": "application/json" }, JSON.stringify(links.templated)]
};
},
foo: {
_links: {
self: { href: '/v1/foo{?user}' },
templated: { href: '/v1/temp{?var}' }
}
},
templated: {
_links: {
self: { href: '/v1/temp{?var}' }
}
}
};
var responses = {
root: [200, { 'Content-Type': 'application/json' }, JSON.stringify(links.root)],
foo: [200, { 'Content-Type': 'application/json' }, JSON.stringify(links.foo)],
templated: [200, { 'Content-Type': 'application/json' }, JSON.stringify(links.templated)]
};
it('returns a resource for the resulting traversal', function() {
var client = new Client('/v1');
it('returns a resource for the resulting traversal', function(done) {
var client = new Client('/v1');
server.respondWith('OPTIONS', '/v1', responses.root);
server.respondWith('OPTIONS', '/v1/foo', responses.foo);
server.respondWith('OPTIONS', /\/v1\/temp/, responses.templated);
server.respondWith('OPTIONS', '/v1', responses.root);
server.respondWith('OPTIONS', '/v1/foo', responses.foo);
server.respondWith('OPTIONS', /\/v1\/temp/, responses.templated);
return expect(client.walk('foo', { name: 'templated', data: { var: 'hello' } })).to.become(new Resource(links.templated));
});
client.walk('foo', { name: 'templated', data: { var: 'hello' } })
.then((res) => {
expect(res).toEqual(new Resource(links.templated));
})
.then(done, done.fail);
});
});
describe('when a shortname walk 404s', function() {
var links = {
root: {
_links: {
self: { href: '/v1' },
foo: { href: '/v1/foo{?user}' }
}
},
foo: {
_links: {
self: { href: '/v1/foo{?user}' }
}
describe('when a shortname walk 404s', function() {
var links = {
root: {
_links: {
self: { href: '/v1' },
foo: { href: '/v1/foo{?user}' }
}
};
var responses = {
root: [200, { "Content-Type": "application/json" }, JSON.stringify(links.root)],
foo: [404, { "Content-Type": "text/plain" }, ''],
};
},
foo: {
_links: {
self: { href: '/v1/foo{?user}' }
}
}
};
var responses = {
root: [200, { 'Content-Type': 'application/json' }, JSON.stringify(links.root)],
foo: [404, { 'Content-Type': 'text/plain' }, ''],
};
it('returns an "Unable to get descriptor" error', function() {
var client = new Client('/v1');
it('returns an "Unable to get descriptor" error', function(done) {
var client = new Client('/v1');
server.respondWith('OPTIONS', '/v1', responses.root);
server.respondWith('OPTIONS', '/v1/foo', responses.foo);
server.respondWith('OPTIONS', '/v1', responses.root);
server.respondWith('OPTIONS', '/v1/foo', responses.foo);
return expect(client.walk('foo')).to.be.rejectedWith(new RegExp('Unable to get descriptor for uri "/v1/foo"'));
});
client.walk('foo')
.catch((e) => {
expect(e.message).toMatch(new RegExp('Unable to get descriptor for uri "/v1/foo"'));
})
.then(done, done.fail);
});

@@ -302,0 +339,0 @@ });

@@ -1,63 +0,41 @@

define(['chai-as-promised', 'lib/resource'], function(chaiAsPromised, Resource) {
chai.use(chaiAsPromised);
import Resource from 'src/resource';
function parseResponse(res) {
return res.json();
}
describe('Resource', function() {
it('is a constructor', function() {
expect(new Resource({ _links: {} }) instanceof Resource).toBeTruthy();
});
describe('Resource', function() {
it('is a constructor', function() {
expect(new Resource({ _links: {} })).to.be.an.instanceOf(Resource);
describe('constructor', function() {
describe('when not given a descriptor', function() {
it('throws an error', function() {
expect(function() {
new Resource();
}).toThrowError(/Resource constructor requires a description/);
});
});
describe('constructor', function() {
describe('when not given a descriptor', function() {
it('throws an error', function() {
expect(function() {
new Resource();
}).to.throw(/Resource constructor requires a description/);
});
describe('when given an ill formed descriptor', function() {
it('throws an error', function() {
expect(function() {
new Resource({ lol: 'bad data' });
}).toThrowError(/Resource constructor given a malformed descriptor/);
});
describe('when given an ill formed descriptor', function() {
it('throws an error', function() {
expect(function() {
new Resource({ lol: 'bad data' });
}).to.throw(/Resource constructor given a malformed descriptor/);
});
});
describe('when given a descriptor with an ill formed link', function() {
it('throws an error', function() {
var descriptor = {
_links: {
bad: {}
}
};
expect(function() {
new Resource(descriptor);
}).to.throw(/Malformed connection/);
});
});
});
describe('#description', function() {
it ('returns the description object', function() {
describe('when given a descriptor with an ill formed link', function() {
it('throws an error', function() {
var descriptor = {
_links: {
self: {
href: '/'
},
foo: {
href: '/foo{?bar}'
}
bad: {}
}
};
var resource = new Resource(descriptor);
expect(resource.description()).to.equal(descriptor._links);
expect(function() {
new Resource(descriptor);
}).toThrowError(/Malformed connection/);
});
});
});
describe('#getConnection', function() {
describe('#description', function() {
it ('returns the description object', function() {
var descriptor = {

@@ -75,649 +53,793 @@ _links: {

describe('when given a resource that is falsey', function() {
it('throws an error', function() {
expect(function() {
resource.getConnection('');
}).to.throw(/No shortname given for connection/);
});
expect(resource.description()).toEqual(descriptor._links);
});
});
describe('#getConnection', function() {
var descriptor = {
_links: {
self: {
href: '/'
},
foo: {
href: '/foo{?bar}'
}
}
};
var resource = new Resource(descriptor);
describe('when given a resource that is falsey', function() {
it('throws an error', function() {
expect(function() {
resource.getConnection('');
}).toThrowError(/No shortname given for connection/);
});
});
describe('when given a shortname that is not known', function() {
it('throws an error', function() {
expect(function() {
resource.getConnection('bar');
}).to.throw(/Unknown connection/);
});
describe('when given a shortname that is not known', function() {
it('throws an error', function() {
expect(function() {
resource.getConnection('bar');
}).toThrowError(/Unknown connection/);
});
});
describe('when given a shortname that is known', function() {
it('returns the href for that connection', function() {
expect(resource.getConnection('self')).to.equal('/');
expect(resource.getConnection('foo')).to.equal('/foo');
});
describe('when given a shortname that is known', function() {
it('returns the href for that connection', function() {
expect(resource.getConnection('self')).toEqual('/');
expect(resource.getConnection('foo')).toEqual('/foo');
});
});
describe('when given a shortname that is an object', function() {
it('returns the href for that connection templated out', function() {
var connection = {
name: 'foo',
data: {
bar: 5
}
};
describe('when given a shortname that is an object', function() {
it('returns the href for that connection templated out', function() {
var connection = {
name: 'foo',
data: {
bar: 5
}
};
expect(resource.getConnection(connection)).to.equal('/foo?bar=5');
});
expect(resource.getConnection(connection)).toEqual('/foo?bar=5');
});
});
});
describe('resource loader methods', function() {
var server;
describe('resource loader methods', function() {
var server;
beforeEach(function() {
server = sinon.fakeServer.create();
server.autoRespond = true;
});
beforeEach(function() {
server = sinon.fakeServer.create();
server.autoRespond = true;
});
afterEach(function() {
server.restore();
server = null;
});
afterEach(function() {
server.restore();
server = null;
});
describe('#get', function() {
var descriptor = {
_links: {
self: {
href: '/v1/foo{?bar}'
}
describe('#get', function() {
var descriptor = {
_links: {
self: {
href: '/v1/foo{?bar}'
}
};
var result = {
hello: 'world'
};
}
};
var result = {
hello: 'world'
};
var responses = {
foo: [200, { "Content-Type": "application/json" }, JSON.stringify(result)],
};
var responses = {
foo: [200, { 'Content-Type': 'application/json' }, JSON.stringify(result)],
};
it('returns the result of sending a GET request to the resource', function() {
it('returns the result of sending a GET request to the resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('GET', '/v1/foo', responses.foo);
resource.get()
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
describe('when given a params object', function() {
it('return the result of sending a GET request to the templated resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('GET', '/v1/foo', responses.foo);
return expect(resource.get().then(parseResponse)).to.become(result);
server.respondWith('GET', '/v1/foo?bar=10', responses.foo);
resource.get({ bar: 10 })
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given a params object', function() {
it('return the result of sending a GET request to the templated resource', function() {
var resource = new Resource(descriptor);
server.respondWith('GET', '/v1/foo?bar=10', responses.foo);
return expect(resource.get({ bar: 10 }).then(parseResponse)).to.become(result);
describe('when given resource level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('GET', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
resource.get({ bar: 10 });
});
});
describe('when given resource level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
describe('when given request level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor);
server.respondWith('GET', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('GET', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.get({ bar: 10 });
});
resource.get({ bar: 10 }, { headers: { foo: 'bar' } });
});
});
describe('when given request level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor);
describe('when given resource and request level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('GET', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('GET', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).toEqual('baz');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.get({ bar: 10 }, { headers: { foo: 'bar' } });
});
resource.get({ bar: 10 }, { headers: { foo: 'baz' } });
});
});
describe('when given resource and request level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
describe('when given resource and request level headers', function() {
it('merges those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('GET', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).to.equal('baz');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('GET', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
expect(req.requestHeaders.other).toEqual('str');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.get({ bar: 10 }, { headers: { foo: 'baz' } });
});
resource.get({ bar: 10 }, { headers: { other: 'str' } });
});
describe('when given resource and request level headers', function() {
it('merges those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
it('overwrites those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('GET', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
expect(req.requestHeaders.other).to.equal('str');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
return resource.get({ bar: 10 }, { headers: { other: 'str' } });
server.respondWith('GET', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).toEqual('baz');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
it('overwrites those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('GET', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).to.equal('baz');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
return resource.get({ bar: 10 }, { headers: { foo: 'baz' } });
});
resource.get({ bar: 10 }, { headers: { foo: 'baz' } });
});
});
});
describe('#head', function() {
var descriptor = {
_links: {
self: {
href: '/v1/foo{?bar}'
}
describe('#head', function() {
var descriptor = {
_links: {
self: {
href: '/v1/foo{?bar}'
}
};
var result = {
hello: 'world'
};
}
};
var result = {
hello: 'world'
};
var responses = {
foo: [200, { "Content-Type": "application/json" }, JSON.stringify(result)],
};
var responses = {
foo: [200, { 'Content-Type': 'application/json' }, JSON.stringify(result)],
};
it('returns the result of sending a DELETE request to the resource', function() {
it('returns the result of sending a DELETE request to the resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('HEAD', '/v1/foo', responses.foo);
resource.head()
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
describe('when given a params object', function() {
it('return the result of sending a DELETE request to the templated resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('HEAD', '/v1/foo', responses.foo);
return expect(resource.head().then(parseResponse)).to.become(result);
server.respondWith('HEAD', '/v1/foo?bar=10', responses.foo);
resource.head({ bar: 10 })
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given a params object', function() {
it('return the result of sending a DELETE request to the templated resource', function() {
var resource = new Resource(descriptor);
server.respondWith('HEAD', '/v1/foo?bar=10', responses.foo);
return expect(resource.head({ bar: 10 }).then(parseResponse)).to.become(result);
describe('when given resource level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('HEAD', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
resource.head({ bar: 10 });
});
});
describe('when given resource level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
describe('when given request level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor);
server.respondWith('HEAD', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('HEAD', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.head({ bar: 10 });
});
resource.head({ bar: 10 }, { headers: { foo: 'bar' } });
});
});
describe('when given request level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor);
describe('when given resource and request level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('HEAD', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
return resource.head({ bar: 10 }, { headers: { foo: 'bar' } });
server.respondWith('HEAD', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).toEqual('baz');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
});
describe('when given resource and request level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('HEAD', '/v1/foo?bar=10', function(req) {
expect(req.requestHeaders.foo).to.equal('baz');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
return resource.head({ bar: 10 }, { headers: { foo: 'baz' } });
});
resource.head({ bar: 10 }, { headers: { foo: 'baz' } });
});
});
});
describe('#post', function() {
var descriptor = {
_links: {
self: {
href: '/v1/foo{?baz}'
}
describe('#post', function() {
var descriptor = {
_links: {
self: {
href: '/v1/foo{?baz}'
}
};
var result = {
hello: 'world'
};
}
};
var result = {
hello: 'world'
};
var responses = {
foo: [200, { "Content-Type": "application/json" }, JSON.stringify(result)],
};
var responses = {
foo: [200, { 'Content-Type': 'application/json' }, JSON.stringify(result)],
};
it('returns the result of sending a POST request to the resource', function() {
it('returns the result of sending a POST request to the resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('POST', '/v1/foo', responses.foo);
resource.post()
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
describe('when given a data object', function() {
it('returns the result of sending a POST request with data to resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('POST', '/v1/foo', responses.foo);
return expect(resource.post().then(parseResponse)).to.become(result);
});
describe('when given a data object', function() {
it('returns the result of sending a POST request with data to resource', function() {
var resource = new Resource(descriptor);
server.respondWith('POST', '/v1/foo', function(req) {
expect(req.requestBody).toEqual('{"bar":10}');
req.respond.apply(req, responses.foo);
});
server.respondWith('POST', '/v1/foo', function(req) {
expect(req.requestBody).to.equal('{"bar":10}');
req.respond.apply(req, responses.foo);
});
return expect(resource.post(JSON.stringify({ bar: 10 })).then(parseResponse)).to.become(result);
});
resource.post(JSON.stringify({ bar: 10 }))
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given a params object', function() {
it('returns the result of sending a POST request to the templated resource', function() {
var resource = new Resource(descriptor);
server.respondWith('POST', '/v1/foo?baz=10', responses.foo);
return expect(resource.post(null, { baz: 10 }).then(parseResponse)).to.become(result);
});
describe('when given a params object', function() {
it('returns the result of sending a POST request to the templated resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('POST', '/v1/foo?baz=10', responses.foo);
resource.post(null, { baz: 10 })
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given a params object and data object', function() {
it('returns the result of sending a POST request with data to the templated resource', function() {
var resource = new Resource(descriptor);
describe('when given a params object and data object', function() {
it('returns the result of sending a POST request with data to the templated resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('POST', '/v1/foo?baz=10', function(req) {
expect(req.requestBody).to.equal('{"bar":10}');
req.respond.apply(req, responses.foo);
});
server.respondWith('POST', '/v1/foo?baz=10', function(req) {
expect(req.requestBody).toEqual('{"bar":10}');
req.respond.apply(req, responses.foo);
});
return expect(resource.post(JSON.stringify({ bar: 10 }), { baz: 10 }).then(parseResponse)).to.become(result);
});
resource.post(JSON.stringify({ bar: 10 }), { baz: 10 })
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given resource level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
describe('when given resource level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('POST', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('POST', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.post(JSON.stringify({ bar: 10 }));
});
resource.post(JSON.stringify({ bar: 10 }));
});
});
describe('when given request level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor);
describe('when given request level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor);
server.respondWith('POST', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('POST', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.post(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'bar' } });
});
resource.post(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'bar' } });
});
});
describe('when given resource and request level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
describe('when given resource and request level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('POST', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('baz');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('POST', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('baz');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.post(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'baz' } });
});
resource.post(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'baz' } });
});
});
});
describe('#put', function() {
var descriptor = {
_links: {
self: {
href: '/v1/foo{?baz}'
}
describe('#put', function() {
var descriptor = {
_links: {
self: {
href: '/v1/foo{?baz}'
}
};
var result = {
hello: 'world'
};
}
};
var result = {
hello: 'world'
};
var responses = {
foo: [200, { "Content-Type": "application/json" }, JSON.stringify(result)],
};
var responses = {
foo: [200, { 'Content-Type': 'application/json' }, JSON.stringify(result)],
};
it('returns the result of sending a PUT request to the resource', function() {
var resource = new Resource(descriptor);
server.respondWith('PUT', '/v1/foo', responses.foo);
return expect(resource.put().then(parseResponse)).to.become(result);
});
it('returns the result of sending a PUT request to the resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('PUT', '/v1/foo', responses.foo);
describe('when given a data object', function() {
it('returns the result of sending a PUT request with data to resource', function() {
var resource = new Resource(descriptor);
resource.put()
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
server.respondWith('PUT', '/v1/foo', function(req) {
expect(req.requestBody).to.equal('{"bar":10}');
req.respond.apply(req, responses.foo);
});
describe('when given a data object', function() {
it('returns the result of sending a PUT request with data to resource', function(done) {
var resource = new Resource(descriptor);
return expect(resource.put(JSON.stringify({ bar: 10 })).then(parseResponse)).to.become(result);
server.respondWith('PUT', '/v1/foo', function(req) {
expect(req.requestBody).toEqual('{"bar":10}');
req.respond.apply(req, responses.foo);
});
resource.put(JSON.stringify({ bar: 10 }))
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given a params object', function() {
it('returns the result of sending a PUT request to the templated resource', function() {
var resource = new Resource(descriptor);
server.respondWith('PUT', '/v1/foo?baz=10', responses.foo);
return expect(resource.put(null, { baz: 10 }).then(parseResponse)).to.become(result);
});
describe('when given a params object', function() {
it('returns the result of sending a PUT request to the templated resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('PUT', '/v1/foo?baz=10', responses.foo);
resource.put(null, { baz: 10 })
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given a params object and data object', function() {
it('returns the result of sending a PUT request with data to the templated resource', function() {
var resource = new Resource(descriptor);
describe('when given a params object and data object', function() {
it('returns the result of sending a PUT request with data to the templated resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('PUT', '/v1/foo?baz=10', function(req) {
expect(req.requestBody).to.equal('{"bar":10}');
req.respond.apply(req, responses.foo);
});
server.respondWith('PUT', '/v1/foo?baz=10', function(req) {
expect(req.requestBody).toEqual('{"bar":10}');
req.respond.apply(req, responses.foo);
});
return expect(resource.put(JSON.stringify({ bar: 10 }), { baz: 10 }).then(parseResponse)).to.become(result);
});
resource.put(JSON.stringify({ bar: 10 }), { baz: 10 })
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given resource level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
describe('when given resource level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('PUT', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('PUT', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.put(JSON.stringify({ bar: 10 }));
});
resource.put(JSON.stringify({ bar: 10 }));
});
});
describe('when given request level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor);
describe('when given request level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor);
server.respondWith('PUT', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('PUT', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.put(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'bar' } });
});
resource.put(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'bar' } });
});
});
describe('when given resource and request level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
describe('when given resource and request level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('PUT', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('baz');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('PUT', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('baz');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.put(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'baz' } });
});
resource.put(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'baz' } });
});
});
});
describe('#patch', function() {
var descriptor = {
_links: {
self: {
href: '/v1/foo{?baz}'
}
describe('#patch', function() {
var descriptor = {
_links: {
self: {
href: '/v1/foo{?baz}'
}
};
var result = {
hello: 'world'
};
}
};
var result = {
hello: 'world'
};
var responses = {
foo: [200, { "Content-Type": "application/json" }, JSON.stringify(result)],
};
var responses = {
foo: [200, { 'Content-Type': 'application/json' }, JSON.stringify(result)],
};
it('returns the result of sending a PATCH request to the resource', function() {
var resource = new Resource(descriptor);
server.respondWith('PATCH', '/v1/foo', responses.foo);
return expect(resource.patch().then(parseResponse)).to.become(result);
});
it('returns the result of sending a PATCH request to the resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('PATCH', '/v1/foo', responses.foo);
describe('when given a data object', function() {
it('returns the result of sending a PATCH request with data to resource', function() {
var resource = new Resource(descriptor);
resource.patch()
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
server.respondWith('PATCH', '/v1/foo', function(req) {
expect(req.requestBody).to.equal('{"bar":10}');
req.respond.apply(req, responses.foo);
});
describe('when given a data object', function() {
it('returns the result of sending a PATCH request with data to resource', function(done) {
var resource = new Resource(descriptor);
return expect(resource.patch(JSON.stringify({ bar: 10 })).then(parseResponse)).to.become(result);
server.respondWith('PATCH', '/v1/foo', function(req) {
expect(req.requestBody).toEqual('{"bar":10}');
req.respond.apply(req, responses.foo);
});
resource.patch(JSON.stringify({ bar: 10 }))
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given a params object', function() {
it('returns the result of sending a PATCH request to the templated resource', function() {
var resource = new Resource(descriptor);
server.respondWith('PATCH', '/v1/foo?baz=10', responses.foo);
return expect(resource.patch(null, { baz: 10 }).then(parseResponse)).to.become(result);
});
describe('when given a params object', function() {
it('returns the result of sending a PATCH request to the templated resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('PATCH', '/v1/foo?baz=10', responses.foo);
resource.patch(null, { baz: 10 })
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given a params object and data object', function() {
it('returns the result of sending a PATCH request with data to the templated resource', function() {
var resource = new Resource(descriptor);
describe('when given a params object and data object', function() {
it('returns the result of sending a PATCH request with data to the templated resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('PATCH', '/v1/foo?baz=10', function(req) {
expect(req.requestBody).to.equal('{"bar":10}');
req.respond.apply(req, responses.foo);
});
server.respondWith('PATCH', '/v1/foo?baz=10', function(req) {
expect(req.requestBody).toEqual('{"bar":10}');
req.respond.apply(req, responses.foo);
});
return expect(resource.patch(JSON.stringify({ bar: 10 }), { baz: 10 }).then(parseResponse)).to.become(result);
});
resource.patch(JSON.stringify({ bar: 10 }), { baz: 10 })
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given resource level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
describe('when given resource level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('PATCH', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('PATCH', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.patch(JSON.stringify({ bar: 10 }));
});
resource.patch(JSON.stringify({ bar: 10 }));
});
});
describe('when given request level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor);
describe('when given request level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor);
server.respondWith('PATCH', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('PATCH', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.patch(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'bar' } });
});
resource.patch(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'bar' } });
});
});
describe('when given resource and request level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
describe('when given resource and request level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('PATCH', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('baz');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('PATCH', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('baz');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.patch(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'baz' } });
});
resource.patch(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'baz' } });
});
});
});
describe('#delete', function() {
var descriptor = {
_links: {
self: {
href: '/v1/foo{?baz}'
}
describe('#delete', function() {
var descriptor = {
_links: {
self: {
href: '/v1/foo{?baz}'
}
};
var result = {
hello: 'world'
};
}
};
var result = {
hello: 'world'
};
var responses = {
foo: [200, { "Content-Type": "application/json" }, JSON.stringify(result)],
};
var responses = {
foo: [200, { 'Content-Type': 'application/json' }, JSON.stringify(result)],
};
it('returns the result of sending a DELETE request to the resource', function() {
var resource = new Resource(descriptor);
server.respondWith('DELETE', '/v1/foo', responses.foo);
return expect(resource.delete().then(parseResponse)).to.become(result);
});
it('returns the result of sending a DELETE request to the resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('DELETE', '/v1/foo', responses.foo);
describe('when given a data object', function() {
it('returns the result of sending a DELETE request with data to resource', function() {
var resource = new Resource(descriptor);
resource.delete()
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
server.respondWith('DELETE', '/v1/foo', function(req) {
expect(req.requestBody).to.equal('{"bar":10}');
req.respond.apply(req, responses.foo);
});
describe('when given a data object', function() {
it('returns the result of sending a DELETE request with data to resource', function(done) {
var resource = new Resource(descriptor);
return expect(resource.delete(JSON.stringify({ bar: 10 })).then(parseResponse)).to.become(result);
server.respondWith('DELETE', '/v1/foo', function(req) {
expect(req.requestBody).toEqual('{"bar":10}');
req.respond.apply(req, responses.foo);
});
resource.delete(JSON.stringify({ bar: 10 }))
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given a params object', function() {
it('returns the result of sending a DELETE request to the templated resource', function() {
var resource = new Resource(descriptor);
server.respondWith('DELETE', '/v1/foo?baz=10', responses.foo);
return expect(resource.delete(null, { baz: 10 }).then(parseResponse)).to.become(result);
});
describe('when given a params object', function() {
it('returns the result of sending a DELETE request to the templated resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('DELETE', '/v1/foo?baz=10', responses.foo);
resource.delete(null, { baz: 10 })
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given a params object and JSON data', function() {
it('returns the result of sending a DELETE request with data to the templated resource', function() {
var resource = new Resource(descriptor);
describe('when given a params object and JSON data', function() {
it('returns the result of sending a DELETE request with data to the templated resource', function(done) {
var resource = new Resource(descriptor);
server.respondWith('DELETE', '/v1/foo?baz=10', function(req) {
expect(req.requestBody).to.equal('{"bar":10}');
req.respond.apply(req, responses.foo);
});
server.respondWith('DELETE', '/v1/foo?baz=10', function(req) {
expect(req.requestBody).toEqual('{"bar":10}');
req.respond.apply(req, responses.foo);
});
return expect(resource.delete(JSON.stringify({ bar: 10 }), { baz: 10 }).then(parseResponse)).to.become(result);
});
resource.delete(JSON.stringify({ bar: 10 }), { baz: 10 })
.then((res) => res.json())
.then((res) => {
expect(res).toEqual(result);
})
.then(done, done.fail);
});
});
describe('when given resource level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
describe('when given resource level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('DELETE', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('DELETE', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.delete(JSON.stringify({ bar: 10 }));
});
resource.delete(JSON.stringify({ bar: 10 }));
});
});
describe('when given request level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor);
describe('when given request level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor);
server.respondWith('DELETE', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('DELETE', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.delete(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'bar' } });
});
resource.delete(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'bar' } });
});
});
describe('when given resource and request level options', function() {
it('passes those options to the ajax request method', function() {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
describe('when given resource and request level options', function() {
it('passes those options to the ajax request method', function(done) {
var resource = new Resource(descriptor, { headers: { foo: 'bar' } });
server.respondWith('DELETE', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('baz');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('DELETE', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('baz');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return resource.delete(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'baz' } });
});
resource.delete(JSON.stringify({ bar: 10 }), {}, { headers: { foo: 'baz' } });
});
});
});
});
describe('when multiple requests are made with the same root options', function() {
var server;
var descriptor = {
_links: {
self: {
href: '/v1/foo{?baz}'
}
describe('when multiple requests are made with the same root options', function() {
var server;
var descriptor = {
_links: {
self: {
href: '/v1/foo{?baz}'
}
};
var otherDescriptor = {
_links: {
self: {
href: '/v1/bar{?baz}'
}
}
};
var otherDescriptor = {
_links: {
self: {
href: '/v1/bar{?baz}'
}
};
}
};
beforeEach(function() {
server = sinon.fakeServer.create();
server.autoRespond = true;
});
beforeEach(function() {
server = sinon.fakeServer.create();
server.autoRespond = true;
});
afterEach(function() {
server.restore();
server = null;
});
afterEach(function() {
server.restore();
server = null;
});
it('does not modify the root options object', function() {
var options = { headers: { foo: 'bar' } };
var resource = new Resource(descriptor, options);
var resourceB = new Resource(otherDescriptor, options);
it('does not modify the root options object', function(done) {
var options = { headers: { foo: 'bar' } };
var resource = new Resource(descriptor, options);
var resourceB = new Resource(otherDescriptor, options);
server.respondWith('GET', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).to.equal('baz');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('GET', '/v1/foo', function(req) {
expect(req.requestHeaders.foo).toEqual('baz');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
});
server.respondWith('GET', '/v1/bar', function(req) {
expect(req.requestHeaders.foo).to.equal('bar');
req.respond(200, { "Content-Type": "text/plain" }, '');
});
server.respondWith('GET', '/v1/bar', function(req) {
expect(req.requestHeaders.foo).toEqual('bar');
req.respond(200, { 'Content-Type': 'text/plain' }, '');
});
return resource.get({}, { headers: { foo: 'baz' } })
.then(function() { return resourceB.get(); });
});
resource.get({}, { headers: { foo: 'baz' } })
.then(function() { return resourceB.get(); })
.then(done, done.fail);
});
});
});

@@ -1,168 +0,168 @@

define(['lib/uri'], function(Uri) {
describe('Uri', function() {
it('can be instantiated', function() {
expect(new Uri()).to.be.an.instanceOf(Uri);
});
import Uri from 'src/uri';
describe('#parse', function() {
describe('when given no uri', function() {
var uri = '',
emptyUri = Uri.parse(uri);
describe('Uri', function() {
it('can be instantiated', function() {
expect(new Uri() instanceof Uri).toBeTruthy();
});
it('has no a protocol', function() {
expect(emptyUri.protocol).to.equal('');
});
describe('#parse', function() {
describe('when given no uri', function() {
const uri = '';
const emptyUri = Uri.parse(uri);
it('has no a user', function() {
expect(emptyUri.user).to.equal('');
});
it('has no a protocol', function() {
expect(emptyUri.protocol).toEqual('');
});
it('has no a password', function() {
expect(emptyUri.password).to.equal('');
});
it('has no a user', function() {
expect(emptyUri.user).toEqual('');
});
it('has no a host', function() {
expect(emptyUri.host).to.equal('');
});
it('has no a password', function() {
expect(emptyUri.password).toEqual('');
});
it('has no a port', function() {
expect(emptyUri.port).to.equal('');
});
it('has no a host', function() {
expect(emptyUri.host).toEqual('');
});
it('has no a path', function() {
expect(emptyUri.path).to.equal('');
});
it('has no a port', function() {
expect(emptyUri.port).toEqual('');
});
it('has no a query', function() {
expect(emptyUri.query).to.equal('');
});
it('has no a path', function() {
expect(emptyUri.path).toEqual('');
});
it('has no a fragment', function() {
expect(emptyUri.fragment).to.equal('');
});
it('has no a query', function() {
expect(emptyUri.query).toEqual('');
});
it('has no userInfo', function() {
expect(emptyUri.userInfo()).to.equal('');
});
it('has no a fragment', function() {
expect(emptyUri.fragment).toEqual('');
});
it('has no authority', function() {
expect(emptyUri.authority()).to.equal('');
});
it('has no userInfo', function() {
expect(emptyUri.userInfo()).toEqual('');
});
it('has no site', function() {
expect(emptyUri.site()).to.equal('');
});
it('has no authority', function() {
expect(emptyUri.authority()).toEqual('');
});
it('toString matches original uri string', function() {
expect(emptyUri.toString()).to.equal(uri);
});
it('has no site', function() {
expect(emptyUri.site()).toEqual('');
});
describe('when given a full uri', function() {
var uri = 'http://user:password@example.com:8080/path?query=value#fragment',
fullUri = Uri.parse(uri);
it('toString matches original uri string', function() {
expect(emptyUri.toString()).toEqual(uri);
});
});
it('has a protocol', function() {
expect(fullUri.protocol).to.equal('http');
});
describe('when given a full uri', function() {
const uri = 'http://user:password@example.com:8080/path?query=value#fragment';
const fullUri = Uri.parse(uri);
it('has a user', function() {
expect(fullUri.user).to.equal('user');
});
it('has a protocol', function() {
expect(fullUri.protocol).toEqual('http');
});
it('has a password', function() {
expect(fullUri.password).to.equal('password');
});
it('has a user', function() {
expect(fullUri.user).toEqual('user');
});
it('has a host', function() {
expect(fullUri.host).to.equal('example.com');
});
it('has a password', function() {
expect(fullUri.password).toEqual('password');
});
it('has a port', function() {
expect(fullUri.port).to.equal('8080');
});
it('has a host', function() {
expect(fullUri.host).toEqual('example.com');
});
it('has a path', function() {
expect(fullUri.path).to.equal('/path');
});
it('has a port', function() {
expect(fullUri.port).toEqual('8080');
});
it('has a query', function() {
expect(fullUri.query).to.equal('query=value');
});
it('has a path', function() {
expect(fullUri.path).toEqual('/path');
});
it('has a fragment', function() {
expect(fullUri.fragment).to.equal('fragment');
});
it('has a query', function() {
expect(fullUri.query).toEqual('query=value');
});
it('has userInfo', function() {
expect(fullUri.userInfo()).to.equal('user:password');
});
it('has a fragment', function() {
expect(fullUri.fragment).toEqual('fragment');
});
it('has an authority', function() {
expect(fullUri.authority()).to.equal('user:password@example.com:8080');
});
it('has userInfo', function() {
expect(fullUri.userInfo()).toEqual('user:password');
});
it('has a site', function() {
expect(fullUri.site()).to.equal('http://user:password@example.com:8080');
});
it('has an authority', function() {
expect(fullUri.authority()).toEqual('user:password@example.com:8080');
});
it('toString matches original uri string', function() {
expect(fullUri.toString()).to.equal(uri);
});
it('has a site', function() {
expect(fullUri.site()).toEqual('http://user:password@example.com:8080');
});
describe('when created with string components', function() {
var uri = 'http://example.com',
stringUri = new Uri({ protocol: 'http', host: 'example.com' });
it('toString matches original uri string', function() {
expect(fullUri.toString()).toEqual(uri);
});
});
it('has a site value of "http://example.com"', function() {
expect(stringUri.site()).to.equal(uri);
});
describe('when created with string components', function() {
const uri = 'http://example.com';
const stringUri = new Uri({ protocol: 'http', host: 'example.com' });
it('is equivalent to the parsed URI', function() {
expect(stringUri).to.deep.equal(Uri.parse(uri));
});
it('has a site value of "http://example.com"', function() {
expect(stringUri.site()).toEqual(uri);
});
describe('when given only an authority', function() {
var authorityUri = new Uri({
user: 'user',
host: 'example.com'
});
it('is equivalent to the parsed URI', function() {
expect(stringUri).toEqual(Uri.parse(uri));
});
});
it('does not infer the protocol', function() {
expect(authorityUri.port).to.equal('');
});
describe('when given only an authority', function() {
var authorityUri = new Uri({
user: 'user',
host: 'example.com'
});
it('does not inder the port', function() {
expect(authorityUri.port).to.equal('');
});
it('does not infer the protocol', function() {
expect(authorityUri.port).toEqual('');
});
it('has a protocol relative site value of "//user@example.com"', function() {
expect(authorityUri.site()).to.equal('//user@example.com');
});
it('does not inder the port', function() {
expect(authorityUri.port).toEqual('');
});
});
describe('#encodeComponent', function() {
it('returns the component in percent encoding', function() {
expect(Uri.encodeComponent('Hello World')).to.equal('Hello%20World');
it('has a protocol relative site value of "//user@example.com"', function() {
expect(authorityUri.site()).toEqual('//user@example.com');
});
});
});
describe('when encoding a string with an existing encoding', function() {
it('returns the correct percent encoded string', function() {
expect(Uri.encodeComponent('JK%4c', '0-9A-IKM-Za-z%')).to.equal('%4AK%4c');
});
describe('#encodeComponent', function() {
it('returns the component in percent encoding', function() {
expect(Uri.encodeComponent('Hello World')).toEqual('Hello%20World');
});
describe('when encoding a string with an existing encoding', function() {
it('returns the correct percent encoded string', function() {
expect(Uri.encodeComponent('JK%4c', '0-9A-IKM-Za-z%')).toEqual('%4AK%4c');
});
});
describe('when encoding a multibyte string', function() {
it('returns the correct percent encoded string', function() {
expect(Uri.encodeComponent('günther')).to.equal('g%C3%BCnther');
});
describe('when encoding a multibyte string', function() {
it('returns the correct percent encoded string', function() {
expect(Uri.encodeComponent('günther')).toEqual('g%C3%BCnther');
});
});
describe('when encoding a string with ASCII chars 0-15', function() {
it('returns the correct percent encoded string', function() {
expect(Uri.encodeComponent('one\ntwo')).to.equal('one%0Atwo');
});
describe('when encoding a string with ASCII chars 0-15', function() {
it('returns the correct percent encoded string', function() {
expect(Uri.encodeComponent('one\ntwo')).toEqual('one%0Atwo');
});

@@ -169,0 +169,0 @@ });

@@ -1,259 +0,256 @@

define(['lib/uritemplate'], function(UriTemplate) {
function generateTests(testcases, variables) {
testcases.forEach(function(testcase) {
var template = testcase[0],
result = testcase[1];
import UriTemplate from 'src/uritemplate';
it('"' + template + '" expands to "' + result + '"', function() {
var uri = new UriTemplate(template);
function generateTests(testcases, variables) {
testcases.forEach(function([template, result]) {
it(`"${template}" expands to "${result}"`, function() {
var uri = new UriTemplate(template);
if (Array.isArray(result)) {
expect(result).to.include(uri.expand(variables).toString());
}
else {
expect(uri.expand(variables).toString()).to.equal(result);
}
});
if (Array.isArray(result)) {
expect(result).toContain(uri.expand(variables).toString());
}
else {
expect(uri.expand(variables).toString()).toEqual(result);
}
});
}
});
}
describe('UrlTemplate', function() {
describe('Level 1 Examples', function() {
var variables = {
var: "value",
hello: "Hello World!"
},
testcases = [
["{var}", "value"],
["{hello}", "Hello%20World%21"]
];
describe('UrlTemplate', function() {
describe('Level 1 Examples', function() {
const variables = {
var: 'value',
hello: 'Hello World!'
};
const testcases = [
['{var}', 'value'],
['{hello}', 'Hello%20World%21']
];
generateTests(testcases, variables);
});
generateTests(testcases, variables);
});
describe('Level 2 Examples', function() {
var variables = {
var: "value",
hello: "Hello World!",
path: "/foo/bar"
},
testcases = [
["{+var}", "value"],
["{+hello}", "Hello%20World!"],
["{+path}/here", "/foo/bar/here"],
["here?ref={+path}", "here?ref=/foo/bar"]
];
describe('Level 2 Examples', function() {
const variables = {
var: 'value',
hello: 'Hello World!',
path: '/foo/bar'
};
const testcases = [
['{+var}', 'value'],
['{+hello}', 'Hello%20World!'],
['{+path}/here', '/foo/bar/here'],
['here?ref={+path}', 'here?ref=/foo/bar']
];
generateTests(testcases, variables);
});
generateTests(testcases, variables);
});
describe('Level 3 Examples', function() {
var variables = {
count: ["one", "two", "three"],
dom: ["example", "com"],
dub: "me/too",
hello: "Hello World!",
half: "50%",
var: "value",
who: "fred",
base: "http://example.com/home/",
path: "/foo/bar",
list: ["red", "green", "blue"],
keys: { semi: ';', dot: '.', comma: ',' },
v: "6",
x: "1024",
y: "768",
empty: "",
empty_keys: [],
undef: null
},
testcases = [
["map?{x,y}", "map?1024,768"],
["{x,hello,y}", "1024,Hello%20World%21,768"],
["{+x,hello,y}", "1024,Hello%20World!,768"],
["{+path,x}/here", "/foo/bar,1024/here"],
["{#x,hello,y}", "#1024,Hello%20World!,768"],
["{#path,x}/here", "#/foo/bar,1024/here"],
["X{.var}", "X.value"],
["X{.x,y}", "X.1024.768"],
["X{.empty}", "X."],
["X{.undef}", "X"],
["X{.empty_keys}", "X"],
["X{.empty_keys*}", "X"],
["X{.undef}", "X"],
["{/empty}", "/"],
["{/undef}", ""],
["{/var}", "/value"],
["{/var,x}/here", "/value/1024/here"],
["{;x,y}", ";x=1024;y=768"],
["{;x,y,empty}", ";x=1024;y=768;empty"],
["{?x,y}", "?x=1024&y=768"],
["{?x,y,empty}", "?x=1024&y=768&empty="],
["?fixed=yes{&x}", "?fixed=yes&x=1024"],
["{&x,y,empty}", "&x=1024&y=768&empty="]
];
describe('Level 3 Examples', function() {
const variables = {
count: ['one', 'two', 'three'],
dom: ['example', 'com'],
dub: 'me/too',
hello: 'Hello World!',
half: '50%',
var: 'value',
who: 'fred',
base: 'http://example.com/home/',
path: '/foo/bar',
list: ['red', 'green', 'blue'],
keys: { semi: ';', dot: '.', comma: ',' },
v: '6',
x: '1024',
y: '768',
empty: '',
empty_keys: [],
undef: null
};
const testcases = [
['map?{x,y}', 'map?1024,768'],
['{x,hello,y}', '1024,Hello%20World%21,768'],
['{+x,hello,y}', '1024,Hello%20World!,768'],
['{+path,x}/here', '/foo/bar,1024/here'],
['{#x,hello,y}', '#1024,Hello%20World!,768'],
['{#path,x}/here', '#/foo/bar,1024/here'],
['X{.var}', 'X.value'],
['X{.x,y}', 'X.1024.768'],
['X{.empty}', 'X.'],
['X{.undef}', 'X'],
['X{.empty_keys}', 'X'],
['X{.empty_keys*}', 'X'],
['X{.undef}', 'X'],
['{/empty}', '/'],
['{/undef}', ''],
['{/var}', '/value'],
['{/var,x}/here', '/value/1024/here'],
['{;x,y}', ';x=1024;y=768'],
['{;x,y,empty}', ';x=1024;y=768;empty'],
['{?x,y}', '?x=1024&y=768'],
['{?x,y,empty}', '?x=1024&y=768&empty='],
['?fixed=yes{&x}', '?fixed=yes&x=1024'],
['{&x,y,empty}', '&x=1024&y=768&empty=']
];
generateTests(testcases, variables);
});
generateTests(testcases, variables);
});
describe('Level 4 Examples', function() {
var variables = {
var: "value",
hello: "Hello World!",
path: "/foo/bar",
list: ["red", "green", "blue"],
keys: { semi: ";", dot: ".", comma: "," }
},
testcases = [
["{var:3}", "val"],
["{var:30}", "value"],
["{list}", "red,green,blue"],
["{list*}", "red,green,blue"],
["{keys}", [
"comma,%2C,dot,.,semi,%3B",
"comma,%2C,semi,%3B,dot,.",
"dot,.,comma,%2C,semi,%3B",
"dot,.,semi,%3B,comma,%2C",
"semi,%3B,comma,%2C,dot,.",
"semi,%3B,dot,.,comma,%2C"
]],
["{keys*}", [
"comma=%2C,dot=.,semi=%3B",
"comma=%2C,semi=%3B,dot=.",
"dot=.,comma=%2C,semi=%3B",
"dot=.,semi=%3B,comma=%2C",
"semi=%3B,comma=%2C,dot=.",
"semi=%3B,dot=.,comma=%2C"
]],
["{+path:6}/here", "/foo/b/here"],
["{+list}", "red,green,blue"],
["{+list*}", "red,green,blue"],
["{+keys}", [
"comma,,,dot,.,semi,;",
"comma,,,semi,;,dot,.",
"dot,.,comma,,,semi,;",
"dot,.,semi,;,comma,,",
"semi,;,comma,,,dot,.",
"semi,;,dot,.,comma,,"
]],
["{+keys*}", [
"comma=,,dot=.,semi=;",
"comma=,,semi=;,dot=.",
"dot=.,comma=,,semi=;",
"dot=.,semi=;,comma=,",
"semi=;,comma=,,dot=.",
"semi=;,dot=.,comma=,"
]],
["{#path:6}/here", "#/foo/b/here"],
["{#list}", "#red,green,blue"],
["{#list*}", "#red,green,blue"],
["{#keys}", [
"#comma,,,dot,.,semi,;",
"#comma,,,semi,;,dot,.",
"#dot,.,comma,,,semi,;",
"#dot,.,semi,;,comma,,",
"#semi,;,comma,,,dot,.",
"#semi,;,dot,.,comma,,"
]],
["{#keys*}", [
"#comma=,,dot=.,semi=;",
"#comma=,,semi=;,dot=.",
"#dot=.,comma=,,semi=;",
"#dot=.,semi=;,comma=,",
"#semi=;,comma=,,dot=.",
"#semi=;,dot=.,comma=,"
]],
["X{.var:3}", "X.val"],
["X{.list}", "X.red,green,blue"],
["X{.list*}", "X.red.green.blue"],
["X{.keys}", [
"X.comma,%2C,dot,.,semi,%3B",
"X.comma,%2C,semi,%3B,dot,.",
"X.dot,.,comma,%2C,semi,%3B",
"X.dot,.,semi,%3B,comma,%2C",
"X.semi,%3B,comma,%2C,dot,.",
"X.semi,%3B,dot,.,comma,%2C"
]],
["{/var:1,var}", "/v/value"],
["{/list}", "/red,green,blue"],
["{/list*}", "/red/green/blue"],
["{/list*,path:4}", "/red/green/blue/%2Ffoo"],
["{/keys}", [
"/comma,%2C,dot,.,semi,%3B",
"/comma,%2C,semi,%3B,dot,.",
"/dot,.,comma,%2C,semi,%3B",
"/dot,.,semi,%3B,comma,%2C",
"/semi,%3B,comma,%2C,dot,.",
"/semi,%3B,dot,.,comma,%2C"
]],
["{/keys*}", [
"/comma=%2C/dot=./semi=%3B",
"/comma=%2C/semi=%3B/dot=.",
"/dot=./comma=%2C/semi=%3B",
"/dot=./semi=%3B/comma=%2C",
"/semi=%3B/comma=%2C/dot=.",
"/semi=%3B/dot=./comma=%2C"
]],
["{;hello:5}", ";hello=Hello"],
["{;list}", ";list=red,green,blue"],
["{;list*}", ";list=red;list=green;list=blue"],
["{;keys}", [
";keys=comma,%2C,dot,.,semi,%3B",
";keys=comma,%2C,semi,%3B,dot,.",
";keys=dot,.,comma,%2C,semi,%3B",
";keys=dot,.,semi,%3B,comma,%2C",
";keys=semi,%3B,comma,%2C,dot,.",
";keys=semi,%3B,dot,.,comma,%2C"
]],
["{;keys*}", [
";comma=%2C;dot=.;semi=%3B",
";comma=%2C;semi=%3B;dot=.",
";dot=.;comma=%2C;semi=%3B",
";dot=.;semi=%3B;comma=%2C",
";semi=%3B;comma=%2C;dot=.",
";semi=%3B;dot=.;comma=%2C"
]],
["{?var:3}", "?var=val"],
["{?list}", "?list=red,green,blue"],
["{?list*}", "?list=red&list=green&list=blue"],
["{?keys}", [
"?keys=comma,%2C,dot,.,semi,%3B",
"?keys=comma,%2C,semi,%3B,dot,.",
"?keys=dot,.,comma,%2C,semi,%3B",
"?keys=dot,.,semi,%3B,comma,%2C",
"?keys=semi,%3B,comma,%2C,dot,.",
"?keys=semi,%3B,dot,.,comma,%2C"
]],
["{?keys*}", [
"?comma=%2C&dot=.&semi=%3B",
"?comma=%2C&semi=%3B&dot=.",
"?dot=.&comma=%2C&semi=%3B",
"?dot=.&semi=%3B&comma=%2C",
"?semi=%3B&comma=%2C&dot=.",
"?semi=%3B&dot=.&comma=%2C"
]],
["{&var:3}", "&var=val"],
["{&list}", "&list=red,green,blue"],
["{&list*}", "&list=red&list=green&list=blue"],
["{&keys}", [
"&keys=comma,%2C,dot,.,semi,%3B",
"&keys=comma,%2C,semi,%3B,dot,.",
"&keys=dot,.,comma,%2C,semi,%3B",
"&keys=dot,.,semi,%3B,comma,%2C",
"&keys=semi,%3B,comma,%2C,dot,.",
"&keys=semi,%3B,dot,.,comma,%2C"
]],
["{&keys*}", [
"&comma=%2C&dot=.&semi=%3B",
"&comma=%2C&semi=%3B&dot=.",
"&dot=.&comma=%2C&semi=%3B",
"&dot=.&semi=%3B&comma=%2C",
"&semi=%3B&comma=%2C&dot=.",
"&semi=%3B&dot=.&comma=%2C"
]]
];
describe('Level 4 Examples', function() {
const variables = {
var: 'value',
hello: 'Hello World!',
path: '/foo/bar',
list: ['red', 'green', 'blue'],
keys: { semi: ';', dot: '.', comma: ',' }
};
const testcases = [
['{var:3}', 'val'],
['{var:30}', 'value'],
['{list}', 'red,green,blue'],
['{list*}', 'red,green,blue'],
['{keys}', [
'comma,%2C,dot,.,semi,%3B',
'comma,%2C,semi,%3B,dot,.',
'dot,.,comma,%2C,semi,%3B',
'dot,.,semi,%3B,comma,%2C',
'semi,%3B,comma,%2C,dot,.',
'semi,%3B,dot,.,comma,%2C'
]],
['{keys*}', [
'comma=%2C,dot=.,semi=%3B',
'comma=%2C,semi=%3B,dot=.',
'dot=.,comma=%2C,semi=%3B',
'dot=.,semi=%3B,comma=%2C',
'semi=%3B,comma=%2C,dot=.',
'semi=%3B,dot=.,comma=%2C'
]],
['{+path:6}/here', '/foo/b/here'],
['{+list}', 'red,green,blue'],
['{+list*}', 'red,green,blue'],
['{+keys}', [
'comma,,,dot,.,semi,;',
'comma,,,semi,;,dot,.',
'dot,.,comma,,,semi,;',
'dot,.,semi,;,comma,,',
'semi,;,comma,,,dot,.',
'semi,;,dot,.,comma,,'
]],
['{+keys*}', [
'comma=,,dot=.,semi=;',
'comma=,,semi=;,dot=.',
'dot=.,comma=,,semi=;',
'dot=.,semi=;,comma=,',
'semi=;,comma=,,dot=.',
'semi=;,dot=.,comma=,'
]],
['{#path:6}/here', '#/foo/b/here'],
['{#list}', '#red,green,blue'],
['{#list*}', '#red,green,blue'],
['{#keys}', [
'#comma,,,dot,.,semi,;',
'#comma,,,semi,;,dot,.',
'#dot,.,comma,,,semi,;',
'#dot,.,semi,;,comma,,',
'#semi,;,comma,,,dot,.',
'#semi,;,dot,.,comma,,'
]],
['{#keys*}', [
'#comma=,,dot=.,semi=;',
'#comma=,,semi=;,dot=.',
'#dot=.,comma=,,semi=;',
'#dot=.,semi=;,comma=,',
'#semi=;,comma=,,dot=.',
'#semi=;,dot=.,comma=,'
]],
['X{.var:3}', 'X.val'],
['X{.list}', 'X.red,green,blue'],
['X{.list*}', 'X.red.green.blue'],
['X{.keys}', [
'X.comma,%2C,dot,.,semi,%3B',
'X.comma,%2C,semi,%3B,dot,.',
'X.dot,.,comma,%2C,semi,%3B',
'X.dot,.,semi,%3B,comma,%2C',
'X.semi,%3B,comma,%2C,dot,.',
'X.semi,%3B,dot,.,comma,%2C'
]],
['{/var:1,var}', '/v/value'],
['{/list}', '/red,green,blue'],
['{/list*}', '/red/green/blue'],
['{/list*,path:4}', '/red/green/blue/%2Ffoo'],
['{/keys}', [
'/comma,%2C,dot,.,semi,%3B',
'/comma,%2C,semi,%3B,dot,.',
'/dot,.,comma,%2C,semi,%3B',
'/dot,.,semi,%3B,comma,%2C',
'/semi,%3B,comma,%2C,dot,.',
'/semi,%3B,dot,.,comma,%2C'
]],
['{/keys*}', [
'/comma=%2C/dot=./semi=%3B',
'/comma=%2C/semi=%3B/dot=.',
'/dot=./comma=%2C/semi=%3B',
'/dot=./semi=%3B/comma=%2C',
'/semi=%3B/comma=%2C/dot=.',
'/semi=%3B/dot=./comma=%2C'
]],
['{;hello:5}', ';hello=Hello'],
['{;list}', ';list=red,green,blue'],
['{;list*}', ';list=red;list=green;list=blue'],
['{;keys}', [
';keys=comma,%2C,dot,.,semi,%3B',
';keys=comma,%2C,semi,%3B,dot,.',
';keys=dot,.,comma,%2C,semi,%3B',
';keys=dot,.,semi,%3B,comma,%2C',
';keys=semi,%3B,comma,%2C,dot,.',
';keys=semi,%3B,dot,.,comma,%2C'
]],
['{;keys*}', [
';comma=%2C;dot=.;semi=%3B',
';comma=%2C;semi=%3B;dot=.',
';dot=.;comma=%2C;semi=%3B',
';dot=.;semi=%3B;comma=%2C',
';semi=%3B;comma=%2C;dot=.',
';semi=%3B;dot=.;comma=%2C'
]],
['{?var:3}', '?var=val'],
['{?list}', '?list=red,green,blue'],
['{?list*}', '?list=red&list=green&list=blue'],
['{?keys}', [
'?keys=comma,%2C,dot,.,semi,%3B',
'?keys=comma,%2C,semi,%3B,dot,.',
'?keys=dot,.,comma,%2C,semi,%3B',
'?keys=dot,.,semi,%3B,comma,%2C',
'?keys=semi,%3B,comma,%2C,dot,.',
'?keys=semi,%3B,dot,.,comma,%2C'
]],
['{?keys*}', [
'?comma=%2C&dot=.&semi=%3B',
'?comma=%2C&semi=%3B&dot=.',
'?dot=.&comma=%2C&semi=%3B',
'?dot=.&semi=%3B&comma=%2C',
'?semi=%3B&comma=%2C&dot=.',
'?semi=%3B&dot=.&comma=%2C'
]],
['{&var:3}', '&var=val'],
['{&list}', '&list=red,green,blue'],
['{&list*}', '&list=red&list=green&list=blue'],
['{&keys}', [
'&keys=comma,%2C,dot,.,semi,%3B',
'&keys=comma,%2C,semi,%3B,dot,.',
'&keys=dot,.,comma,%2C,semi,%3B',
'&keys=dot,.,semi,%3B,comma,%2C',
'&keys=semi,%3B,comma,%2C,dot,.',
'&keys=semi,%3B,dot,.,comma,%2C'
]],
['{&keys*}', [
'&comma=%2C&dot=.&semi=%3B',
'&comma=%2C&semi=%3B&dot=.',
'&dot=.&comma=%2C&semi=%3B',
'&dot=.&semi=%3B&comma=%2C',
'&semi=%3B&comma=%2C&dot=.',
'&semi=%3B&dot=.&comma=%2C'
]]
];
generateTests(testcases, variables);
});
generateTests(testcases, variables);
});
});

@@ -1,112 +0,123 @@

define(['chai-as-promised', 'lib/xhr'], function(chaiAsPromised, xhr) {
chai.use(chaiAsPromised);
// import chaiAsPromised from 'chai-as-promised';
import xhr from 'src/xhr';
describe('xhr', function() {
var server;
// chai.use(chaiAsPromised);
describe('return values', function() {
beforeEach(function() {
server = sinon.fakeServer.create();
server.respondWith('GET', '/', [200, { 'Content-Type': 'text/plain' }, 'hello world']);
server.autoRespond = true;
});
describe('xhr', function() {
var server;
afterEach(function() {
server.restore();
server = null;
});
describe('return values', function() {
beforeEach(function() {
server = sinon.fakeServer.create();
server.respondWith('GET', '/', [200, { 'Content-Type': 'text/plain' }, 'hello world']);
server.autoRespond = true;
});
it('returns a promise', function() {
expect(xhr('/').then).to.be.an('function');
});
afterEach(function() {
server.restore();
server = null;
});
it('returns a resolved promise on XHR success', function() {
return expect(xhr('/').then(function(res) { return res.text(); })).to.become('hello world');
});
it('returns a promise', function() {
expect(xhr('/')).toEqual(jasmine.any(Promise));
});
it('returns a successful promise on XHR complete', function() {
return expect(xhr('/foo').then(function(res) { return res.status; })).to.become(404);
});
it('returns a resolved promise on XHR success', function(done) {
return xhr('/')
.then((res) => res.text())
.then((text) => {
expect(text).toEqual('hello world');
})
.then(done);
});
describe('request paramaters', function() {
beforeEach(function() {
server = sinon.fakeServer.create();
server.autoRespond = true;
});
it('returns a successful promise on XHR complete', function(done) {
return xhr('/foo')
.then((res) => {
expect(res.status).toEqual(404);
})
.then(done);
});
});
afterEach(function() {
server.restore();
server = null;
});
describe('request paramaters', function() {
beforeEach(function() {
server = sinon.fakeServer.create();
server.autoRespond = true;
});
describe('when specifying request method', function() {
it('capitalizes the method', function() {
var uri = '/';
afterEach(function() {
server.restore();
server = null;
});
server.respondWith(function(request) {
expect(request.method).to.equal('PATCH');
request.respond(200, { "Content-Type": "text/plain" }, '');
});
describe('when specifying request method', function() {
it('capitalizes the method', function(done) {
var uri = '/';
return xhr(uri, { method: 'patch' });
server.respondWith(function(request) {
expect(request.method).toEqual('PATCH');
request.respond(200, { 'Content-Type': 'text/plain' }, '');
done();
});
return xhr(uri, { method: 'patch' });
});
});
describe('when making a request to the same domain', function() {
it('sets cross origin properties', function() {
var uri = '/';
describe('when making a request to the same domain', function() {
it('sets cross origin properties', function(done) {
var uri = '/';
server.respondWith(function(request) {
expect(request.url).to.equal(uri);
request.respond(200, { "Content-Type": "text/plain" }, '');
expect(request.withCredentials).to.be.undefined;
expect(request.crossOrigin).to.be.false;
});
server.respondWith(function(request) {
expect(request.url).toEqual(uri);
request.respond(200, { 'Content-Type': 'text/plain' }, '');
expect(request.withCredentials).toBeFalsy();
done();
});
return xhr(uri);
});
return xhr(uri);
});
});
describe('when making a request to the same domain but a different port', function() {
it('sets cross origin properties', function() {
var uri = 'http://localhost:8000';
describe('when making a request to the same domain but a different port', function() {
it('sets cross origin properties', function(done) {
var uri = 'http://localhost:8000';
server.respondWith(function(request) {
expect(request.url).to.equal(uri);
request.respond(200, { "Content-Type": "text/plain" }, '');
expect(request.withCredentials).to.be.undefined;
expect(request.crossOrigin).to.be.true;
});
server.respondWith(function(request) {
expect(request.url).toEqual(uri);
request.respond(200, { 'Content-Type': 'text/plain' }, '');
expect(request.withCredentials).toBeTruthy();
done();
});
return xhr(uri);
});
return xhr(uri);
});
});
describe('when making a request to the same domain', function() {
it('sets cross origin properties', function() {
var uri = 'http://example.com/';
describe('when making a request to the same domain', function() {
it('sets cross origin properties', function(done) {
var uri = 'http://example.com/';
server.respondWith(function(request) {
expect(request.url).to.equal(uri);
request.respond(200, { "Content-Type": "text/plain" }, '');
expect(request.withCredentials).to.be.undefined;
expect(request.crossOrigin).to.be.true;
});
return xhr(uri);
server.respondWith(function(request) {
expect(request.url).toEqual(uri);
request.respond(200, { 'Content-Type': 'text/plain' }, '');
expect(request.withCredentials).toBeTruthy();
done();
});
it('doesn\'t override withCredentials to be true', function() {
var uri = 'http://example.com/';
return xhr(uri);
});
server.respondWith(function(request) {
expect(request.url).to.equal(uri);
request.respond(200, { "Content-Type": "text/plain" }, '');
expect(request.withCredentials).to.be.false;
expect(request.crossOrigin).to.be.true;
});
it('doesn\'t override withCredentials to be true', function(done) {
var uri = 'http://example.com/';
return xhr(uri, { withCredentials: false });
server.respondWith(function(request) {
expect(request.url).toEqual(uri);
request.respond(200, { 'Content-Type': 'text/plain' }, '');
expect(request.withCredentials).toBeTruthy();
done();
});
return xhr(uri, { withCredentials: false });
});

@@ -113,0 +124,0 @@ });

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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