2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-12 04:59:30 +00:00
|
|
|
const context = require('../helpers/context');
|
2015-10-05 18:23:47 +00:00
|
|
|
const request = require('supertest');
|
2015-10-13 06:19:42 +00:00
|
|
|
const Koa = require('../..');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
describe('ctx.attachment([filename])', function(){
|
|
|
|
describe('when given a filename', function(){
|
|
|
|
it('should set the filename param', function(){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.attachment('path/to/tobi.png');
|
2015-10-05 18:23:47 +00:00
|
|
|
const str = 'attachment; filename="tobi.png"';
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.response.header['content-disposition'].should.equal(str);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
describe('when omitting filename', function(){
|
|
|
|
it('should not set filename param', function(){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.attachment();
|
|
|
|
ctx.response.header['content-disposition'].should.equal('attachment');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-09-15 08:39:53 +00:00
|
|
|
|
|
|
|
describe('when given a no-ascii filename', function(){
|
|
|
|
it('should set the encodeURI filename param', function(){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2014-09-15 08:39:53 +00:00
|
|
|
ctx.attachment('path/to/include-no-ascii-char-中文名-ok.png');
|
2015-10-05 18:23:47 +00:00
|
|
|
const str = 'attachment; filename=\"include-no-ascii-char-???-ok.png\"; filename*=UTF-8\'\'include-no-ascii-char-%E4%B8%AD%E6%96%87%E5%90%8D-ok.png';
|
2014-09-15 08:39:53 +00:00
|
|
|
ctx.response.header['content-disposition'].should.equal(str);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-09-15 08:39:53 +00:00
|
|
|
|
|
|
|
it('should work with http client', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-09-15 08:39:53 +00:00
|
|
|
|
2015-10-14 00:45:18 +00:00
|
|
|
app.use(function *(ctx, next){
|
|
|
|
ctx.attachment('path/to/include-no-ascii-char-中文名-ok.json');
|
|
|
|
ctx.body = {foo: 'bar'};
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-09-15 08:39:53 +00:00
|
|
|
|
|
|
|
request(app.listen())
|
|
|
|
.get('/')
|
|
|
|
.expect('content-disposition', 'attachment; filename="include-no-ascii-char-???-ok.json"; filename*=UTF-8\'\'include-no-ascii-char-%E4%B8%AD%E6%96%87%E5%90%8D-ok.json')
|
|
|
|
.expect({foo: 'bar'})
|
2015-10-12 20:36:41 +00:00
|
|
|
.expect(200, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|