Skip to content

Commit a576936

Browse files
committed
✅ Merge koa1 and koa2 tests
1 parent 945d2ab commit a576936

8 files changed

Lines changed: 246 additions & 173 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"test": "npm install --no-save leancloud-storage@3 && npm-run-all test-tsd test-express test-koa1 test-koa2",
5454
"test-express": "mocha test test/express",
5555
"test-tsd": "tsc leanengine.d.ts",
56-
"test-koa1": "env FRAMEWORK=koa npm install --no-save leancloud-storage@3 koa@1 koa-bodyparser@2 && mocha test test/koa",
57-
"test-koa2": "env FRAMEWORK=koa npm install --no-save leancloud-storage@3 koa@2 koa-bodyparser@4 && mocha test test/koa test/koa2"
56+
"test-koa1": "npm install --no-save leancloud-storage@3 koa@1 koa-bodyparser@2 && env FRAMEWORK=koa KOA_VER=1 mocha test test/koa",
57+
"test-koa2": "npm install --no-save leancloud-storage@3 koa@2 koa-bodyparser@4 && env FRAMEWORK=koa KOA_VER=2 mocha test test/koa"
5858
}
5959
}

test/fixtures/app.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
const AV = require('../..');
22

33
module.exports = function(options) {
4-
if (process.env.FRAMEWORK == 'koa') {
4+
if (process.env.FRAMEWORK === 'koa') {
55
const Koa = require('koa');
66
var app = new Koa();
7-
app.use(AV.koa(options));
7+
8+
if (process.env.KOA_VER === '1') {
9+
app.use(AV.koa(options));
10+
} else {
11+
app.use(AV.koa2(options));
12+
}
13+
814
return app.listen();
915
} else {
1016
return AV.express(options);

test/koa/cookie-session-koa-test.js

Lines changed: 0 additions & 100 deletions
This file was deleted.

test/koa/cookie-session-test.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
var Koa = require('koa');
2+
var bodyParser = require('koa-bodyparser');
3+
var request = require('supertest');
4+
var should = require('should');
5+
6+
const AV = require('../..');
7+
const appInfo = require('../fixtures/app-info');
8+
9+
var app = new Koa();
10+
11+
if (process.env.KOA_VER === '1') {
12+
app.use(AV.koa());
13+
app.use(bodyParser());
14+
app.use(AV.Cloud.CookieSession({framework: 'koa', secret: 'my secret', maxAge: 3600000, fetchUser: true}));
15+
16+
app.use(function *(next) {
17+
try {
18+
yield next;
19+
} catch (err) {
20+
this.status = err.status || 500;
21+
this.body = err.message;
22+
this.app.emit('error', err, this);
23+
}
24+
});
25+
26+
app.use(function *(next) {
27+
var method = this.request.method;
28+
var url = this.request.url;
29+
30+
if (method === 'GET' && url === '/') {
31+
this.status = 200;
32+
this.body = '<p>Hello world</p>';
33+
} else if (method === 'POST' && url === '/login') {
34+
return AV.User.logIn(this.request.body.username, this.request.body.password).then( user => {
35+
this.saveCurrentUser(user);
36+
this.response.redirect('/profile');
37+
});
38+
} else if (method === 'GET' && url === '/profile') {
39+
this.status = 200;
40+
this.body = this.currentUser;
41+
} else if (method === 'POST' && url === '/logout') {
42+
this.status = 200;
43+
this.saveCurrentUser(null);
44+
} else {
45+
yield next;
46+
}
47+
});
48+
} else {
49+
app.use(AV.koa2());
50+
app.use(bodyParser());
51+
app.use(AV.Cloud.CookieSession({framework: 'koa2', secret: 'my secret', maxAge: 3600000, fetchUser: true}));
52+
53+
app.use(async (ctx, next) => {
54+
try {
55+
await next();
56+
} catch (err) {
57+
ctx.status = err.status || 500;
58+
ctx.body = err.message;
59+
ctx.app.emit('error', err, this);
60+
}
61+
});
62+
63+
app.use(async (ctx, next) => {
64+
var method = ctx.request.method;
65+
var url = ctx.request.url;
66+
67+
if (method === 'GET' && url === '/') {
68+
ctx.status = 200;
69+
ctx.body = '<p>Hello world</p>';
70+
} else if (method === 'POST' && url === '/login') {
71+
return AV.User.logIn(ctx.request.body.username, ctx.request.body.password).then( user => {
72+
ctx.saveCurrentUser(user);
73+
ctx.response.redirect('/profile');
74+
});
75+
} else if (method === 'GET' && url === '/profile') {
76+
ctx.status = 200;
77+
ctx.body = ctx.currentUser;
78+
} else if (method === 'POST' && url === '/logout') {
79+
ctx.status = 200;
80+
ctx.saveCurrentUser(null);
81+
} else {
82+
return next();
83+
}
84+
});
85+
}
86+
87+
var server = app.listen();
88+
89+
describe('koa/cookie-session', function() {
90+
it('index', function(done) {
91+
request(server).get('/')
92+
.expect(200, function(err, res) {
93+
res.headers['content-type'].should.be.startWith('text/html');
94+
res.text.should.be.equal('<p>Hello world</p>');
95+
done(err);
96+
});
97+
});
98+
99+
it('loign', function(done) {
100+
request(server).post('/login')
101+
.send({
102+
username: 'admin',
103+
password: 'admin'
104+
})
105+
.expect(302, function(err, res) {
106+
res.headers.location.should.equal('/profile');
107+
res.headers['set-cookie'][0].indexOf('avos:sess=eyJfdWlkIjoiNTRmZDZhMDNlNGIwNmM0MWUwMGIxZjQwIiwiX3Nlc3Npb25Ub2tlbiI6IncyanJ0a2JlaHAzOG90cW1oYnF1N3liczkifQ==; path=/; expires=').should.equal(0);
108+
res.headers['set-cookie'][1].indexOf('avos:sess.sig=jMYF3Iwhmw903-K1K12MVdAFOh0; path=/; expires=').should.equal(0);
109+
done(err);
110+
});
111+
});
112+
113+
it('profile', function(done) {
114+
request(server).get('/profile')
115+
.set('Cookie', 'avos:sess=eyJfdWlkIjoiNTRmZDZhMDNlNGIwNmM0MWUwMGIxZjQwIiwiX3Nlc3Npb25Ub2tlbiI6IncyanJ0a2JlaHAzOG90cW1oYnF1N3liczkifQ==; avos:sess.sig=jMYF3Iwhmw903-K1K12MVdAFOh0')
116+
.expect(200, function(err, res) {
117+
should.exist(res.body.objectId);
118+
res.body.username.should.be.equal('admin');
119+
done(err);
120+
});
121+
});
122+
123+
it('profile without cookie', function(done) {
124+
request(server).get('/profile')
125+
.expect(204, function(err, res) {
126+
res.body.should.be.empty();
127+
done(err);
128+
});
129+
});
130+
131+
it('logout', function(done) {
132+
request(server).post('/logout')
133+
.set('Cookie', 'avos:sess=eyJfdWlkIjoiNTRmZDZhMDNlNGIwNmM0MWUwMGIxZjQwIiwiX3Nlc3Npb25Ub2tlbiI6IncyanJ0a2JlaHAzOG90cW1oYnF1N3liczkifQ==; avos:sess.sig=jMYF3Iwhmw903-K1K12MVdAFOh0')
134+
.expect(200, function(err, res) {
135+
res.headers['set-cookie'][0].indexOf('avos:sess=; path=/; expires=').should.equal(0);
136+
done(err);
137+
});
138+
})
139+
});

test/koa/https-redirect-koa-test.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

test/koa/https-redirect-test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
var Koa = require('koa');
4+
var request = require('supertest');
5+
require('should');
6+
7+
var AV = require('../..');
8+
const appInfo = require('../fixtures/app-info');
9+
10+
var app = new Koa();
11+
12+
app.proxy = true;
13+
14+
if (process.env.KOA_VER === '1') {
15+
app.use(AV.Cloud.HttpsRedirect({framework: 'koa'}));
16+
17+
app.use(function *(next) {
18+
this.body = 'Hello World!';
19+
yield next;
20+
});
21+
} else {
22+
app.use(AV.Cloud.HttpsRedirect({framework: 'koa2'}));
23+
24+
app.use(async ctx => {
25+
ctx.body = 'Hello World!';
26+
});
27+
}
28+
29+
var server = app.listen();
30+
31+
describe('koa/https-redirect', function() {
32+
it('should redirect', function(done) {
33+
request(server)
34+
.get('/test')
35+
.set('host', 'stg-abc.leanapp.cn')
36+
.expect(302)
37+
.end(function(err, res) {
38+
res.headers.location.should.equal('https://stg-abc.leanapp.cn/test');
39+
res.text.should.endWith('Redirecting to https://stg-abc.leanapp.cn/test');
40+
done();
41+
});
42+
});
43+
44+
it('should not redirect (local)', function(done) {
45+
request(server)
46+
.get('/test')
47+
.expect(200)
48+
.expect("Hello World!", done);
49+
});
50+
51+
it('should not redirect (https)', function(done) {
52+
request(server)
53+
.get('/test')
54+
.set('HOST', 'stg-abc.leanapp.cn')
55+
.set('X-Forwarded-Proto', 'https')
56+
.expect(200)
57+
.expect("Hello World!", done);
58+
});
59+
});

0 commit comments

Comments
 (0)