From 9ff4ff6377ed8c962b5200facf7a494ed38e7078 Mon Sep 17 00:00:00 2001 From: Jared Date: Mon, 5 Jan 2015 19:41:01 +1300 Subject: [PATCH 1/2] Add additional methods to nock. --- nock/nock-tests.ts | 33 +++++++++++++++++++++++++++++++++ nock/nock.d.ts | 28 +++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/nock/nock-tests.ts b/nock/nock-tests.ts index f3c166aa1a..b64163007f 100644 --- a/nock/nock-tests.ts +++ b/nock/nock-tests.ts @@ -4,29 +4,47 @@ import nock = require('nock'); var inst: nock.Scope; var str: string; +var strings: string[]; var bool: boolean; var data: string; var num: number; var obj: {}; +var objects: Object[]; var value: any; var regex: RegExp; var options: nock.Options; var headers: Object; inst = inst.head(str); + inst = inst.get(str); inst = inst.get(str, data); + +inst = inst.patch(str); +inst = inst.patch(str, str); +inst = inst.patch(str, obj); + inst = inst.post(str); inst = inst.post(str, data); +inst = inst.post(str, obj); + inst = inst.put(str); inst = inst.put(str, data); +inst = inst.put(str, obj); inst = inst.delete(str); inst = inst.delete(str, data); +inst = inst.delete(str, obj); + +inst = inst.merge(str); +inst = inst.merge(str, data); +inst = inst.merge(str, obj); inst = inst.intercept(str, str); inst = inst.intercept(str, str, str); +inst = inst.intercept(str, str, obj); inst = inst.intercept(str, str, str, value); +inst = inst.intercept(str, str, obj, str); inst = inst.reply(num); inst = inst.reply(num, str); @@ -44,6 +62,9 @@ inst = inst.replyWithFile(num, str); inst = inst.defaultReplyHeaders(value); inst = inst.matchHeader(str, str); +inst = inst.delay(num); +inst = inst.delayConnection(num); + inst = inst.filteringPath(regex, str); inst = inst.filteringPath((path: string) => { return str; @@ -61,3 +82,15 @@ inst = inst.log(() => { inst.done(); bool = inst.isDone(); inst.restore(); + +objects = inst.pendingMocks(); + +nock.recorder.rec(); +nock.recorder.rec(true); +nock.recorder.rec({ + dont_print: true, + output_objects: true +}); + +strings = nock.recorder.play(); +objects = nock.recorder.play(); \ No newline at end of file diff --git a/nock/nock.d.ts b/nock/nock.d.ts index 18119e1db0..6cafb54c61 100644 --- a/nock/nock.d.ts +++ b/nock/nock.d.ts @@ -22,11 +22,26 @@ declare module "nock" { export interface Scope { get(path: string, data?: string): Scope; + post(path: string, data?: string): Scope; + post(path: string, data?: Object): Scope; + + patch(path: string, data?: string): Scope; + patch(path: string, data?: Object): Scope; + put(path: string, data?: string): Scope; + put(path: string, data?: Object): Scope; + head(path: string): Scope; + delete(path: string, data?: string): Scope; + delete(path: string, data?: Object): Scope; + + merge(path: string, data?: string): Scope; + merge(path: string, data?: Object): Scope; + intercept(path: string, verb: string, body?: string, options?: any): Scope; + intercept(path: string, verb: string, body?: Object, options?: any): Scope; reply(responseCode: number, body?: string, headers?: Object): Scope; reply(responseCode: number, body?: Object, headers?: Object): Scope; @@ -44,18 +59,29 @@ declare module "nock" { persist(): Scope; log(out: () => void): Scope; + delay(timeMs: number): Scope; + delayConnection(timeMs: number): Scope; + done(): void; isDone(): boolean; restore(): void; + pendingMocks(): Object[]; } export interface Recorder { rec(capture?: boolean): void; - play(): string[]; + rec(options?: RecorderOptions): void; + play(): any[]; } export interface Options { allowUnmocked?: boolean; } + + export interface RecorderOptions { + dont_print?: boolean; + output_objects?: boolean; + enable_reqheaders_recording?: boolean; + } } } From 63db9fec7db859b46af50c8d9d084727b0ccf69e Mon Sep 17 00:00:00 2001 From: Jared Date: Tue, 6 Jan 2015 19:09:49 +1300 Subject: [PATCH 2/2] Add Regexp support, request repeating and include version number. --- nock/nock-tests.ts | 14 +++++++++++++- nock/nock.d.ts | 13 ++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/nock/nock-tests.ts b/nock/nock-tests.ts index b64163007f..a209a8c941 100644 --- a/nock/nock-tests.ts +++ b/nock/nock-tests.ts @@ -23,28 +23,35 @@ inst = inst.get(str, data); inst = inst.patch(str); inst = inst.patch(str, str); inst = inst.patch(str, obj); +inst = inst.patch(str, regex); inst = inst.post(str); inst = inst.post(str, data); inst = inst.post(str, obj); +inst = inst.post(str, regex); inst = inst.put(str); inst = inst.put(str, data); inst = inst.put(str, obj); +inst = inst.put(str, regex); inst = inst.delete(str); inst = inst.delete(str, data); inst = inst.delete(str, obj); +inst = inst.delete(str, regex); inst = inst.merge(str); inst = inst.merge(str, data); inst = inst.merge(str, obj); +inst = inst.merge(str, regex); inst = inst.intercept(str, str); inst = inst.intercept(str, str, str); inst = inst.intercept(str, str, obj); +inst = inst.intercept(str, str, regex); inst = inst.intercept(str, str, str, value); -inst = inst.intercept(str, str, obj, str); +inst = inst.intercept(str, str, obj, value); +inst = inst.intercept(str, str, regex, value); inst = inst.reply(num); inst = inst.reply(num, str); @@ -59,6 +66,11 @@ inst = inst.reply(num, (uri: string, body: string) => { }, headers); inst = inst.replyWithFile(num, str); +inst = inst.times(4); +inst = inst.once(); +inst = inst.twice(); +inst = inst.thrice(); + inst = inst.defaultReplyHeaders(value); inst = inst.matchHeader(str, str); diff --git a/nock/nock.d.ts b/nock/nock.d.ts index 6cafb54c61..bcf4560ce3 100644 --- a/nock/nock.d.ts +++ b/nock/nock.d.ts @@ -1,4 +1,4 @@ -// Type definitions for nock +// Type definitions for nock v0.54.0 // Project: https://github.com/pgte/nock // Definitions by: bonnici // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -25,23 +25,29 @@ declare module "nock" { post(path: string, data?: string): Scope; post(path: string, data?: Object): Scope; + post(path: string, regex?: RegExp): Scope; patch(path: string, data?: string): Scope; patch(path: string, data?: Object): Scope; + patch(path: string, regex?: RegExp): Scope; put(path: string, data?: string): Scope; put(path: string, data?: Object): Scope; + put(path: string, regex?: RegExp): Scope; head(path: string): Scope; delete(path: string, data?: string): Scope; delete(path: string, data?: Object): Scope; + delete(path: string, regex?: RegExp): Scope; merge(path: string, data?: string): Scope; merge(path: string, data?: Object): Scope; + merge(path: string, regex?: RegExp): Scope; intercept(path: string, verb: string, body?: string, options?: any): Scope; intercept(path: string, verb: string, body?: Object, options?: any): Scope; + intercept(path: string, verb: string, body?: RegExp, options?: any): Scope; reply(responseCode: number, body?: string, headers?: Object): Scope; reply(responseCode: number, body?: Object, headers?: Object): Scope; @@ -62,6 +68,11 @@ declare module "nock" { delay(timeMs: number): Scope; delayConnection(timeMs: number): Scope; + times(repeats: number): Scope; + once(): Scope; + twice(): Scope; + thrice(): Scope; + done(): void; isDone(): boolean; restore(): void;