Skip to content

Commit

Permalink
Koa tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
gevorg committed Jun 11, 2016
1 parent 3660b89 commit 74e2623
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 150 deletions.
19 changes: 13 additions & 6 deletions package.json
Expand Up @@ -31,25 +31,32 @@
"node-uuid": "^1.4.7"
},
"devDependencies": {
"coffee-script": "^1.10.0",
"babel-cli": "^6.9.0",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.9.0",
"mocha": "^2.5.3",
"chai": "^3.5.0",
"coffee-script": "^1.10.0",
"express": "^4.13.4",
"http-proxy": "^1.13.3",
"request": "^2.72.0",
"koa": "^1.2.0",
"mocha": "^2.5.3",
"passport": "^0.3.2",
"koa": "^1.2.0"
"request": "^2.72.0"
},
"engines": {
"node": ">=5"
},
"scripts": {
"test": "mocha --compilers js:babel-core/register",
"test": "mocha --compilers js:babel-core/register --require babel-polyfill",
"compile": "node ./node_modules/coffee-script/bin/coffee --compile -o gensrc src",
"prepublish": "npm run compile",
"pretest": "npm run compile"
},
"keywords": ["http", "basic", "digest", "access", "authentication"]
"keywords": [
"http",
"basic",
"digest",
"access",
"authentication"
]
}
73 changes: 0 additions & 73 deletions test-old/test-koa-exception.coffee

This file was deleted.

71 changes: 0 additions & 71 deletions test-old/test-koa.coffee

This file was deleted.

96 changes: 96 additions & 0 deletions test/koa.js
@@ -0,0 +1,96 @@
"use strict";

// Expect module.
import {expect} from 'chai'

// Request module.
import request from 'request'

// Koa.
import koa from 'koa'

// Source.
import auth from '../gensrc/http-auth'

// Express.
describe('koa', function () {
let server = undefined;

before(function () {
// Configure authentication.
let basic = auth.basic({
realm: "Private Area."
}, function (username, password, done) {
if (username === 'gevorg') {
done(new Error("Error comes here"));
} else if (username === "mia" && password === "supergirl") {
done(true);
} else if (username === "ColonUser" && password === "apasswordwith:acolon") {
done(true);
} else {
done(false);
}
});

// Creating new HTTP server.
let app = koa();
app.use(auth.koa(basic));

// Setup route.
app.use(function *(){
this.body = 'Welcome to private area - ' + this.req.user + '!';
});

// Start server.
server = app.listen(1337);
});

after(function () {
server.close();
});

it('error', function () {
let callback = function (error, response, body) {
expect(body).to.equal("Error comes here");
};

// Test request.
request.get('http://127.0.0.1:1337', callback).auth('gevorg', 'gpass');
});

it('success', function () {
let callback = function (error, response, body) {
expect(body).to.equal("Welcome to private area - mia!");
};

// Test request.
request.get('http://127.0.0.1:1337', callback).auth('mia', 'supergirl');
});

it('wrong password', function () {
let callback = function (error, response, body) {
expect(body).to.equal("401 Unauthorized");
};

// Test request.
request.get('http://127.0.0.1:1337', callback).auth('mia', 'cute');
});

it('wrong user', function () {
let callback = function (error, response, body) {
expect(body).to.equal("401 Unauthorized");
};

// Test request.
request.get('http://127.0.0.1:1337', callback).auth('Tina', 'supergirl');
});

it('password with colon', function () {
let callback = function (error, response, body) {
expect(body).to.equal("Welcome to private area - ColonUser!");
};

// Test request.
request.get('http://127.0.0.1:1337', callback).auth('ColonUser', 'apasswordwith:acolon');
});
});

0 comments on commit 74e2623

Please sign in to comment.