kopia lustrzana https://github.com/c9/core
remove tape tests
rodzic
e0c071b497
commit
259a0f36b6
|
@ -1,151 +0,0 @@
|
|||
"use strict";
|
||||
|
||||
var sinon = require("sinon");
|
||||
var frontdoor = require('../frontdoor');
|
||||
var Route = frontdoor.Route;
|
||||
var assert = require('assert');
|
||||
|
||||
require("c9/inline-mocha")(module);
|
||||
require("amd-loader");
|
||||
|
||||
it("test router: simple route with argument", function(done) {
|
||||
var route = new Route("/user/:name", sinon.stub());
|
||||
|
||||
var req = {};
|
||||
assert.equal(route.match(req, "/juhu"), false);
|
||||
assert.equal(route.match(req, "/juhu/12"), false);
|
||||
|
||||
assert.equal(route.match(req, "/user/fabian"), true);
|
||||
assert.equal(req.match.name, "fabian");
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it("test router: simple route with number argument", function(done) {
|
||||
var route = new Route("/user/:id", {
|
||||
params: {
|
||||
id: {
|
||||
type: "int"
|
||||
}
|
||||
}
|
||||
}, sinon.stub());
|
||||
|
||||
var req = {};
|
||||
assert.equal(route.match(req, "/user/fabian"), false);
|
||||
assert.equal(route.match(req, "/user/123"), true);
|
||||
assert.equal(req.match.id, 123);
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it("test router: for params if the value is a string it is treated as the type", function(done) {
|
||||
var route = new Route("/user/:id", {
|
||||
params: {
|
||||
id: "int"
|
||||
}
|
||||
}, sinon.stub());
|
||||
|
||||
var req = {};
|
||||
assert.equal(route.match(req, "/user/123"), true);
|
||||
assert.equal(req.match.id, 123);
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
it("test router: complex route with wildcard arguments", function(done) {
|
||||
var route = new Route("/user/:name/:rest*", {
|
||||
params: {
|
||||
id: {
|
||||
type: "int"
|
||||
},
|
||||
rest: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
}, sinon.stub());
|
||||
|
||||
var req = {};
|
||||
|
||||
assert.equal(route.match(req, "/user/fabian"), false);
|
||||
assert.equal(route.match(req, "/user/fabian/"), true);
|
||||
assert.equal(req.match.name, "fabian");
|
||||
assert.equal(req.match.rest, "/");
|
||||
assert.equal(route.match(req, "/user/fabian/abc"), true);
|
||||
assert.equal(req.match.name, "fabian");
|
||||
assert.equal(req.match.rest, "/abc");
|
||||
assert.equal(route.match(req, "/user/fabian/abc/123"), true);
|
||||
assert.equal(req.match.name, "fabian");
|
||||
assert.equal(req.match.rest, "/abc/123");
|
||||
|
||||
done();
|
||||
|
||||
});
|
||||
|
||||
it("test router: complex route with multiple arguments", function(done) {
|
||||
var route = new Route("/user/:name/:id", {
|
||||
params: {
|
||||
id: {
|
||||
type: "int"
|
||||
}
|
||||
}
|
||||
}, sinon.stub());
|
||||
|
||||
var req = {};
|
||||
|
||||
assert.equal(route.match(req, "/user/fabian"), false);
|
||||
assert.equal(route.match(req, "/user/123"), false);
|
||||
assert.equal(route.match(req, "/user/fabian/123"), true);
|
||||
assert.equal(req.match.id, 123);
|
||||
assert.equal(req.match.name, "fabian");
|
||||
|
||||
done();
|
||||
|
||||
});
|
||||
|
||||
it("test regexp types", function(done) {
|
||||
var route = new Route("/users/:uid", {
|
||||
params: {
|
||||
uid: /u\d+/
|
||||
}
|
||||
}, sinon.stub());
|
||||
|
||||
var req = {};
|
||||
|
||||
assert.ok(route.match(req, "/users/u123"));
|
||||
assert.ok(!route.match(req, "/users/_u123"));
|
||||
|
||||
done();
|
||||
|
||||
});
|
||||
|
||||
it("test custom type without register", function(done) {
|
||||
var DateType = {
|
||||
parse: function(string) {
|
||||
if (!/\d{13}/.test(string))
|
||||
throw new Error("not a timestamp");
|
||||
|
||||
return new Date(parseInt(string, 10));
|
||||
},
|
||||
check: function(value) {
|
||||
return value instanceof Date;
|
||||
}
|
||||
};
|
||||
|
||||
var route = new Route("/ts/:ts", {
|
||||
params: {
|
||||
ts: {
|
||||
type: DateType
|
||||
}
|
||||
}
|
||||
}, sinon.stub());
|
||||
|
||||
var req = {};
|
||||
|
||||
assert.ok(route.match(req, "/ts/1353676299181"));
|
||||
assert.ok(req.match.ts instanceof Date);
|
||||
|
||||
assert.ok(!route.match(req, "/ts/353676299181"));
|
||||
assert.ok(!route.match(req, "/ts/abc"));
|
||||
|
||||
done();
|
||||
});
|
|
@ -1,170 +0,0 @@
|
|||
var assert = require('assert-diff');
|
||||
var Section = require('../frontdoor').Section;
|
||||
var Url = require('url');
|
||||
|
||||
require("c9/inline-mocha")(module);
|
||||
// require("amd-loader");
|
||||
|
||||
|
||||
var mock = {
|
||||
req: function( method, uri) {
|
||||
var parsedUrl = Url.parse(uri||'', true);
|
||||
|
||||
return {
|
||||
method: method || 'get',
|
||||
parsedUrl: parsedUrl,
|
||||
pathname: parsedUrl.pathname,
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
it('Defines params on section level', function(done) {
|
||||
var testParams = {
|
||||
required: {
|
||||
type: 'int',
|
||||
optional: false,
|
||||
},
|
||||
int: {
|
||||
type: 'int',
|
||||
source: 'url'
|
||||
},
|
||||
string: 'string',
|
||||
alphanum: {
|
||||
type: /[a-z0-9]+/,
|
||||
source: 'url'
|
||||
},
|
||||
};
|
||||
|
||||
var cases = [
|
||||
{
|
||||
label: 'Match a simple string param',
|
||||
path: '/test/:string',
|
||||
url: '/test/foo',
|
||||
params: {
|
||||
string: 'foo',
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Match a simple number param',
|
||||
path: '/test/:int',
|
||||
url: '/test/123',
|
||||
params: {
|
||||
int: 123,
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Match multiple params',
|
||||
path: '/test/:int/:string',
|
||||
url: '/test/123/hello',
|
||||
params: {
|
||||
string: 'hello',
|
||||
int: 123,
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Match multiple params 3x',
|
||||
path: '/test/:string/:int/:alphanum',
|
||||
url: '/test/hello/123/baz123',
|
||||
params: {
|
||||
string: 'hello',
|
||||
int: 123,
|
||||
alphanum: 'baz123'
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Check ordered params',
|
||||
path: '/test/:string/:int/:alphanum',
|
||||
url: '/test/123/hello/baz123',
|
||||
err: true,
|
||||
},
|
||||
{
|
||||
label: 'Must match type int param',
|
||||
path: '/test/:int',
|
||||
url: '/test/test',
|
||||
err: true,
|
||||
},
|
||||
{
|
||||
label: 'Must match optinal type int',
|
||||
path: '/test/:int',
|
||||
url: '/test',
|
||||
err: true,
|
||||
},
|
||||
{
|
||||
label: 'Must match required type int',
|
||||
path: '/test/:required',
|
||||
url: '/test',
|
||||
err: true,
|
||||
},
|
||||
{
|
||||
label: 'Match an optional param',
|
||||
path: '/test/:optional',
|
||||
url: '/test',
|
||||
err: true,
|
||||
},
|
||||
{
|
||||
label: 'Match an implied url param',
|
||||
path: '/test/:implied',
|
||||
url: '/test/ok',
|
||||
params: {
|
||||
implied: 'ok',
|
||||
},
|
||||
},
|
||||
{
|
||||
label: 'Query params can be passed along',
|
||||
path: '/test/:string/:int/:alphanum',
|
||||
url: '/test/hello/123/baz123?q=123',
|
||||
options: {
|
||||
params: {
|
||||
q: {
|
||||
type: 'int',
|
||||
optional: false,
|
||||
source: 'query',
|
||||
}
|
||||
}
|
||||
},
|
||||
params: {
|
||||
string: 'hello',
|
||||
int: 123,
|
||||
alphanum: 'baz123',
|
||||
q: 123
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Required query params must be passed',
|
||||
path: '/test/:string/:int/:alphanum',
|
||||
url: '/test/hello/123/baz123',
|
||||
err: true,
|
||||
options: {
|
||||
params: {
|
||||
q: {
|
||||
type: 'int',
|
||||
optional: false,
|
||||
source: 'query',
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
cases.forEach(function(testCase) {
|
||||
var req = mock.req('get', testCase.url),
|
||||
api = new Section('test');
|
||||
|
||||
api.params = testParams;
|
||||
|
||||
api.get( testCase.path, testCase.options || {}, function(req, res, next){
|
||||
assert.deepEqual( req.params, testCase.params, testCase.label );
|
||||
});
|
||||
|
||||
api.handle( req.pathname, req, {}, function(err) {
|
||||
if ( testCase.err ) {
|
||||
assert.ok( 'route not matched: ' + testCase.label );
|
||||
return;
|
||||
}
|
||||
assert.fail( 'route not matched: ' + testCase.label );
|
||||
});
|
||||
});
|
||||
|
||||
done();
|
||||
});
|
Ładowanie…
Reference in New Issue