Skip to content

Commit

Permalink
Merge pull request #4 from nswbmw/master
Browse files Browse the repository at this point in the history
Restore this.path when upstream
  • Loading branch information
tj committed Oct 21, 2015
2 parents 1fdf5aa + f2a27e8 commit c749de7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ function rewrite(src, dst) {
});

debug('rewrite %s -> %s', orig, this.path);

yield* next;

this.path = orig;
return;
}

yield next;
yield* next;
}
}

Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"description": "URL rewrite middleware for koa",
"repository": "koajs/rewrite",
"version": "1.1.0",
"scripts": {
"test": "./node_modules/mocha/bin/mocha --harmony test/test"
},
"keywords": [
"koa",
"middleware",
Expand All @@ -14,7 +17,9 @@
"index.js"
],
"devDependencies": {
"koa": "~0.2.1"
"koa": "~0.2.1",
"mocha": "^2.2.4",
"supertest": "^0.15.0"
},
"license": "MIT",
"dependencies": {
Expand Down
73 changes: 73 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
var request = require('supertest');
var koa = require('koa');
var rewrite = require('..');

describe('koa-rewrite', function () {
it('rewrite /^\/i(\w+)/ -> /items/$1', function (done) {
var app = koa();
app.use(function* (next) {
var orig = this.path;
yield* next;
if (orig !== this.path) this.throw(this.path + ' not equal to original path ' + orig);
});
app.use(rewrite(/^\/i(\w+)/, '/items/$1'));
app.use(function* () {
this.body = this.path;
});

request(app.callback())
.get('/i124')
.expect('/items/124', done);
});

it('rewrite /:src..:dst -> /commits/$1/to/$2', function (done) {
var app = koa();
app.use(function* (next) {
var orig = this.path;
yield* next;
if (orig !== this.path) this.throw(this.path + ' not equal to original path ' + orig);
});
app.use(rewrite('/:src..:dst', '/commits/$1/to/$2'));
app.use(function* () {
this.body = this.path;
});

request(app.callback())
.get('/foo..bar')
.expect('/commits/foo/to/bar', done);
});

it('rewrite /:src..:dst -> /commits/:src/to/:dst', function (done) {
var app = koa();
app.use(function* (next) {
var orig = this.path;
yield* next;
if (orig !== this.path) this.throw(this.path + ' not equal to original path ' + orig);
});
app.use(rewrite('/:src..:dst', '/commits/:src/to/:dst'));
app.use(function* () {
this.body = this.path;
});

request(app.callback())
.get('/foo..bar')
.expect('/commits/foo/to/bar', done);
});

it('rewrite /js/* -> /public/assets/js/$1', function (done) {
var app = koa();
app.use(function* (next) {
var orig = this.path;
yield* next;
if (orig !== this.path) this.throw(this.path + ' not equal to original path ' + orig);
});
app.use(rewrite('/js/*', '/public/assets/js/$1'));
app.use(function* () {
this.body = this.path;
});

request(app.callback())
.get('/js/jquery.js')
.expect('/public/assets/js/jquery.js', done);
});
});

0 comments on commit c749de7

Please sign in to comment.