本文整理匯總了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!');
});