From d0204038a4fa77e04dfcbf48c33125d7d603b21d Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Fri, 26 Apr 2019 13:28:14 -0600 Subject: [PATCH] [nock] Add matchHeader to Interceptor. (#35022) This method is available on the Scope or Interceptor level, however, the type was only included on Scope before. --- types/nock/index.d.ts | 7 +++-- types/nock/nock-tests.ts | 28 +++++++++++++++-- types/nock/tslint.json | 67 +--------------------------------------- 3 files changed, 31 insertions(+), 71 deletions(-) diff --git a/types/nock/index.d.ts b/types/nock/index.d.ts index 958e97bc15..46121765bd 100644 --- a/types/nock/index.d.ts +++ b/types/nock/index.d.ts @@ -1,5 +1,5 @@ -// Type definitions for nock v9.3.3 -// Project: https://github.com/node-nock/nock +// Type definitions for nock 10.0 +// Project: https://github.com/nock/nock // Definitions by: bonnici // Horiuchi_H // afharo @@ -86,7 +86,7 @@ declare namespace nock { } export interface Interceptor { - query(params: boolean | { (querObject: any): boolean; } | any): this; + query(params: boolean | { (queryObject: any): boolean; } | any): this; reply(responseCode: number, body?: string | any, headers?: HttpHeaders): Scope; reply(responseCode: number, callback: (uri: string, body: string, cb?: ReplyCallback) => ReplyCallbackResult, headers?: HttpHeaders): Scope; @@ -94,6 +94,7 @@ declare namespace nock { replyWithError(errorMessage: string | any): Scope; replyWithFile(responseCode: number, fileName: string, headers?: HttpHeaders): Scope; + matchHeader(name: string, value: string | RegExp | { (value: string): boolean; }): this; basicAuth(options: { user: string; pass?: string; }): this; times(newCounter: number): this; diff --git a/types/nock/nock-tests.ts b/types/nock/nock-tests.ts index 76f1e0762c..e156481ec7 100644 --- a/types/nock/nock-tests.ts +++ b/types/nock/nock-tests.ts @@ -311,7 +311,7 @@ var scope = nock('http://www.google.com') /// Access original request and headers var scope = nock('http://www.google.com') .get('/cat-poems') - .reply(function (uri, requestBody) { + .reply(function(uri, requestBody) { console.log('path:', this.req.path); console.log('headers:', this.req.headers); // ... @@ -519,7 +519,7 @@ var scope = nock('http://api.myservice.com') .post('/', 'ABC') .reply(201, 'OK'); -// Request Headers Matching +// Request Headers Matching on the Scope Level var scope = nock('http://api.myservice.com') .matchHeader('accept', 'application/json') .get('/') @@ -543,6 +543,30 @@ var scope = nock('http://api.myservice.com') data: 'hello world' }) +// Request Headers Matching on the Interceptor Level +var scope = nock('http://api.myservice.com') + .get('/') + .matchHeader('accept', 'application/json') + .reply(200, { + data: 'hello world' + }) + +var scope = nock('http://api.myservice.com') + .get('/') + .matchHeader('User-Agent', /Mozilla\/.*/) + .reply(200, { + data: 'hello world' + }) + +var scope = nock('http://api.myservice.com') + .get('/') + .matchHeader('content-length', (val) => { + return Number(val) >= 1000; + }) + .reply(200, { + data: 'hello world' + }) + // Allow unmocked requests on a mocked hostname options = {allowUnmocked: true}; var scope = nock('http://my.existing.service.com', options) diff --git a/types/nock/tslint.json b/types/nock/tslint.json index 3d59f55fda..6c83e2e70e 100644 --- a/types/nock/tslint.json +++ b/types/nock/tslint.json @@ -1,80 +1,15 @@ { "extends": "dtslint/dt.json", "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, "comment-format": false, - "dt-header": false, - "npm-naming": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false + "strict-export-declare-modifiers": false } }