From edd32617ace7e076cdfe586665231365f9988615 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Mon, 12 Jan 2015 14:22:58 +1300 Subject: [PATCH] Add definition for chai-http --- chai-http/chai-http-tests.ts | 103 +++++++++++++++++++++++++++++++++++ chai-http/chai-http.d.ts | 87 +++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 chai-http/chai-http-tests.ts create mode 100644 chai-http/chai-http.d.ts diff --git a/chai-http/chai-http-tests.ts b/chai-http/chai-http-tests.ts new file mode 100644 index 000000000..e6e471321 --- /dev/null +++ b/chai-http/chai-http-tests.ts @@ -0,0 +1,103 @@ +/// +/// + +import fs = require('fs'); +import http = require('http'); +import chai = require('chai'); +import chaiHttp = require('chai-http'); +import when = require('when'); + +chai.use(chaiHttp); + +// Add promise support if this does not exist natively. +if (!global.Promise) { + chai.request.addPromises(when.promise); +} + + +var 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'); + +chai.request(app) + .get('/search') + .query('name', 'foo') + .query('limit', '10'); + +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; }); + +var 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)); + }); + +function test1() { + var 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'); + }, (err: any) => { + throw err; + }); +} + + +when(chai.request(app).get('/')).done(() => console.log('success'), () => console.log('failure')); diff --git a/chai-http/chai-http.d.ts b/chai-http/chai-http.d.ts new file mode 100644 index 000000000..fed2a8416 --- /dev/null +++ b/chai-http/chai-http.d.ts @@ -0,0 +1,87 @@ +// Type definitions for chai-http +// Project: https://github.com/chaijs/chai-http +// Definitions by: Wim Looman +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// + +declare module chai { + export function request(server: any): chaiHttp.Agent; + + export module request { + export function agent(server: any): chaiHttp.Agent; + export function addPromises(promiseConstructor: chaiHttp.PromiseConstructor): void; + } + + interface Assertions extends chaiHttp.Assertions { + } + + interface TypeComparison extends chaiHttp.TypeComparison { + } +} + +declare function chaiHttp(chai: any, utils: any): void; +declare module chaiHttp { + interface PromiseConstructor { + (resolver: (resolve: (value: T) => void, reject: (reason: any) => void) => void): Promise; + } + + interface Promise { + then(onFulfilled: (value: T) => U, onRejected?: (reason: any) => U): Promise; + } + + interface Response { + body: any; + type: string; + status: number; + } + + interface Request extends FinishedRequest { + attach(field: string, file: string, filename: string): Request; + attach(field: string, file: Buffer, filename: string): Request; + set(field: string, val: string): Request; + query(key: string, value: string): Request; + send(data: Object): Request; + auth(user: string, name: string): Request; + field(name: string, val: string): Request; + end(callback?: (err: any, res: Response) => void): FinishedRequest; + } + + interface FinishedRequest { + then(success?: (res: Response) => void, failure?: (err: any) => void): FinishedRequest; + catch(failure?: (err: any) => void): FinishedRequest; + } + + interface Agent { + get(url: string, callback?: (err: any, res: Response) => void): Request; + post(url: string, callback?: (err: any, res: Response) => void): Request; + put(url: string, callback?: (err: any, res: Response) => void): Request; + head(url: string, callback?: (err: any, res: Response) => void): Request; + del(url: string, callback?: (err: any, res: Response) => void): Request; + options(url: string, callback?: (err: any, res: Response) => void): Request; + patch(url: string, callback?: (err: any, res: Response) => void): Request; + } + + interface Assertions { + status(code: number): any; + header(key: string, value?: string): any; + header(key: string, value?: RegExp): any; + headers: any; + json: any; + // text: any; + // html: any; + redirect: any; + redirectTo(location: string): any; + param(key: string, value?: string): any; + cookie(key: string, value?: string): any; + } + + interface TypeComparison { + ip: any; + } +} + +declare module "chai-http" { + export = chaiHttp; +}