本文整理汇总了TypeScript中supertest类的典型用法代码示例。如果您正苦于以下问题:TypeScript supertest类的具体用法?TypeScript supertest怎么用?TypeScript supertest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了supertest类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: express
/// <reference path="supertest.d.ts" />
/// <reference path="../express/express.d.ts" />
import * as supertest from 'supertest';
import * as express from 'express';
const app = express();
supertest(app)
.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '20')
.expect(201)
.end((err, res) => {
if (err) throw err;
});
// cookie scenario
const request = supertest(app);
const agent = supertest.agent();
request
.post('/login')
.end((err: any, res: supertest.Response) => {
if (err) throw err;
agent.saveCookies(res);
const req = request.get('/admin');
agent.attachCookies(req);
req.expect(200, (err: any, res: supertest.Response) => {
if (err) throw err;
});
示例2: express
import * as supertest from 'supertest';
import * as express from 'express';
const app = express();
const request: supertest.SuperTest<supertest.Test> = supertest(app);
(request
.get('/user') as supertest.Test)
.expect('Content-Type', /json/)
.expect('Content-Length', '20')
.expect(201)
.end((err, res) => {
if (err) throw err;
});
// cookie scenario
const agent = supertest.agent();
request
.post('/login')
.end((err: any, res: supertest.Response) => {
if (err) throw err;
agent.saveCookies(res);
const req = request.get('/admin') as supertest.Test;
agent.attachCookies(req);
req.expect(200, (err: any, res: supertest.Response) => {
if (err) throw err;
});
});
示例3: logger
function logger(format: String = '') {//最佳实践:中间件最好有名字(不用匿名函数)
format = format || ':method | :url';
return async function (ctx: Koa.Context, next: Function) {
const str = format
.replace(':method', ctx.method)
.replace(':url', ctx.url);
if (ctx.body) {
ctx.body += ' ' + str;
} else {
ctx.body = str;
}
await next();
};
}
app.use(logger(':method & :url'));
app.listen(3000);
supertest('http://localhost:3000')
.get('/test')
.expect(200)
.expect('GET & /test')
.end((err, res) => {
if (err) {
throw err;
} else {
console.log('测试通过!');
}
});
示例4: request
it('should render page when form is invalid and everything is fine', async () => {
await request(app)
.post(Paths.noClaimNumberPage.uri)
.expect(res => expect(res).to.be.successful.withText('Which service did you use to view or make the claim?', 'div class="error-summary"'))
})
示例5: it
it("should return a delete", function(done) {
request(app)
.delete("/one/2")
.expect('{"id":"2","method":"delete"}', done);
});
示例6: request
it(`/POST (Observable stream)`, () => {
return request(server)
.post('/?command=stream.sum')
.send([1, 2, 3, 4, 5])
.expect(200, '15');
});
示例7: request
it('should render page when everything is fine', async () => {
await request(app)
.get(pagePath)
.expect(res => expect(res).to.be.successful.withText(expectedTextOnPage))
})
示例8: request
it('should render privacy policy page when everything is fine', async () => {
await request(app)
.get(Paths.privacyPolicyPage.uri)
.expect(res => expect(res).to.be.successful.withText('Privacy policy'))
})
示例9: request
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
});