diff --git a/superagent/superagent-tests.ts b/superagent/superagent-tests.ts
index 5779f81e9..436874aaf 100644
--- a/superagent/superagent-tests.ts
+++ b/superagent/superagent-tests.ts
@@ -1,13 +1,249 @@
-///
+///
+///
-import superagent = require('superagent')
+// via: http://visionmedia.github.io/superagent/
-var agent = superagent.agent();
-agent
- .post('http://localhost:3000/signin')
- .send({ email: 'test@dummy.com', password: 'bacon' })
- .end((err, res) => {
- if (err) throw err;
- if (res.status !== 200) throw new Error('bad status ' + res.status);
- if (res.body.foo === 'bar') throw new Error('bad body');
+import request = require('superagent')
+import fs = require('fs');
+
+request
+ .post('/api/pet')
+ .send({ name: 'Manny', species: 'cat' })
+ .set('X-API-Key', 'foobar')
+ .set('Accept', 'application/json')
+ .end((res: request.Response) => {
+ if (res.ok) {
+ console.log('yay got ' + JSON.stringify(res.body));
+ } else {
+ console.log('Oh no! error ' + res.text);
+ }
});
+
+var agent = request.agent();
+agent
+ .post('/api/pet')
+ .send({ name: 'Manny', species: 'cat' })
+ .set('X-API-Key', 'foobar')
+ .set('Accept', 'application/json')
+ .end((res: request.Response) => {
+ if (res.error) {
+ console.log('oh no ' + res.error.message);
+ } else {
+ console.log('got ' + res.status + ' response');
+ }
+ });
+
+
+var callback = (res: request.Response) => {};
+
+// Request basics
+request
+ .get('/search')
+ .end(callback);
+
+request('GET', '/search')
+ .end(callback);
+
+request
+ .head('/favicon.ico')
+ .end(callback);
+
+request
+ .del('/user/1')
+ .end(callback);
+
+request('/search')
+ .end(callback);
+
+// Setting header fields
+request
+ .get('/search')
+ .set('API-Key', 'foobar')
+ .set('Accept', 'application/json')
+ .end(callback);
+
+request
+ .get('/search')
+ .set({ 'API-Key': 'foobar', Accept: 'application/json' })
+ .end(callback);
+
+// GET requests
+request
+ .get('/search')
+ .query({ query: 'Manny' })
+ .query({ range: '1..5' })
+ .query({ order: 'desc' })
+ .end(callback);
+
+request
+ .get('/search')
+ .query({ query: 'Manny', range: '1..5', order: 'desc' })
+ .end(callback);
+
+request
+ .get('/querystring')
+ .query('search=Manny&range=1..5')
+ .end(callback);
+
+request
+ .get('/querystring')
+ .query('search=Manny')
+ .query('range=1..5')
+ .end(callback);
+
+// POST / PUT requests
+request.post('/user')
+ .set('Content-Type', 'application/json')
+ .send('{"name":"tj","pet":"tobi"}')
+ .end(callback);
+
+request.post('/user')
+ .send({ name: 'tj', pet: 'tobi' })
+ .end(callback);
+
+request.post('/user')
+ .send({ name: 'tj' })
+ .send({ pet: 'tobi' })
+ .end(callback);
+
+request.post('/user')
+ .send('name=tj')
+ .send('pet=tobi')
+ .end(callback);
+
+request.post('/user')
+ .type('form')
+ .send({ name: 'tj' })
+ .send({ pet: 'tobi' })
+ .end(callback);
+
+// Setting the Content-Type
+request.post('/user')
+ .set('Content-Type', 'application/json');
+
+request.post('/user')
+ .type('application/json');
+
+request.post('/user')
+ .type('json');
+
+request.post('/user')
+ .type('png');
+
+request.get('/user')
+ .accept('application/json');
+
+request.get('/user')
+ .accept('json');
+
+request.get('/user')
+ .accept('png');
+
+// Query strings
+request
+ .post('/')
+ .query({ format: 'json' })
+ .query({ dest: '/login' })
+ .send({ post: 'data', here: 'wahoo' })
+ .end(callback);
+
+// Parsing response bodies
+request('/search')
+ .end((res: request.Response) => {
+ var status: number = res.status;
+ var body = res.body;
+ var files: Object = res.files;
+ var text: string = res.text;
+ var contentLength = res.header['content-length'];
+ var contentType: string = res.type;
+ var charset: string = res.charset;
+ });
+
+var req = request.get('/hoge');
+// Aborting requests
+req.abort();
+
+// Request timeouts
+req.timeout(100);
+
+// Basic authentication
+request.get('http://tobi:learnboost@local').end(callback);
+request
+ .get('http://local')
+ .auth('tobo', 'learnboost')
+ .end(callback);
+
+// Following redirects
+request
+ .get('/some.png')
+ .redirects(2)
+ .end(callback);
+
+// Piping data
+/*
+(function() {
+var stream = fs.createReadStream('path/to/my.json');
+var req = request.post('/somewhere');
+req.type('json');
+stream.pipe(req);
+})();
+*/
+
+(function() {
+var stream = fs.createWriteStream('path/to/my.json');
+var req = request.get('/some.json');
+req.pipe(stream);
+})();
+
+// Multipart requests
+(function() {
+var req = request.post('/upload');
+
+req.part()
+ .set('Content-Type', 'image/png')
+ .set('Content-Disposition', 'attachment; filename="myimage.png"')
+ .write('some image data')
+ .write('some more image data');
+
+req.part()
+ .set('Content-Disposition', 'form-data; name="name"')
+ .set('Content-Type', 'text/plain')
+ .write('tobi');
+
+req.end(callback);
+})();
+
+// Attaching files
+request
+ .post('/upload')
+ .attach('avatar', 'path/to/tobi.png', 'user.png')
+ .attach('image', 'path/to/loki.png')
+ .attach('file', 'path/to/jane.png')
+ .end(callback);
+
+// Field values
+request
+ .post('/upload')
+ .field('user[name]', 'Tobi')
+ .field('user[email]', 'tobi@learnboost.com')
+ .attach('image', 'path/to/tobi.png')
+ .end(callback);
+
+// CORS
+request
+ .get('http://localhost:4001/')
+ .withCredentials()
+ .end(callback);
+
+// Error handling
+request
+ .post('/upload')
+ .attach('image', 'path/to/tobi.png')
+ .end((err: any, res: request.Response): void => {});
+request
+ .post('/upload')
+ .attach('image', 'path/to/tobi.png')
+ .on('error', (err: any) => {})
+ .end(callback);
+
+
diff --git a/superagent/superagent.d.ts b/superagent/superagent.d.ts
index 0abed7438..ef477e16a 100644
--- a/superagent/superagent.d.ts
+++ b/superagent/superagent.d.ts
@@ -6,85 +6,108 @@
///
declare module "superagent" {
- import stream = require('stream');
- export interface Response {
- text: string;
- body: any;
- files: any;
- header: any;
- type: string;
- charset: string;
- status: number;
- statusType: number;
- info: boolean;
- ok: boolean;
- redirect: boolean;
- clientError: boolean;
- serverError: boolean;
- error: any;
- accepted: boolean;
- noContent: boolean;
- badRequest: boolean;
- unauthorized: boolean;
- notAcceptable: boolean;
- notFound: boolean;
- forbidden: boolean;
- get(header: string): string;
+ import stream = require('stream');
+
+ type CallbackHandler = { (err: any, res: request.Response): void; }|{ (res: request.Response): void; };
+
+ var request: request.SuperAgentStatic;
+
+ module request {
+ interface SuperAgentStatic extends SuperAgent {
+ (url: string): SuperAgentRequest;
+ (method: string, url: string): SuperAgentRequest;
+
+ agent(): SuperAgent;
+ }
+
+ interface SuperAgent> extends stream.Stream {
+ get(url: string, callback?: CallbackHandler): Req;
+ post(url: string, callback?: CallbackHandler): Req;
+ put(url: string, callback?: CallbackHandler): Req;
+ head(url: string, callback?: CallbackHandler): Req;
+ del(url: string, callback?: CallbackHandler): Req;
+ options(url: string, callback?: CallbackHandler): Req;
+ trace(url: string, callback?: CallbackHandler): Req;
+ copy(url: string, callback?: CallbackHandler): Req;
+ lock(url: string, callback?: CallbackHandler): Req;
+ mkcol(url: string, callback?: CallbackHandler): Req;
+ move(url: string, callback?: CallbackHandler): Req;
+ purge(url: string, callback?: CallbackHandler): Req;
+ propfind(url: string, callback?: CallbackHandler): Req;
+ proppatch(url: string, callback?: CallbackHandler): Req;
+ unlock(url: string, callback?: CallbackHandler): Req;
+ report(url: string, callback?: CallbackHandler): Req;
+ mkactivity(url: string, callback?: CallbackHandler): Req;
+ checkout(url: string, callback?: CallbackHandler): Req;
+ merge(url: string, callback?: CallbackHandler): Req;
+ // m-search(url: string, callback?: CallbackHandler): Req;
+ notify(url: string, callback?: CallbackHandler): Req;
+ subscribe(url: string, callback?: CallbackHandler): Req;
+ unsubscribe(url: string, callback?: CallbackHandler): Req;
+ patch(url: string, callback?: CallbackHandler): Req;
+ search(url: string, callback?: CallbackHandler): Req;
+ connect(url: string, callback?: CallbackHandler): Req;
+
+ parse(fn: Function): Req;
+ saveCookies(res: Response): void;
+ attachCookies(req: Req): void;
+ }
+
+ interface Response extends NodeJS.ReadableStream {
+ text: string;
+ body: any;
+ files: any;
+ header: any;
+ type: string;
+ charset: string;
+ status: number;
+ statusType: number;
+ info: boolean;
+ ok: boolean;
+ redirect: boolean;
+ clientError: boolean;
+ serverError: boolean;
+ error: Error;
+ accepted: boolean;
+ noContent: boolean;
+ badRequest: boolean;
+ unauthorized: boolean;
+ notAcceptable: boolean;
+ notFound: boolean;
+ forbidden: boolean;
+ get(header: string): string;
+ }
+
+ interface Request> /* extends NodeJS.WritableStream */ {
+ abort(): void;
+ accept(type: string): Req;
+ attach(field: string, file: string, filename?: string): Req;
+ auth(user: string, name: string): Req;
+ buffer(val: boolean): Req;
+ clearTimeout(): Req;
+ end(callback?: CallbackHandler): Req;
+ field(name: string, val: string): Req;
+ get(field: string): string;
+ on(name: string, handler: Function): Req;
+ on(name: 'error', handler: (err: any) => void): Req;
+ part(): Req;
+ pipe(stream: NodeJS.WritableStream, options?: Object): stream.Writable;
+ query(val: Object): Req;
+ redirects(n: number): Req;
+ send(data: string): Req;
+ send(data: Object): Req;
+ set(field: string, val: string): Req;
+ set(field: Object): Req;
+ timeout(ms: number): Req;
+ type(val: string): Req;
+ withCredentials(): Req;
+ write(data: string, encoding?: string): Req;
+ write(data: Buffer, encoding?: string): Req;
+ }
+ interface SuperAgentRequest extends Request>>> {}
+
}
- export interface Request {
- attach(field: string, file: string, filename: string): Request;
- redirects(n: number): Request;
- part(): Request;
- set(field: string, val: string): Request;
- set(field: Object): Request;
- get(field: string): string;
- type(val: string): Request;
- query(val: Object): Request;
- send(data: string): Request;
- send(data: Object): Request;
- write(data: string, encoding: string): boolean;
- write(data: Buffer, encoding: string): boolean;
- pipe(stream: NodeJS.WritableStream, options?: Object): stream.Writable;
- buffer(val: boolean): Request;
- timeout(ms: number): Request;
- clearTimeout(): Request;
- abort(): void;
- auth(user: string, name: string): Request;
- field(name: string, val: string): Request;
- end(callback?: (err: Error, res: Response) => void): Request;
- }
-
- export interface Agent {
- get(url: string, callback?: (err: Error, res: Response) => void): Request;
- post(url: string, callback?: (err: Error, res: Response) => void): Request;
- put(url: string, callback?: (err: Error, res: Response) => void): Request;
- head(url: string, callback?: (err: Error, res: Response) => void): Request;
- del(url: string, callback?: (err: Error, res: Response) => void): Request;
- options(url: string, callback?: (err: Error, res: Response) => void): Request;
- trace(url: string, callback?: (err: Error, res: Response) => void): Request;
- copy(url: string, callback?: (err: Error, res: Response) => void): Request;
- lock(url: string, callback?: (err: Error, res: Response) => void): Request;
- mkcol(url: string, callback?: (err: Error, res: Response) => void): Request;
- move(url: string, callback?: (err: Error, res: Response) => void): Request;
- propfind(url: string, callback?: (err: Error, res: Response) => void): Request;
- proppatch(url: string, callback?: (err: Error, res: Response) => void): Request;
- unlock(url: string, callback?: (err: Error, res: Response) => void): Request;
- report(url: string, callback?: (err: Error, res: Response) => void): Request;
- mkactivity(url: string, callback?: (err: Error, res: Response) => void): Request;
- checkout(url: string, callback?: (err: Error, res: Response) => void): Request;
- merge(url: string, callback?: (err: Error, res: Response) => void): Request;
- //m-search(url: string, callback?: (err: Error, res: Response) => void): Request;
- notify(url: string, callback?: (err: Error, res: Response) => void): Request;
- subscribe(url: string, callback?: (err: Error, res: Response) => void): Request;
- unsubscribe(url: string, callback?: (err: Error, res: Response) => void): Request;
- patch(url: string, callback?: (err: Error, res: Response) => void): Request;
- search(url: string, callback?: (err: Error, res: Response) => void): Request;
- connect(url: string, callback?: (err: Error, res: Response) => void): Request;
- parse(fn: Function): Request;
- saveCookies(res: Response): void;
- attachCookies(req: Request): void;
- }
-
- export function agent(): Agent;
+ export = request;
}
+
diff --git a/supertest/supertest-tests.ts b/supertest/supertest-tests.ts
index 120cb43c0..d76c97ea4 100644
--- a/supertest/supertest-tests.ts
+++ b/supertest/supertest-tests.ts
@@ -11,7 +11,7 @@ supertest(app)
.expect('Content-Type', /json/)
.expect('Content-Length', '20')
.expect(201)
- .end((err, res) => {
+ .end((err: any, res: supertest.Response) => {
if (err) throw err;
});
@@ -20,13 +20,13 @@ var request = supertest(app);
var agent = supertest.agent();
request
.post('/login')
- .end((err, res) => {
+ .end((err: any, res: supertest.Response) => {
if (err) throw err;
agent.saveCookies(res);
var req = request.get('/admin');
agent.attachCookies(req);
- req.expect(200, (err, res) => {
+ req.expect(200, (err: any, res: supertest.Response) => {
if (err) throw err;
});
});
@@ -35,11 +35,11 @@ request
var client = supertest.agent(app);
client
.post('/login')
- .end((err, res) => {
+ .end((err: any, res: supertest.Response) => {
if (err) throw err;
client.get('/admin')
- .expect(200, (err, res) => {
+ .expect(200, (err: any, res: supertest.Response) => {
if (err) throw err;
});
});
@@ -48,7 +48,7 @@ client
supertest(app)
.get('/')
.expect(hasPreviousAndNextKeys)
- .end((err, res) => {
+ .end((err: any, res: supertest.Response) => {
if (err) throw err;
});
diff --git a/supertest/supertest.d.ts b/supertest/supertest.d.ts
index cb2bb78b3..dccbd47c8 100644
--- a/supertest/supertest.d.ts
+++ b/supertest/supertest.d.ts
@@ -8,93 +8,32 @@
declare module "supertest" {
import superagent = require('superagent');
- module supertest {
- interface Test extends superagent.Request {
- url: string;
- serverAddress(app: any, path: string): string;
- expect(status: number, callback?: (err: Error, res: Response) => void): Test;
- expect(status: number, body: string, callback?: (err: Error, res: Response) => void): Test;
- expect(body: string, callback?: (err: Error, res: Response) => void): Test;
- expect(body: RegExp, callback?: (err: Error, res: Response) => void): Test;
- expect(body: Object, callback?: (err: Error, res: Response) => void): Test;
- expect(field: string, val: string, callback?: (err: Error, res: Response) => void): Test;
- expect(field: string, val: RegExp, callback?: (err: Error, res: Response) => void): Test;
- expect(checker: (res: Response) => any): Test;
-
- attach(field: string, file: string, filename: string): Test;
- redirects(n: number): Test;
- part(): Test;
- set(field: string, val: string): Test;
- set(field: Object): Test;
- type(val: string): Test;
- query(val: Object): Test;
- send(data: string): Test;
- send(data: Object): Test;
- buffer(val: boolean): Test;
- timeout(ms: number): Test;
- clearTimeout(): Test;
- auth(user: string, name: string): Test;
- field(name: string, val: string): Test;
- end(callback?: (err: Error, res: Response) => void): Test;
- }
- interface Response extends superagent.Response {}
-
- interface SuperTest {
- get(url: string): Test;
- post(url: string): Test;
- put(url: string): Test;
- head(url: string): Test;
- del(url: string): Test;
- options(url: string): Test;
- trace(url: string): Test;
- copy(url: string): Test;
- lock(url: string): Test;
- mkcol(url: string): Test;
- move(url: string): Test;
- propfind(url: string): Test;
- proppatch(url: string): Test;
- unlock(url: string): Test;
- report(url: string): Test;
- mkactivity(url: string): Test;
- checkout(url: string): Test;
- merge(url: string): Test;
- //m-search(url: string): Test;
- notify(url: string): Test;
- subscribe(url: string): Test;
- unsubscribe(url: string): Test;
- patch(url: string): Test;
- }
-
- interface TestAgent extends superagent.Agent {
- get(url: string, callback?: (err: Error, res: Response) => void): Test;
- post(url: string, callback?: (err: Error, res: Response) => void): Test;
- put(url: string, callback?: (err: Error, res: Response) => void): Test;
- head(url: string, callback?: (err: Error, res: Response) => void): Test;
- del(url: string, callback?: (err: Error, res: Response) => void): Test;
- options(url: string, callback?: (err: Error, res: Response) => void): Test;
- trace(url: string, callback?: (err: Error, res: Response) => void): Test;
- copy(url: string, callback?: (err: Error, res: Response) => void): Test;
- lock(url: string, callback?: (err: Error, res: Response) => void): Test;
- mkcol(url: string, callback?: (err: Error, res: Response) => void): Test;
- move(url: string, callback?: (err: Error, res: Response) => void): Test;
- propfind(url: string, callback?: (err: Error, res: Response) => void): Test;
- proppatch(url: string, callback?: (err: Error, res: Response) => void): Test;
- unlock(url: string, callback?: (err: Error, res: Response) => void): Test;
- report(url: string, callback?: (err: Error, res: Response) => void): Test;
- mkactivity(url: string, callback?: (err: Error, res: Response) => void): Test;
- checkout(url: string, callback?: (err: Error, res: Response) => void): Test;
- merge(url: string, callback?: (err: Error, res: Response) => void): Test;
- //m-search(url: string, callback?: (err: Error, res: Response) => void): Test;
- notify(url: string, callback?: (err: Error, res: Response) => void): Test;
- subscribe(url: string, callback?: (err: Error, res: Response) => void): Test;
- unsubscribe(url: string, callback?: (err: Error, res: Response) => void): Test;
- patch(url: string, callback?: (err: Error, res: Response) => void): Test;
- search(url: string, callback?: (err: Error, res: Response) => void): Test;
- connect(url: string, callback?: (err: Error, res: Response) => void): Test;
- }
- function agent(app?: any): supertest.TestAgent;
- }
+ type CallbackHandler = { (err: any, res: supertest.Response): void; }|{ (res: supertest.Response): void; };
function supertest(app: any): supertest.SuperTest;
+
+ module supertest {
+ function agent(app?: any): supertest.SuperTest;
+
+ interface SuperTest extends superagent.SuperAgent {
+ }
+
+ interface Test extends superagent.Request {
+ url: string;
+ serverAddress(app: any, path: string): string;
+ expect(status: number, callback?: CallbackHandler): Test;
+ expect(status: number, body: string, callback?: CallbackHandler): Test;
+ expect(body: string, callback?: CallbackHandler): Test;
+ expect(body: RegExp, callback?: CallbackHandler): Test;
+ expect(body: Object, callback?: CallbackHandler): Test;
+ expect(field: string, val: string, callback?: CallbackHandler): Test;
+ expect(field: string, val: RegExp, callback?: CallbackHandler): Test;
+ expect(checker: (res: Response) => any): Test;
+ }
+
+ interface Response extends superagent.Response {
+ }
+ }
+
export = supertest;
}