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"')) })
it("should return a delete", function(done) { request(app) .delete("/one/2") .expect('{"id":"2","method":"delete"}', done); });
it("should return a patch", function(done) { request(app) .patch("/one/4") .expect('{"id":"4","method":"patch"}', done); });
it(`/POST (Observable stream)`, () => { return request(server) .post('/?command=stream.sum') .send([1, 2, 3, 4, 5]) .expect(200, '15'); });
/// <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; });
it('should render page when everything is fine', async () => { await request(app) .get(pagePath) .expect(res => expect(res).to.be.successful.withText(expectedTextOnPage)) })
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')) })
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; }); });
it('/ (GET)', () => { return request(app.getHttpServer()) .get('/') .expect(200) .expect('Hello World!'); });
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('测试通过!'); } });