-
Notifications
You must be signed in to change notification settings - Fork 113
/
test.ts
125 lines (106 loc) · 3.83 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import fs = require('fs');
import http = require('http');
import chai = require('chai');
import ChaiHttp = require('./index');
chai.use(ChaiHttp);
declare const app: http.Server;
chai.request(app).get('/');
chai.request('http://localhost:8080').get('/');
chai.request(app)
.put('/user/me')
.set('X-API-Key', 'foobar')
.send({ password: '123', confirmPassword: '123' });
chai.request(app)
.post('/user/me')
.field('_method', 'put')
.field('password', '123')
.field('confirmPassword', '123');
chai.request(app)
.post('/user/avatar')
.attach('imageField', fs.readFileSync('avatar.png'), 'avatar.png');
chai.request(app)
.get('/protected')
.auth('user', 'pass');
// HTTPS request, from: https://github.com/visionmedia/superagent/commit/6158efbf42cb93d77c1a70887284be783dd7dabe
const ca = fs.readFileSync('ca.cert.pem');
const key = fs.readFileSync('key.pem');
const cert = fs.readFileSync('cert.pem');
const callback = (err: any, res: ChaiHttp.Response) => {};
chai.request(app)
.post('/secure')
.ca(ca)
.key(key)
.cert(cert)
.end(callback);
const pfx = fs.readFileSync('cert.pfx');
chai.request(app)
.post('/secure')
.pfx(pfx)
.end(callback);
chai.request(app)
.get('/search')
.query({ name: 'foo', limit: 10 });
chai.request(app)
.get('/download')
.buffer()
.parse((res, cb) => {
let data = '';
res.setEncoding('binary');
res.on('data', (chunk: any) => { data += chunk; });
res.on('end', () => { cb(undefined, new Buffer(data, 'binary')); });
});
chai.request(app)
.put('/user/me')
.send({ passsword: '123', confirmPassword: '123' })
.end((err: any, res: ChaiHttp.Response) => {
chai.expect(err).to.be.null;
chai.expect(res).to.have.status(200);
});
chai.request(app)
.put('/user/me')
.send({ passsword: '123', confirmPassword: '123' })
.then((res: ChaiHttp.Response) => chai.expect(res).to.have.status(200))
.catch((err: any) => { throw err; });
chai.request(app)
.keepOpen()
.close((err: any) => { throw err; });
const agent = chai.request.agent(app);
agent
.post('/session')
.send({ username: 'me', password: '123' })
.then((res: ChaiHttp.Response) => {
chai.expect(res).to.have.cookie('sessionid');
// The `agent` now has the sessionid cookie saved, and will send it
// back to the server in the next request:
return agent.get('/user/me')
.then((res: ChaiHttp.Response) => chai.expect(res).to.have.status(200));
});
agent.close((err: any) => { throw err; });
function test1() {
const req = chai.request(app).get('/');
req.then((res: ChaiHttp.Response) => {
chai.expect(res).to.have.status(200);
chai.expect(res).to.have.header('content-type', 'text/plain');
chai.expect(res).to.have.header('content-type', /^text/);
chai.expect(res).to.have.headers;
chai.expect('127.0.0.1').to.be.an.ip;
chai.expect(res).to.be.json;
chai.expect(res).to.be.html;
chai.expect(res).to.be.text;
chai.expect(res).to.redirect;
chai.expect(res).to.redirectTo('http://example.com');
chai.expect(res).to.have.param('orderby');
chai.expect(res).to.have.param('orderby', 'date');
chai.expect(res).to.not.have.param('limit');
chai.expect(req).to.have.cookie('session_id');
chai.expect(req).to.have.cookie('session_id', '1234');
chai.expect(req).to.not.have.cookie('PHPSESSID');
chai.expect(res).to.have.cookie('session_id');
chai.expect(res).to.have.cookie('session_id', '1234');
chai.expect(res).to.not.have.cookie('PHPSESSID');
chai.expect(res.body).to.have.property('version', '4.0.0');
chai.expect(res.text).to.equal('<html><body></body></html>');
}, (err: any) => {
throw err;
});
}