DefinitelyTyped/restify/restify-tests.ts
James Trinklein 53c667d1a5 Update restify typings (#9379)
* restify: update Request, fix id type

change id from string property to function returning string
add getId() alias
include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L214

* restify: update Request, add getPath()

include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L234

* restify: update Request, fix query type

define query as function returning a string
add getQuery() alias
include JSDoc from source:
https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L247

* restify: update Request, fix time type

change time from number property to function returning number
include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L262

* restify: update Request, add getUrl

include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L273

* restify: update Request, add getVersion

getVersion is a function that returns a string
add version() alias
include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L288

* restify: update Request, fix header type

change header return type from any to string
include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L316

* restify: update Request, add trailer

trailer is a function that returns a string
trailer requires a string parameter called name
trailer optionally takes a second string parameter for default value

include JSDoc from source:
https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L338

* restify: update Request, add JSDoc

include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L359

* restify: update Request, add isChunked

isChunked is a function that returns a boolean
include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L394

* restify: update Request, add isKeepAlive

isKeepAlive is a function that returns a boolean
include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L405

* restify: update Request, add JSDoc for isSecure

include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L426

* restify: update Request, add isUpgradeRequest

isUpgradeRequest is a function that returns a boolean
include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L442

* restify: update Request, add isUpload

isUpload is a function that returns a boolean
include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L457

* restify: update Request, add userAgent

userAgent is a function that returns a boolean
include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L494

* restify: update Request, add start/endHandlerTimer

startHandlerTimer is a function that takes a string
endHandlerTimer is a function that takes a string

include JSDoc from source:
https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L505

* restify: update Request, add absoluteUri

absoluteUri is a function that takes a string and returns a string
include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L52

* restify: update Request, fix accepts

accepts can take a parameter of either string or array of strings
include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L68

* restify: update Request, add acceptsEncoding

acceptsEncoding is a function that returns a boolean
acceptsEncoding can take a parameter of either string or array of strings

include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L96

* restify: update Request, fix contentLength

add getContentLength which is a function which returns a number
contentLength is an alias for getContentLength function

include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L116

* restify: update Request, fix contentType

add getContentType which is a function which returns a string
contentType is an alias for getContentType function

include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L152

* restify: update Request, add getHref

getHref is a function which returns a string
href is an alias to getHref

include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L202

* restify: update Request, add timers

timers is an array of handler timer objects
a handler timer object contains a string name and tuple of numbers representing the elapsed time
this is the only way to access times from start/endHandlerTimer()
from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L557

* restify: update RouteSpec, fix path

path can be either a string or RegExp
also shown in RouteOptions.path
2016-06-02 23:52:47 +09:00

349 lines
10 KiB
TypeScript

/// <reference path="restify.d.ts" />
import restify = require("restify");
import url = require("url");
var server = restify.createServer({
formatters: {
'application/foo': function formatFoo(req: restify.Request, res: restify.Response, body: any) {
if (body instanceof Error)
return body.stack;
if (body)
return body.toString('base64');
return body;
}
}
});
server = restify.createServer({
certificate: "test",
key: "test",
formatters: {},
log: {},
name: "test",
spdy: {},
version: "",
responseTimeHeader: "",
responseTimeFormatter : (durationInMilliseconds: number) => {}
});
server.pre(restify.pre.sanitizePath());
server.on('someEvent', ()=>{});
server.use((req, res, next)=>{});
server.use([(req, res, next)=>{}]);
server.use((req, res, next)=>{}, (req, res, next)=>{});
function send(req: restify.Request, res: restify.Response, next: restify.Next) {
req.header('key', 'val');
req.header('key') === 'val';
req.trailer('key', 'val');
req.trailer('key') === 'val';
req.accepts('test') === true;
req.accepts(['test']) === true;
req.acceptsEncoding('test') === true;
req.acceptsEncoding(['test']) === true;
req.is('test') === true;
req.isChunked() === true;
req.isKeepAlive() === true;
req.isSecure() === true;
req.isUpgradeRequest() === true;
req.isUpload() === true;
req.userAgent() === 'test';
req.startHandlerTimer('test');
req.endHandlerTimer('test');
req.absoluteUri('test') === 'test';
req.timers.pop() === { name: 'test', time: [0, 1234] };
req.getLogger('test');
var log = req.log;
log.debug({params: req.params}, 'Hello there %s', 'foo');
req.getContentLength() === 50;
req.contentLength() === 50;
req.getContentType() === 'test';
req.contentType() === 'test';
req.getHref() === 'test';
req.href() === 'test';
req.getId() === 'test';
req.id() === 'test';
req.getPath() === 'test';
req.path() === 'test';
req.getQuery() === 'test';
req.query() === 'test';
req.secure === true;
req.time() === 1463518410080;
req.getUrl() === url.parse('http://test.test.test/test');
req.getVersion() === 'test';
req.version() === 'test';
req.params;
res.header('test');
res.header('test', {});
res.header('test', new Date());
res.cache();
res.cache('testst', {});
res.status(344);
res.send({hello: 'world'});
res.send(201, {hello: 'world'});
res.send(new restify.BadRequestError('meh'));
res.json(201, {hello: 'world'});
res.json({hello: 'world'});
res.code === 50;
res.contentLength === 50;
res.charSet('test');
res.contentType === 'test';
res.headers;
res.id === 'test';
res.send('hello ' + req.params.name);
return next();
}
server.post('/hello', send);
server.put( '/hello', send);
server.del( '/hello', send);
server.get( '/hello', send);
server.head('/hello', send);
server.opts('/hello', send);
server.post(/(.*)/, send);
server.put( /(.*)/, send);
server.del( /(.*)/, send);
server.get( /(.*)/, send);
server.head(/(.*)/, send);
server.opts(/(.*)/, send);
new restify.BadRequestError();
new restify.UnauthorizedError();
new restify.PaymentRequiredError();
new restify.ForbiddenError();
new restify.NotFoundError();
new restify.MethodNotAllowedError();
new restify.NotAcceptableError();
new restify.ProxyAuthenticationRequiredError();
new restify.RequestTimeoutError();
new restify.ConflictError();
new restify.GoneError();
new restify.LengthRequiredError();
new restify.RequestEntityTooLargeError();
new restify.RequesturiTooLargeError();
new restify.UnsupportedMediaTypeError();
new restify.RequestedRangeNotSatisfiableError();
new restify.ExpectationFailedError();
new restify.ImATeapotError();
new restify.UnprocessableEntityError();
new restify.LockedError();
new restify.FailedDependencyError();
new restify.UnorderedCollectionError();
new restify.UpgradeRequiredError();
new restify.PreconditionRequiredError();
new restify.TooManyRequestsError();
new restify.RequestHeaderFieldsTooLargeError();
new restify.InternalServerError();
new restify.NotImplementedError();
new restify.BadGatewayError();
new restify.ServiceUnavailableError();
new restify.GatewayTimeoutError();
new restify.HttpVersionNotSupportedError();
new restify.VariantAlsoNegotiatesError();
new restify.InsufficientStorageError();
new restify.BandwidthLimitExceededError();
new restify.NotExtendedError();
new restify.NetworkAuthenticationRequiredError();
new restify.RestError();
new restify.BadRequestError(new Error(), 'foo');
new restify.UnauthorizedError(new Error(), 'foo');
new restify.PaymentRequiredError(new Error(), 'foo');
new restify.ForbiddenError(new Error(), 'foo');
new restify.NotFoundError(new Error(), 'foo');
new restify.MethodNotAllowedError(new Error(), 'foo');
new restify.NotAcceptableError(new Error(), 'foo');
new restify.ProxyAuthenticationRequiredError(new Error(), 'foo');
new restify.RequestTimeoutError(new Error(), 'foo');
new restify.ConflictError(new Error(), 'foo');
new restify.GoneError(new Error(), 'foo');
new restify.LengthRequiredError(new Error(), 'foo');
new restify.RequestEntityTooLargeError(new Error(), 'foo');
new restify.RequesturiTooLargeError(new Error(), 'foo');
new restify.UnsupportedMediaTypeError(new Error(), 'foo');
new restify.RequestedRangeNotSatisfiableError(new Error(), 'foo');
new restify.ExpectationFailedError(new Error(), 'foo');
new restify.ImATeapotError(new Error(), 'foo');
new restify.UnprocessableEntityError(new Error(), 'foo');
new restify.LockedError(new Error(), 'foo');
new restify.FailedDependencyError(new Error(), 'foo');
new restify.UnorderedCollectionError(new Error(), 'foo');
new restify.UpgradeRequiredError(new Error(), 'foo');
new restify.PreconditionRequiredError(new Error(), 'foo');
new restify.TooManyRequestsError(new Error(), 'foo');
new restify.RequestHeaderFieldsTooLargeError(new Error(), 'foo');
new restify.InternalServerError(new Error(), 'foo');
new restify.NotImplementedError(new Error(), 'foo');
new restify.BadGatewayError(new Error(), 'foo');
new restify.ServiceUnavailableError(new Error(), 'foo');
new restify.GatewayTimeoutError(new Error(), 'foo');
new restify.HttpVersionNotSupportedError(new Error(), 'foo');
new restify.VariantAlsoNegotiatesError(new Error(), 'foo');
new restify.InsufficientStorageError(new Error(), 'foo');
new restify.BandwidthLimitExceededError(new Error(), 'foo');
new restify.NotExtendedError(new Error(), 'foo');
new restify.NetworkAuthenticationRequiredError(new Error(), 'foo');
new restify.RestError(new Error(), 'foo');
new restify.PreconditionFailedError(new Error(), 'foo');
new restify.BadDigestError(new Error(), 'foo');
new restify.BadMethodError(new Error(), 'foo');
new restify.InternalError(new Error(), 'foo');
new restify.InvalidArgumentError(new Error(), 'foo');
new restify.InvalidContentError(new Error(), 'foo');
new restify.InvalidCredentialsError(new Error(), 'foo');
new restify.InvalidHeaderError(new Error(), 'foo');
new restify.InvalidVersionError(new Error(), 'foo');
new restify.MissingParameterError(new Error(), 'foo');
new restify.NotAuthorizedError(new Error(), 'foo');
new restify.RequestExpiredError(new Error(), 'foo');
new restify.RequestThrottledError(new Error(), 'foo');
new restify.ResourceNotFoundError(new Error(), 'foo');
new restify.WrongAcceptError(new Error(), 'foo');
new restify.PreconditionFailedError();
new restify.BadDigestError();
new restify.BadMethodError();
new restify.InternalError();
new restify.InvalidArgumentError();
new restify.InvalidContentError();
new restify.InvalidCredentialsError();
new restify.InvalidHeaderError();
new restify.InvalidVersionError();
new restify.MissingParameterError();
new restify.NotAuthorizedError();
new restify.RequestExpiredError();
new restify.RequestThrottledError();
new restify.ResourceNotFoundError();
new restify.WrongAcceptError();
server.name = "";
server.version = "";
server.log = {};
server.acceptable = ["test"];
server.url = "";
server.address().port;
server.address().family;
server.address().address;
server.listen("somePath", send);
server.close();
server.use(restify.acceptParser(server.acceptable));
server.use(restify.authorizationParser());
server.use(restify.dateParser());
server.use(restify.queryParser());
server.use(restify.jsonp());
server.use(restify.gzipResponse());
server.use(restify.bodyParser());
server.use(restify.throttle({
burst: 100,
rate: 50,
ip: true,
overrides: {
'192.168.1.1': {
rate: 0,
burst: 0
}
}
}));
server.on('after', restify.auditLogger({
log: ()=>{}
}));
server.on('after', (req: restify.Request, res: restify.Response, route: restify.Route, err: any) => {
route.spec.method === 'GET';
route.spec.name === 'routeName';
route.spec.path === '/some/path';
route.spec.path === /\/some\/path\/.*/;
route.spec.versions === ['v1'];
restify.auditLogger({ log: ()=>{} })(req, res, route, err);
});
restify.defaultResponseHeaders = function(data: any) {
this.header('Server', 'helloworld');
};
restify.defaultResponseHeaders = false;
//RESTIFY Client Tests
var client = restify.createJsonClient({
url: 'https://api.us-west-1.joyentcloud.com',
version: '*'
});
client = restify.createStringClient({
accept: "test",
connectTimeout: 30,
dtrace: {},
gzip: {},
headers: {},
log: {},
retry: {},
signRequest: ()=>{},
url: "",
userAgent: "",
version: ""
});
client.head('test', function(err: any, req: restify.Request, res: restify.Response) { });
client.put('path', {}, function(err: any, req: restify.Request, res: restify.Response, obj: any) { });
client.del('path', function(err: any, req: restify.Request, res: restify.Response) { });
client.post('/foo', { hello: 'world' }, function(err: any, req: restify.Request, res: restify.Response, obj: any) {
console.log('%d -> %j', res.statusCode, res.headers);
console.log('%j', obj);
});
client.get('/foo/bar', function(err: any, req: restify.Request, res: restify.Response, data: string) {
console.log('%s', data);
});
var client2 = restify.createClient({
url: 'http://127.0.0.1'
});
client2.get('/str/mcavage', function(err: any, req: any) {
req.on('result', function(err: any, res: any) {
res.body = '';
res.setEncoding('utf8');
res.on('data', function(chunk: string) {
res.body += chunk;
});
res.on('end', function() {
console.log(res.body);
});
});
});
client.basicAuth('test', 'password');
client2.basicAuth('test', 'password');