-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nswbmw/master
Restore this.path when upstream
- Loading branch information
Showing
3 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |