kopia lustrzana https://github.com/c9/core
refactor tests as mocha
rodzic
eb671b6d13
commit
e0c071b497
|
@ -1,14 +1,14 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
"use server";
|
|
||||||
|
|
||||||
var assert = require("assert");
|
|
||||||
var sinon = require("sinon");
|
var sinon = require("sinon");
|
||||||
|
var frontdoor = require('../frontdoor');
|
||||||
|
var Route = frontdoor.Route;
|
||||||
|
var assert = require('assert');
|
||||||
|
|
||||||
var Route = require("./route");
|
require("c9/inline-mocha")(module);
|
||||||
|
require("amd-loader");
|
||||||
|
|
||||||
module.exports = {
|
it("test router: simple route with argument", function(done) {
|
||||||
"test router: simple route with argument": function() {
|
|
||||||
var route = new Route("/user/:name", sinon.stub());
|
var route = new Route("/user/:name", sinon.stub());
|
||||||
|
|
||||||
var req = {};
|
var req = {};
|
||||||
|
@ -17,9 +17,11 @@ module.exports = {
|
||||||
|
|
||||||
assert.equal(route.match(req, "/user/fabian"), true);
|
assert.equal(route.match(req, "/user/fabian"), true);
|
||||||
assert.equal(req.match.name, "fabian");
|
assert.equal(req.match.name, "fabian");
|
||||||
},
|
|
||||||
|
|
||||||
"test router: simple route with number argument": function() {
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("test router: simple route with number argument", function(done) {
|
||||||
var route = new Route("/user/:id", {
|
var route = new Route("/user/:id", {
|
||||||
params: {
|
params: {
|
||||||
id: {
|
id: {
|
||||||
|
@ -32,9 +34,11 @@ module.exports = {
|
||||||
assert.equal(route.match(req, "/user/fabian"), false);
|
assert.equal(route.match(req, "/user/fabian"), false);
|
||||||
assert.equal(route.match(req, "/user/123"), true);
|
assert.equal(route.match(req, "/user/123"), true);
|
||||||
assert.equal(req.match.id, 123);
|
assert.equal(req.match.id, 123);
|
||||||
},
|
|
||||||
|
|
||||||
"test router: for params if the value is a string it is treated as the type": function() {
|
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", {
|
var route = new Route("/user/:id", {
|
||||||
params: {
|
params: {
|
||||||
id: "int"
|
id: "int"
|
||||||
|
@ -44,9 +48,11 @@ module.exports = {
|
||||||
var req = {};
|
var req = {};
|
||||||
assert.equal(route.match(req, "/user/123"), true);
|
assert.equal(route.match(req, "/user/123"), true);
|
||||||
assert.equal(req.match.id, 123);
|
assert.equal(req.match.id, 123);
|
||||||
},
|
|
||||||
|
|
||||||
"test router: complex route with wildcard arguments": function() {
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("test router: complex route with wildcard arguments", function(done) {
|
||||||
var route = new Route("/user/:name/:rest*", {
|
var route = new Route("/user/:name/:rest*", {
|
||||||
params: {
|
params: {
|
||||||
id: {
|
id: {
|
||||||
|
@ -70,9 +76,12 @@ module.exports = {
|
||||||
assert.equal(route.match(req, "/user/fabian/abc/123"), true);
|
assert.equal(route.match(req, "/user/fabian/abc/123"), true);
|
||||||
assert.equal(req.match.name, "fabian");
|
assert.equal(req.match.name, "fabian");
|
||||||
assert.equal(req.match.rest, "/abc/123");
|
assert.equal(req.match.rest, "/abc/123");
|
||||||
},
|
|
||||||
|
|
||||||
"test router: complex route with multiple arguments": function() {
|
done();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it("test router: complex route with multiple arguments", function(done) {
|
||||||
var route = new Route("/user/:name/:id", {
|
var route = new Route("/user/:name/:id", {
|
||||||
params: {
|
params: {
|
||||||
id: {
|
id: {
|
||||||
|
@ -88,9 +97,12 @@ module.exports = {
|
||||||
assert.equal(route.match(req, "/user/fabian/123"), true);
|
assert.equal(route.match(req, "/user/fabian/123"), true);
|
||||||
assert.equal(req.match.id, 123);
|
assert.equal(req.match.id, 123);
|
||||||
assert.equal(req.match.name, "fabian");
|
assert.equal(req.match.name, "fabian");
|
||||||
},
|
|
||||||
|
|
||||||
"test regexp types": function() {
|
done();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it("test regexp types", function(done) {
|
||||||
var route = new Route("/users/:uid", {
|
var route = new Route("/users/:uid", {
|
||||||
params: {
|
params: {
|
||||||
uid: /u\d+/
|
uid: /u\d+/
|
||||||
|
@ -101,9 +113,12 @@ module.exports = {
|
||||||
|
|
||||||
assert.ok(route.match(req, "/users/u123"));
|
assert.ok(route.match(req, "/users/u123"));
|
||||||
assert.ok(!route.match(req, "/users/_u123"));
|
assert.ok(!route.match(req, "/users/_u123"));
|
||||||
},
|
|
||||||
|
|
||||||
"test custom type without register": function() {
|
done();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it("test custom type without register", function(done) {
|
||||||
var DateType = {
|
var DateType = {
|
||||||
parse: function(string) {
|
parse: function(string) {
|
||||||
if (!/\d{13}/.test(string))
|
if (!/\d{13}/.test(string))
|
||||||
|
@ -131,7 +146,6 @@ module.exports = {
|
||||||
|
|
||||||
assert.ok(!route.match(req, "/ts/353676299181"));
|
assert.ok(!route.match(req, "/ts/353676299181"));
|
||||||
assert.ok(!route.match(req, "/ts/abc"));
|
assert.ok(!route.match(req, "/ts/abc"));
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
!module.parent && require("asyncjs").test.testcase(module.exports).exec();
|
done();
|
||||||
|
});
|
|
@ -0,0 +1,170 @@
|
||||||
|
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