Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add jsonp middleware for mock #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test: jshint
./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -R spec -t 20000 --require should --inline-diffs
node --harmony $(NODE_DEBUG) ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -R spec -t 20000 --require should --inline-diffs

coveralls: test
cat ./coverage/lcov.info | ./node_modules/.bin/coveralls
Expand Down
4 changes: 3 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ program
.option('--weinre', 'weinre')
.option('--https', 'https')
.option('--verbose', 'show more logging')
.option('--jsonp', 'jsonp middleware')
.parse(process.argv);

//log.config(program);
Expand All @@ -35,7 +36,8 @@ var args = {
weinre: program.weinre,
livereload: program.livereload,
proxy: program.proxy,
port: program.port || 8000
port: program.port || 8000,
jsonp: program.jsonp
};

var sw = require('spm-webpack');
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var readFile = fs.readFileSync;
var url = require('url');
var util = require('util');
var events = require('events');
var jsonp = require('koa-jsonp');

function Server(compiler, opts) {
events.EventEmitter.call(this);
Expand All @@ -36,6 +37,10 @@ function Server(compiler, opts) {

var app = this.app = koa();

// support jsonp
if(opts.jsonp || (opts.pkg && opts.pkg.spm && opts.pkg.spm.jsonp)) {
app.use(jsonp());
}
// combo 拆分
app.use(require('./combo')({
hostname: ip,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"gnode": "~0.1.1",
"internal-ip": "~1.0.0",
"koa": "~0.19.1",
"koa-jsonp": "~0.1.2",
"koa-send": "~1.3.0",
"koa-serve-index": "~1.0.1",
"koa-static": "~1.4.9",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/server-with-jsonp/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(1);
7 changes: 7 additions & 0 deletions test/fixtures/server-with-jsonp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"spm": {
"output": [
"a.js"
]
}
}
3 changes: 3 additions & 0 deletions test/fixtures/server-with-jsonp/x.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
49 changes: 49 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,55 @@ var fs = require('fs');
var existsSync = fs.existsSync;
var readFileSync = fs.readFileSync;

describe('server-with-jsonp', function() {

var app = null;
var args = null;

before(function (done) {
args = {
cwd : join(fixtures, 'server-with-jsonp'),
jsonp : true,
debug : true
};
process.chdir(args.cwd);
var pkgFile = join(args.cwd, 'package.json');
if (existsSync(pkgFile)) {
args.pkg = JSON.parse(readFileSync(pkgFile, 'utf-8'));
}
console.log(args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

调试代码?

sw.build.getWebpackOpts(args, function (err, webpackOpts) {
var server = new Server(sw.webpack(webpackOpts), args);
app = server.app;
server.once('done', done);
});
});

it('get /x.json', function(done) {
request(app.listen())
.get('/x.json')
.expect(function(res){
if(res.text.should.be.eql('{\n "hello": "world"\n}\n') === 0) throw new Error('Can not get right json content');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res.text.should.be.eql(...) 不就行了么,不用自己抛出异常吧?

})
.end(function(err){
if (err) return done(err);
done();
});
});

it('get /x.json?callback=t', function(done) {
request(app.listen())
.get('/x.json?callback=t')
.expect(function(res){
if(res.text.should.be.eql(';t({\n "hello": "world"\n}\n);') === 0) throw new Error('Can not get right jsonp content');
})
.end(function(err){
if (err) return done(err);
done();
});
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缺少 pkg.spm.jsonp 用例

});

describe('server-with-pkg-name', function() {

Expand Down