From c268497e1fd848d3423d379a2671361ea56c9b53 Mon Sep 17 00:00:00 2001 From: Timothy Jones Date: Mon, 6 Sep 2021 22:11:13 +1000 Subject: [PATCH] fix: You no longer need to import the verifier from /v3, it can be imported directly from @pact-foundation/pact --- src/v3/verifier.spec.ts | 50 -------------- src/v3/verifier.ts | 147 ++++------------------------------------ 2 files changed, 12 insertions(+), 185 deletions(-) delete mode 100644 src/v3/verifier.spec.ts diff --git a/src/v3/verifier.spec.ts b/src/v3/verifier.spec.ts deleted file mode 100644 index 18eddfbaf..000000000 --- a/src/v3/verifier.spec.ts +++ /dev/null @@ -1,50 +0,0 @@ -import * as mockery from 'mockery'; - -import * as chai from 'chai'; - -const MockNative = { - generate_datetime_string: () => '', - generate_regex_string: () => '', -}; -mockery.registerMock('../../native/index.node', MockNative); - -// eslint-disable-next-line import/first -import { VerifierV3 } from './verifier'; - -const { expect } = chai; - -describe('V3 Verifier', () => { - describe('invalid configuration', () => { - it('returns an error when no provider name is given', () => { - expect( - () => - new VerifierV3({ - logLevel: 'info', - provider: '', - providerBaseUrl: 'http://localhost', - }) - ).to.throw('provider name is required'); - }); - it('returns an error when no pactBrokerUrl and an empty list of pactUrls is given', () => { - expect( - () => - new VerifierV3({ - logLevel: 'info', - provider: 'unitTest', - providerBaseUrl: 'http://localhost', - pactUrls: [], - }) - ).to.throw('a list of pactUrls or a pactBrokerUrl must be provided'); - }); - it('returns an error when no pactBrokerUrl an no pactUrls is given', () => { - expect( - () => - new VerifierV3({ - logLevel: 'info', - provider: 'unitTest', - providerBaseUrl: 'http://localhost', - }) - ).to.throw('a list of pactUrls or a pactBrokerUrl must be provided'); - }); - }); -}); diff --git a/src/v3/verifier.ts b/src/v3/verifier.ts index 5f85cfd59..2ffc0527b 100644 --- a/src/v3/verifier.ts +++ b/src/v3/verifier.ts @@ -1,16 +1,11 @@ -import { isEmpty } from 'ramda'; import { ProxyOptions, StateHandlers } from 'dsl/verifier/proxy/types'; import * as express from 'express'; -import * as http from 'http'; -import * as url from 'url'; -import { localAddresses } from '../common/net'; -import { createProxy, waitForServerReady } from '../dsl/verifier/proxy'; +import { ConsumerVersionSelector } from '@pact-foundation/pact-core'; -import ConfigurationError from '../errors/configurationError'; -import logger, { setLogLevel } from '../common/logger'; +import logger from '../common/logger'; -import * as PactNative from '../../native/index.node'; +import { Verifier } from '../dsl/verifier'; export interface VerifierV3Options { provider: string; @@ -37,144 +32,26 @@ export interface VerifierV3Options { disableSSLVerification?: boolean; } -export interface ConsumerVersionSelector { - pacticipant?: string; - tag?: string; - version?: string; - latest?: boolean; - all?: boolean; -} - -interface InternalVerifierOptions { - consumerVersionSelectorsString?: string[]; -} +export { ConsumerVersionSelector }; export type VerifierOptions = VerifierV3Options & ProxyOptions; export class VerifierV3 { - private config: VerifierOptions; - - private address = 'http://localhost'; - - private stateSetupPath = '/_pactSetup'; + private internalVerifier: Verifier; constructor(config: VerifierOptions) { - this.config = config; - this.validateConfiguration(); + this.internalVerifier = new Verifier(config); } /** * Verify a HTTP Provider */ public verifyProvider(): Promise { - // Start the verification CLI proxy server - const server = createProxy(this.config, this.stateSetupPath); - - // Run the verification once the proxy server is available - // and properly shut down the proxy before returning - return waitForServerReady(server) - .then(this.runProviderVerification()) - .then( - (result: unknown) => - new Promise((resolve) => { - server.close(() => { - resolve(result); - }); - }) - ) - .catch( - (e: Error) => - new Promise((_, reject) => { - server.close(() => { - reject(e); - }); - }) - ); - } - - private validateConfiguration() { - const config: VerifierV3Options & InternalVerifierOptions = { - ...this.config, - }; - - if (this.config.logLevel && !isEmpty(this.config.logLevel)) { - setLogLevel(this.config.logLevel); - } - - if (this.config.validateSSL === undefined) { - this.config.validateSSL = true; - } - - if (this.config.changeOrigin === undefined) { - this.config.changeOrigin = false; - - if (!this.isLocalVerification()) { - this.config.changeOrigin = true; - logger.warn( - `non-local provider address ${this.config.providerBaseUrl} detected, setting 'changeOrigin' to 'true'. This property can be overridden.` - ); - } - } - - if (isEmpty(this.config)) { - throw new ConfigurationError('no configuration provided to verifier'); - } - - // This is just too messy to do on the rust side. neon-serde would have helped, but appears unmaintained - // and is currently incompatible - if (this.config.consumerVersionSelectors) { - config.consumerVersionSelectorsString = - this.config.consumerVersionSelectors.map((s) => JSON.stringify(s)); - } - - if (!this.config.provider) { - throw new ConfigurationError('provider name is required'); - } - - if ( - (isEmpty(this.config.pactUrls) || !this.config.pactUrls) && - !this.config.pactBrokerUrl - ) { - throw new ConfigurationError( - 'a list of pactUrls or a pactBrokerUrl must be provided' - ); - } - - return config; - } - - // Run the Verification CLI process - private runProviderVerification() { - return (server: http.Server) => - new Promise((resolve, reject) => { - const opts = { - providerStatesSetupUrl: `${this.address}:${server.address().port}${ - this.stateSetupPath - }`, - ...this.config, - providerBaseUrl: `${this.address}:${server.address().port}`, - }; - - try { - PactNative.verify_provider(opts, (err, val) => { - if (err || !val) { - logger.trace(`verification failed (err=${err}, val=${val})`); - reject(err); - } else { - logger.trace(`verification succeeded (val=${val})`); - resolve(val); - } - }); - logger.trace('submitted test to verify_provider'); - } catch (e) { - reject(e); - } - }); - } - - private isLocalVerification() { - const u = new url.URL(this.config.providerBaseUrl); - return ( - localAddresses.includes(u.host) || localAddresses.includes(u.hostname) + logger.warn( + 'You no longer need to import the verifier from @pact-foundation/pact/v3' + ); + logger.warn( + " - you may now update your imports to import { Verifier } from '@pact-foundation/pact'" ); + return this.internalVerifier.verifyProvider(); } }