Comparing version 2.0.3 to 2.0.4
{ | ||
"name": "pactum", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "REST API endpoint testing tool with a mock server & compatible with pact.io for contract testing", | ||
"main": "./src/index.js", | ||
"types": "./src/index.d.ts", | ||
"files": [ | ||
@@ -7,0 +8,0 @@ "/src" |
const { PactumHandlerError } = require('../helpers/errors'); | ||
/** | ||
* @callback RequestResponseHandler | ||
* @param {object} req - request object | ||
* @param {object} res - response object | ||
*/ | ||
/** | ||
* @typedef {object} Context | ||
* @property {any} [data] - custom data object passed to handler | ||
* @property {object} [spec] - spec will be available when used by `pactum.setState` | ||
*/ | ||
/** | ||
* @callback StateHandler | ||
* @param {Context} ctx - context object | ||
*/ | ||
const expectHandlers = {}; | ||
@@ -27,11 +10,2 @@ const retryHandlers = {}; | ||
/** | ||
* adds a custom expect handler | ||
* @param {string} name - name of the custom expect handler | ||
* @param {RequestResponseHandler} func - handler function | ||
* @example | ||
* pactum.handler.addExpectHandler('isUser', (req, res) => { | ||
* assert.strictEqual(res.json.type, 'user'); | ||
* }); | ||
*/ | ||
addExpectHandler(name, func) { | ||
@@ -47,9 +21,2 @@ isValidHandler(name, func, 'expect'); | ||
/** | ||
* adds a custom retry handler | ||
* @param {string} name - retry handler name | ||
* @param {RequestResponseHandler} func - retry handler function | ||
* @example | ||
* pactum.handler.addRetryHandler('RetryTill200', (req, res) => res.statusCode !== 200); | ||
*/ | ||
addRetryHandler(name, func) { | ||
@@ -65,9 +32,2 @@ isValidHandler(name, func, 'retry'); | ||
/** | ||
* adds a custom return handler | ||
* @param {string} name - return handler name | ||
* @param {RequestResponseHandler} func - return handler function | ||
* @example | ||
* pactum.handler.addReturnHandler('ReturnOrderId', (req, res) => { return res.json.id }); | ||
*/ | ||
addReturnHandler(name, func) { | ||
@@ -82,11 +42,2 @@ isValidHandler(name, func, 'return'); | ||
/** | ||
* adds a custom state handler | ||
* @param {string} name - state handler name | ||
* @param {StateHandler} func - state handler function | ||
* @example | ||
* pactum.handler.addStateHandler('there are no users', async (ctx) => { await db.clearUsers(); }); | ||
* pactum.handler.addStateHandler('there is an order', async (ctx) => { await db.addOrder(ctx.data); }); | ||
* pactum.handler.addStateHandler('there is a user', (ctx) => { ctx.spec.addInteraction({}); }); | ||
*/ | ||
addStateHandler(name, func) { | ||
@@ -93,0 +44,0 @@ isValidHandler(name, func, 'state'); |
@@ -9,112 +9,2 @@ const Interaction = require('../models/interaction'); | ||
/** | ||
* request method | ||
* @typedef {('GET'|'POST'|'PUT'|'DELETE'|'PATCH'|'HEAD')} RequestMethod | ||
*/ | ||
/** | ||
* basic mock interaction | ||
* @typedef {object} BasicInteraction | ||
* @property {string} [get] - get request path | ||
* @property {string} [post] - post request path | ||
* @property {string} [put] - put request path | ||
* @property {string} [patch] - patch request path | ||
* @property {string} [delete] - delete request path | ||
* @property {number} [status=200] - status code to return | ||
* @property {any} [return=''] - body to return | ||
*/ | ||
/** | ||
* interaction details | ||
* @typedef {object} Interaction | ||
* @property {string} [id] - unique id of the interaction | ||
* @property {string} [consumer] - name of the consumer | ||
* @property {string} [provider] - name of the provider | ||
* @property {string} [state] - state of the provider | ||
* @property {string} [uponReceiving] - description of the request | ||
* @property {object} withRequest - interaction request details | ||
* @property {RequestMethod} withRequest.method - request method | ||
* @property {string} withRequest.path - request path | ||
* @property {object} [withRequest.headers] - request headers | ||
* @property {object} [withRequest.query] - request query | ||
* @property {object} [withRequest.graphQL] - graphQL request | ||
* @property {string} withRequest.graphQL.query - graphQL query | ||
* @property {object} [withRequest.graphQL.variables] - graphQL variables | ||
* @property {object} [withRequest.body] - request body | ||
* @property {boolean} [withRequest.ignoreQuery] - ignores request query while matching | ||
* @property {boolean} [withRequest.ignoreBody] - ignores request body while matching | ||
* @property {object} willRespondWith - interaction response details | ||
* @property {string} willRespondWith.status - response status code | ||
* @property {string} [willRespondWith.headers] - response headers | ||
* @property {object} [willRespondWith.body] - response body | ||
*/ | ||
/** | ||
* pact interaction details | ||
* @typedef {object} PactInteraction | ||
* @property {string} [id] - unique id of the interaction | ||
* @property {string} provider - name of the provider | ||
* @property {string} state - state of the provider | ||
* @property {string} uponReceiving - description of the request | ||
* @property {PactInteractionRequest} withRequest - interaction request details | ||
* @property {PactInteractionResponse} willRespondWith - interaction response details | ||
*/ | ||
/** | ||
* mock interaction details | ||
* @typedef {object} MockInteraction | ||
* @property {string} [id] - unique id of the interaction | ||
* @property {string} [provider] - name of the provider | ||
* @property {MockInteractionRequest} withRequest - interaction request details | ||
* @property {MockInteractionResponse} willRespondWith - interaction response details | ||
*/ | ||
/** | ||
* pact interaction request details | ||
* @typedef {object} PactInteractionRequest | ||
* @property {RequestMethod} method - request method | ||
* @property {string} path - request method | ||
* @property {object} [headers] - request headers | ||
* @property {object} [query] - request query | ||
* @property {GraphQLRequest} [graphQL] - graphQL request | ||
* @property {object} [body] - request body | ||
*/ | ||
/** | ||
* @typedef {Object} MockInteractionRequestType | ||
* @property {boolean} ignoreQuery - ignores request query while matching | ||
* @property {boolean} ignoreBody - ignores request body while matching | ||
* | ||
* mock interaction request details | ||
* @typedef {PactInteractionRequest & MockInteractionRequestType} MockInteractionRequest | ||
*/ | ||
/** | ||
* graphQL request details | ||
* @typedef {object} GraphQLRequest | ||
* @property {string} query - graphQL query | ||
* @property {string} [variables] - graphQL variables | ||
*/ | ||
/** | ||
* pact interaction response details | ||
* @typedef {object} PactInteractionResponse | ||
* @property {number} status - response status code | ||
* @property {object} [headers] - response headers | ||
* @property {object} [body] - response body | ||
*/ | ||
/** | ||
* @typedef {Object} MockInteractionResponseType | ||
* @property {number} [fixedDelay] - response fixed delay in ms | ||
* @property {object} [randomDelay] - response random delay | ||
* @property {number} randomDelay.min - min delay in ms | ||
* @property {number} randomDelay.max - max delay in ms | ||
* @property {Object.<number, MockInteractionResponse>} onCall - behavior on consecutive calls | ||
* | ||
* mock interaction response details | ||
* @typedef {PactInteractionResponse & MockInteractionResponseType} MockInteractionResponse | ||
*/ | ||
const mock = { | ||
@@ -124,6 +14,2 @@ | ||
/** | ||
* starts server on specified port or defaults to 9393 | ||
* @param {number} port - port number of mock server | ||
*/ | ||
start(port) { | ||
@@ -136,5 +22,2 @@ if (port) { | ||
/** | ||
* stops server on specified port or defaults to 9393 | ||
*/ | ||
stop() { | ||
@@ -144,7 +27,2 @@ return this._server.stop(); | ||
/** | ||
* @deprecated | ||
* set default port number to run mock server | ||
* @param {number} port - port number of mock server | ||
*/ | ||
setDefaultPort(port) { | ||
@@ -158,6 +36,2 @@ log.debug('Setting default port number for mock server', port); | ||
/** | ||
* adds a basic mock interaction | ||
* @param {BasicInteraction} basicInteraction | ||
*/ | ||
addInteraction(basicInteraction) { | ||
@@ -174,6 +48,2 @@ const rawInteraction = { | ||
/** | ||
* adds basic mock interactions | ||
* @param {BasicInteraction[]} basicInteractions | ||
*/ | ||
addInteractions(basicInteractions) { | ||
@@ -195,6 +65,2 @@ const rawInteractions = []; | ||
/** | ||
* adds a mock interaction | ||
* @param {MockInteraction} interaction | ||
*/ | ||
addMockInteraction(interaction) { | ||
@@ -207,6 +73,2 @@ const interactionObj = new Interaction(interaction, true); | ||
/** | ||
* adds mock interactions | ||
* @param {MockInteraction[]} interactions | ||
*/ | ||
addMockInteractions(interactions) { | ||
@@ -227,6 +89,2 @@ if (!Array.isArray(interactions)) { | ||
/** | ||
* adds a pact interaction | ||
* @param {PactInteraction} interaction | ||
*/ | ||
addPactInteraction(interaction) { | ||
@@ -239,6 +97,2 @@ const interactionObj = new Interaction(interaction); | ||
/** | ||
* adds pact interactions | ||
* @param {PactInteraction[]} interactions | ||
*/ | ||
addPactInteractions(interactions) { | ||
@@ -258,6 +112,2 @@ if (!Array.isArray(interactions)) { | ||
/** | ||
* removes specified default interaction from server | ||
* @param {string} interactionId - id of the interaction | ||
*/ | ||
removeInteraction(interactionId) { | ||
@@ -270,5 +120,2 @@ if (typeof interactionId !== 'string' || !interactionId) { | ||
/** | ||
* clear all interactions | ||
*/ | ||
clearInteractions() { | ||
@@ -278,6 +125,2 @@ this._server.clearAllInteractions(); | ||
/** | ||
* returns true if interaction is exercised | ||
* @param {string} id - id of the interaction | ||
*/ | ||
isInteractionExercised(id) { | ||
@@ -287,6 +130,2 @@ return this._server.getInteractionDetails(id).exercised; | ||
/** | ||
* returns interaction call count | ||
* @param {string} id - id of the interaction | ||
*/ | ||
getInteractionCallCount(id) { | ||
@@ -293,0 +132,0 @@ return this._server.getInteractionDetails(id).callCount; |
@@ -5,21 +5,4 @@ const config = require('../config'); | ||
/** | ||
* @typedef {object} PublishOptions | ||
* @property {string[]} pactFilesOrDirs - array of pact files or directories (PACT_DIR) | ||
* @property {string} pactBroker - pact broker url (PACT_BROKER_URL) | ||
* @property {string} consumerVersion - version of the consumer | ||
* @property {string} pactBrokerUsername - pact broker user name (PACT_BROKER_USERNAME) | ||
* @property {string} pactBrokerPassword - pact broker password (PACT_BROKER_PASSWORD) | ||
* @property {string[]} tags - tags | ||
*/ | ||
const pact = { | ||
/** | ||
* @alias PACT_DIR | ||
* | ||
* sets directory for saving pact files | ||
* @param {string} dir - directory for saving pact files | ||
* @default './pacts/' | ||
*/ | ||
setPactFilesDirectory(dir) { | ||
@@ -32,8 +15,2 @@ if (typeof dir !== 'string') { | ||
/** | ||
* @alias PACT_CONSUMER_NAME | ||
* | ||
* sets the name of the consumer | ||
* @param {string} name - name of the consumer | ||
*/ | ||
setConsumerName(name) { | ||
@@ -46,5 +23,2 @@ if (typeof name !== 'string' || !name) { | ||
/** | ||
* saves all the contracts(pact files) in the specified directory | ||
*/ | ||
save() { | ||
@@ -54,6 +28,2 @@ store.save(); | ||
/** | ||
* publish pact files to pact broker | ||
* @param {PublishOptions} options - publish options | ||
*/ | ||
publish(options) { | ||
@@ -60,0 +30,0 @@ return store.publish(options); |
@@ -9,18 +9,2 @@ const phin = require('phin'); | ||
/** | ||
* provider options | ||
* @typedef {object} ProviderOptions | ||
* @property {string} providerBaseUrl - running API provider host endpoint. | ||
* @property {string} provider - name of the provider. | ||
* @property {string} [providerVersion] - provider version, required to publish verification results to a broker | ||
* @property {any} [stateHandlers] - provider state handlers. A map of 'string -> () => Promise', where each string is the state to setup, and the function is used to configure the state in the Provider. | ||
* @property {any} [customProviderHeaders] - Header(s) to add to any requests to the provider service. eg { 'Authorization': 'Basic cGFjdDpwYWN0'}. | ||
* @property {string[]} [pactFilesOrDirs] - array of local pact files or directories | ||
* @property {string} [pactBrokerUrl] - URL of the Pact Broker to retrieve pacts from. Required if not using pactFilesOrDirs. | ||
* @property {string} [pactBrokerUsername] - username for Pact Broker basic authentication. | ||
* @property {string} [pactBrokerPassword] - password for Pact Broker basic authentication. | ||
* @property {string} [pactBrokerToken] - bearer token for Pact Broker authentication. | ||
* @property {boolean} [publishVerificationResult] - publish verification result to Broker | ||
*/ | ||
class Provider { | ||
@@ -322,6 +306,2 @@ | ||
/** | ||
* validate provider | ||
* @param {ProviderOptions} options - provider options | ||
*/ | ||
validate(options) { | ||
@@ -328,0 +308,0 @@ const providerObj = new Provider(options); |
@@ -11,7 +11,2 @@ const form = require('form-data'); | ||
/** | ||
* adds a default header to all the requests | ||
* @param {string} key - header key | ||
* @param {string} value - header value | ||
*/ | ||
setDefaultHeader(key, value) { | ||
@@ -24,6 +19,2 @@ if (!key) { | ||
/** | ||
* adds default headers to all the requests | ||
* @param {object} headers - header key-value pairs | ||
*/ | ||
setDefaultHeaders(headers) { | ||
@@ -36,7 +27,2 @@ if (!helper.isValidObject(headers)) { | ||
/** | ||
* sets a default timeout to all the requests | ||
* Default is 3000ms | ||
* @param {number} timeout - timeout in milliseconds | ||
*/ | ||
setDefaultTimeout(timeout) { | ||
@@ -49,6 +35,2 @@ if (typeof timeout !== 'number') { | ||
/** | ||
* sets base url | ||
* @param {string} url - base url | ||
*/ | ||
setBaseUrl(url) { | ||
@@ -61,6 +43,2 @@ if (typeof url !== 'string') { | ||
/** | ||
* removes the default header | ||
* @param {string} key - header key | ||
*/ | ||
removeDefaultHeader(key) { | ||
@@ -73,5 +51,2 @@ if (!key) { | ||
/** | ||
* removes all default headers | ||
*/ | ||
removeDefaultHeaders() { | ||
@@ -78,0 +53,0 @@ config.request.headers = {}; |
@@ -5,6 +5,2 @@ const logger = require('../helpers/logger'); | ||
/** | ||
* sets log level | ||
* @param {('TRACE'|'DEBUG'|'INFO'|'WARN'|'ERROR')} level - log level | ||
*/ | ||
setLogLevel(level) { | ||
@@ -11,0 +7,0 @@ logger.setLevel(level); |
@@ -5,10 +5,2 @@ const State = require('../models/State'); | ||
/** | ||
* runs the specified state handler | ||
* @param {string} name - name of the state handler | ||
* @param {any} [data] - data to be passed to the context | ||
* @example | ||
* await state.set('there is a user in system'); | ||
* await state.set('there is a user in system with', { id: 1, name: 'stark' }); | ||
*/ | ||
set(name, data) { | ||
@@ -15,0 +7,0 @@ const s = new State(); |
229
src/index.js
@@ -13,88 +13,2 @@ const Spec = require('./models/Spec'); | ||
/** | ||
* request method | ||
* @typedef {('GET'|'POST'|'PUT'|'DELETE'|'PATCH'|'HEAD')} RequestMethod | ||
*/ | ||
/** | ||
* basic mock interaction | ||
* @typedef {object} BasicInteraction | ||
* @property {string} [get] - get request path | ||
* @property {string} [post] - post request path | ||
* @property {string} [put] - put request path | ||
* @property {string} [patch] - patch request path | ||
* @property {string} [delete] - delete request path | ||
* @property {number} [status=200] - status code to return | ||
* @property {any} [return=''] - body to return | ||
*/ | ||
/** | ||
* pact interaction details | ||
* @typedef {object} PactInteraction | ||
* @property {string} [id] - unique id of the interaction | ||
* @property {string} provider - name of the provider | ||
* @property {string} state - state of the provider | ||
* @property {string} uponReceiving - description of the request | ||
* @property {PactInteractionRequest} withRequest - interaction request details | ||
* @property {PactInteractionResponse} willRespondWith - interaction response details | ||
*/ | ||
/** | ||
* mock interaction details | ||
* @typedef {object} MockInteraction | ||
* @property {string} [id] - unique id of the interaction | ||
* @property {string} [provider] - name of the provider | ||
* @property {MockInteractionRequest} withRequest - interaction request details | ||
* @property {MockInteractionResponse} willRespondWith - interaction response details | ||
*/ | ||
/** | ||
* pact interaction request details | ||
* @typedef {object} PactInteractionRequest | ||
* @property {RequestMethod} method - request method | ||
* @property {string} path - request path | ||
* @property {object} [headers] - request headers | ||
* @property {object} [query] - request query | ||
* @property {GraphQLRequest} [graphQL] - graphQL request | ||
* @property {object} [body] - request body | ||
*/ | ||
/** | ||
* @typedef {Object} MockInteractionRequestType | ||
* @property {boolean} ignoreQuery - ignores request query while matching | ||
* @property {boolean} ignoreBody - ignores request body while matching | ||
* | ||
* mock interaction request details | ||
* @typedef {PactInteractionRequest & MockInteractionRequestType} MockInteractionRequest | ||
*/ | ||
/** | ||
* graphQL request details | ||
* @typedef {object} GraphQLRequest | ||
* @property {string} query - graphQL query | ||
* @property {string} [variables] - graphQL variables | ||
*/ | ||
/** | ||
* pact interaction response details | ||
* @typedef {object} PactInteractionResponse | ||
* @property {number} status - response status code | ||
* @property {object} [headers] - response headers | ||
* @property {object} [body] - response body | ||
*/ | ||
/** | ||
* @typedef {Object} MockInteractionResponseType | ||
* @property {number} [fixedDelay] - response fixed delay in ms | ||
* @property {object} [randomDelay] - response random delay | ||
* @property {number} randomDelay.min - min delay in ms | ||
* @property {number} randomDelay.max - max delay in ms | ||
* @property {Object.<number, MockInteractionResponse>} onCall - behavior on consecutive calls | ||
* | ||
* mock interaction response details | ||
* @typedef {PactInteractionResponse & MockInteractionResponseType} MockInteractionResponse | ||
*/ | ||
const pactum = { | ||
@@ -112,12 +26,2 @@ | ||
/** | ||
* runs the specified state handler | ||
* @param {string} name - name of the state handler | ||
* @param {any} [data] - data to be passed to the context | ||
* @example | ||
* await pactum | ||
* .setState('there are users in the system') | ||
* .get('/api/users') | ||
* .expectStatus(200); | ||
*/ | ||
setState(name, data) { | ||
@@ -127,49 +31,6 @@ return new Spec().setState(name, data); | ||
/** | ||
* adds a mock interaction to the server | ||
* @param {BasicInteraction} interaction | ||
* @see addMockInteraction for more options | ||
* @example | ||
* await pactum | ||
* .addInteraction({ | ||
* get: '/api/address/4' | ||
* return: { | ||
* city: 'WinterFell' | ||
* } | ||
* }) | ||
* .get('/api/users/4') | ||
* .expectStatus(200); | ||
*/ | ||
addInteraction(interaction) { | ||
return new Spec().addInteraction(interaction); | ||
}, | ||
/** | ||
* adds a mock interaction to the server | ||
* @param {MockInteraction} interaction - interaction details | ||
* @example | ||
* await pactum | ||
* .addMockInteraction({ | ||
* withRequest: { | ||
* method: 'GET', | ||
* path: '/api/projects/1' | ||
* }, | ||
* willRespondWith: { | ||
* status: 200, | ||
* headers: { | ||
* 'Content-Type': 'application/json' | ||
* }, | ||
* body: { | ||
* id: 1, | ||
* name: 'fake' | ||
* } | ||
* } | ||
* }) | ||
* .get('https://jsonplaceholder.typicode.com/posts') | ||
* .expectStatus(200) | ||
* .expectJsonLike({ | ||
* userId: 1, | ||
* id: 1 | ||
* }); | ||
*/ | ||
addMockInteraction(interaction) { | ||
@@ -179,33 +40,2 @@ return new Spec().addMockInteraction(interaction); | ||
/** | ||
* adds a pact interaction to the server | ||
* @param {PactInteraction} interaction - interaction details | ||
* @example | ||
* await pactum | ||
* .addPactInteraction({ | ||
* provider: 'project-provider', | ||
* state: 'when there is a project with id 1', | ||
* uponReceiving: 'a request for project 1', | ||
* withRequest: { | ||
* method: 'GET', | ||
* path: '/api/projects/1' | ||
* }, | ||
* willRespondWith: { | ||
* status: 200, | ||
* headers: { | ||
* 'Content-Type': 'application/json' | ||
* }, | ||
* body: { | ||
* id: 1, | ||
* name: 'fake' | ||
* } | ||
* } | ||
* }) | ||
* .get('https://jsonplaceholder.typicode.com/posts') | ||
* .expectStatus(200) | ||
* .expectJsonLike({ | ||
* userId: 1, | ||
* id: 1 | ||
* }); | ||
*/ | ||
addPactInteraction(interaction) { | ||
@@ -215,15 +45,2 @@ return new Spec().addPactInteraction(interaction); | ||
/** | ||
* The GET method requests a representation of the specified resource. Requests using GET should only retrieve data. | ||
* @param {string} url - HTTP url | ||
* @example | ||
* await pactum | ||
* .get('https://jsonplaceholder.typicode.com/posts') | ||
* .withQueryParam('postId', 1) | ||
* .expectStatus(200) | ||
* .expectJsonLike({ | ||
* userId: 1, | ||
* id: 1 | ||
* }); | ||
*/ | ||
get(url) { | ||
@@ -233,6 +50,2 @@ return new Spec().get(url); | ||
/** | ||
* The HEAD method asks for a response identical to that of a GET request, but without the response body. | ||
* @param {string} url - HTTP url | ||
*/ | ||
head(url) { | ||
@@ -242,13 +55,2 @@ return new Spec().head(url); | ||
/** | ||
* The PATCH method is used to apply partial modifications to a resource. | ||
* @param {string} url - HTTP url | ||
* @example | ||
* await pactum | ||
* .patch('https://jsonplaceholder.typicode.com/posts/1') | ||
* .withJson({ | ||
* title: 'foo' | ||
* }) | ||
* .expectStatus(200); | ||
*/ | ||
patch(url) { | ||
@@ -258,15 +60,2 @@ return new Spec().patch(url); | ||
/** | ||
* The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server. | ||
* @param {string} url - HTTP url | ||
* @example | ||
* await pactum | ||
* .post('https://jsonplaceholder.typicode.com/posts') | ||
* .withJson({ | ||
* title: 'foo', | ||
* body: 'bar', | ||
* userId: 1 | ||
* }) | ||
* .expectStatus(201); | ||
*/ | ||
post(url) { | ||
@@ -276,16 +65,2 @@ return new Spec().post(url); | ||
/** | ||
* The PUT method replaces all current representations of the target resource with the request payload. | ||
* @param {string} url - HTTP url | ||
* @example | ||
* await pactum | ||
* .put('https://jsonplaceholder.typicode.com/posts/1') | ||
* .withJson({ | ||
* id: 1, | ||
* title: 'foo', | ||
* body: 'bar', | ||
* userId: 1 | ||
* }) | ||
* .expectStatus(200); | ||
*/ | ||
put(url) { | ||
@@ -309,2 +84,2 @@ return new Spec().put(url); | ||
module.exports = pactum; | ||
module.exports = pactum; |
@@ -11,87 +11,2 @@ const FormData = require('form-data'); | ||
/** | ||
* request method | ||
* @typedef {('GET'|'POST'|'PUT'|'DELETE'|'PATCH'|'HEAD')} RequestMethod | ||
*/ | ||
/** | ||
* basic mock interaction | ||
* @typedef {object} BasicInteraction | ||
* @property {string} [get] - get request path | ||
* @property {string} [post] - post request path | ||
* @property {string} [put] - put request path | ||
* @property {string} [patch] - patch request path | ||
* @property {string} [delete] - delete request path | ||
* @property {number} [status=200] - status code to return | ||
* @property {any} [return=''] - body to return | ||
*/ | ||
/** | ||
* pact interaction details | ||
* @typedef {object} PactInteraction | ||
* @property {string} [id] - unique id of the interaction | ||
* @property {string} provider - name of the provider | ||
* @property {string} state - state of the provider | ||
* @property {string} uponReceiving - description of the request | ||
* @property {PactInteractionRequest} withRequest - interaction request details | ||
* @property {PactInteractionResponse} willRespondWith - interaction response details | ||
*/ | ||
/** | ||
* mock interaction details | ||
* @typedef {object} MockInteraction | ||
* @property {string} [id] - unique id of the interaction | ||
* @property {string} [provider] - name of the provider | ||
* @property {MockInteractionRequest} withRequest - interaction request details | ||
* @property {MockInteractionResponse} willRespondWith - interaction response details | ||
*/ | ||
/** | ||
* pact interaction request details | ||
* @typedef {object} PactInteractionRequest | ||
* @property {RequestMethod} method - request method | ||
* @property {string} path - request method | ||
* @property {object} [headers] - request headers | ||
* @property {object} [query] - request query | ||
* @property {GraphQLRequest} [graphQL] - graphQL request | ||
* @property {object} [body] - request body | ||
*/ | ||
/** | ||
* @typedef {Object} MockInteractionRequestType | ||
* @property {boolean} ignoreQuery - ignores request query while matching | ||
* @property {boolean} ignoreBody - ignores request body while matching | ||
* | ||
* mock interaction request details | ||
* @typedef {PactInteractionRequest & MockInteractionRequestType} MockInteractionRequest | ||
*/ | ||
/** | ||
* graphQL request details | ||
* @typedef {object} GraphQLRequest | ||
* @property {string} query - graphQL query | ||
* @property {string} [variables] - graphQL variables | ||
*/ | ||
/** | ||
* pact interaction response details | ||
* @typedef {object} PactInteractionResponse | ||
* @property {number} status - response status code | ||
* @property {object} [headers] - response headers | ||
* @property {object} [body] - response body | ||
*/ | ||
/** | ||
* @typedef {Object} MockInteractionResponseType | ||
* @property {number} [fixedDelay] - response fixed delay in ms | ||
* @property {object} [randomDelay] - response random delay | ||
* @property {number} randomDelay.min - min delay in ms | ||
* @property {number} randomDelay.max - max delay in ms | ||
* @property {Object.<number, MockInteractionResponse>} onCall - behavior on consecutive calls | ||
* | ||
* mock interaction response details | ||
* @typedef {PactInteractionResponse & MockInteractionResponseType} MockInteractionResponse | ||
*/ | ||
class Spec { | ||
@@ -112,12 +27,2 @@ | ||
/** | ||
* runs the specified state handler | ||
* @param {string} name - name of the state handler | ||
* @param {any} [data] - data to be passed to the context | ||
* @example | ||
* await pactum | ||
* .setState('there are users in the system') | ||
* .get('/api/users') | ||
* .expectStatus(200); | ||
*/ | ||
setState(name, data) { | ||
@@ -128,17 +33,2 @@ this._state.add(name, data); | ||
/** | ||
* adds a basic mock interaction to the server | ||
* @param {BasicInteraction} basicInteraction | ||
* @see addMockInteraction for more options | ||
* @example | ||
* await pactum | ||
* .addInteraction({ | ||
* get: '/api/address/4' | ||
* return: { | ||
* city: 'WinterFell' | ||
* } | ||
* }) | ||
* .get('/api/users/4') | ||
* .expectStatus(200); | ||
*/ | ||
addInteraction(basicInteraction) { | ||
@@ -155,30 +45,2 @@ const rawInteraction = { | ||
/** | ||
* adds a mock interaction to the server | ||
* @param {MockInteraction} rawInteraction - interaction details | ||
* @example | ||
* await pactum | ||
* .addMockInteraction({ | ||
* withRequest: { | ||
* method: 'GET', | ||
* path: '/api/projects/1' | ||
* }, | ||
* willRespondWith: { | ||
* status: 200, | ||
* headers: { | ||
* 'Content-Type': 'application/json' | ||
* }, | ||
* body: { | ||
* id: 1, | ||
* name: 'fake' | ||
* } | ||
* } | ||
* }) | ||
* .get('https://jsonplaceholder.typicode.com/posts') | ||
* .expectStatus(200) | ||
* .expectJsonLike({ | ||
* userId: 1, | ||
* id: 1 | ||
* }); | ||
*/ | ||
addMockInteraction(rawInteraction) { | ||
@@ -191,33 +53,2 @@ const interaction = new Interaction(rawInteraction, true); | ||
/** | ||
* adds a pact interaction to the server | ||
* @param {PactInteraction} rawInteraction - interaction details | ||
* @example | ||
* await pactum | ||
* .addPactInteraction({ | ||
* provider: 'project-provider', | ||
* state: 'when there is a project with id 1', | ||
* uponReceiving: 'a request for project 1', | ||
* withRequest: { | ||
* method: 'GET', | ||
* path: '/api/projects/1' | ||
* }, | ||
* willRespondWith: { | ||
* status: 200, | ||
* headers: { | ||
* 'Content-Type': 'application/json' | ||
* }, | ||
* body: { | ||
* id: 1, | ||
* name: 'fake' | ||
* } | ||
* } | ||
* }) | ||
* .get('https://jsonplaceholder.typicode.com/posts') | ||
* .expectStatus(200) | ||
* .expectJsonLike({ | ||
* userId: 1, | ||
* id: 1 | ||
* }); | ||
*/ | ||
addPactInteraction(rawInteraction) { | ||
@@ -230,15 +61,2 @@ const interaction = new Interaction(rawInteraction, false); | ||
/** | ||
* The GET method requests a representation of the specified resource. Requests using GET should only retrieve data. | ||
* @param {string} url - HTTP url | ||
* @example | ||
* await pactum | ||
* .get('https://jsonplaceholder.typicode.com/posts') | ||
* .withQueryParam('postId', 1) | ||
* .expectStatus(200) | ||
* .expectJsonLike({ | ||
* userId: 1, | ||
* id: 1 | ||
* }); | ||
*/ | ||
get(url) { | ||
@@ -251,6 +69,2 @@ validateRequestUrl(this._request, url); | ||
/** | ||
* The HEAD method asks for a response identical to that of a GET request, but without the response body. | ||
* @param {string} url - HTTP url | ||
*/ | ||
head(url) { | ||
@@ -263,13 +77,2 @@ validateRequestUrl(this._request, url); | ||
/** | ||
* The PATCH method is used to apply partial modifications to a resource. | ||
* @param {string} url - HTTP url | ||
* @example | ||
* await pactum | ||
* .patch('https://jsonplaceholder.typicode.com/posts/1') | ||
* .withJson({ | ||
* title: 'foo' | ||
* }) | ||
* .expectStatus(200); | ||
*/ | ||
patch(url) { | ||
@@ -282,15 +85,2 @@ validateRequestUrl(this._request, url); | ||
/** | ||
* The POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server. | ||
* @param {string} url - HTTP url | ||
* @example | ||
* await pactum | ||
* .post('https://jsonplaceholder.typicode.com/posts') | ||
* .withJson({ | ||
* title: 'foo', | ||
* body: 'bar', | ||
* userId: 1 | ||
* }) | ||
* .expectStatus(201); | ||
*/ | ||
post(url) { | ||
@@ -303,16 +93,2 @@ validateRequestUrl(this._request, url); | ||
/** | ||
* The PUT method replaces all current representations of the target resource with the request payload. | ||
* @param {string} url - HTTP url | ||
* @example | ||
* await pactum | ||
* .put('https://jsonplaceholder.typicode.com/posts/1') | ||
* .withJson({ | ||
* id: 1, | ||
* title: 'foo', | ||
* body: 'bar', | ||
* userId: 1 | ||
* }) | ||
* .expectStatus(200); | ||
*/ | ||
put(url) { | ||
@@ -340,14 +116,2 @@ validateRequestUrl(this._request, url); | ||
/** | ||
* appends query param to the request url - /comments?postId=1 | ||
* @param {string} key - query parameter key | ||
* @param {string} value - query parameter value | ||
* @example | ||
* await pactum | ||
* .get('https://jsonplaceholder.typicode.com/comments') | ||
* .withQueryParam('postId', '1') | ||
* .withQueryParam('userId', '2') | ||
* .expectStatus(200); | ||
* @summary generated url will look like - /comments?postId=1&userId=2 | ||
*/ | ||
withQueryParam(key, value) { | ||
@@ -367,12 +131,2 @@ if (!helper.isValidString(key)) { | ||
/** | ||
* adds query params to the request url - /comments?postId=1 | ||
* @param {object} params - query params | ||
* @example | ||
* await pactum | ||
* .get('https://jsonplaceholder.typicode.com/comments') | ||
* .withQueryParams({ 'postId': '1' }) | ||
* .expectStatus(200); | ||
* @summary generated url will look like - /comments?postId=1&userId=2 | ||
*/ | ||
withQueryParams(params) { | ||
@@ -389,11 +143,2 @@ if (!helper.isValidObject(params) || Object.keys(params).length === 0) { | ||
/** | ||
* appends graphQL query to the request body | ||
* @param {string} query - graphQL query | ||
* @example | ||
* await pactum | ||
* .post('http://www.graph.com/graphql') | ||
* .withGraphQLQuery(`{ hello }`) | ||
* .expectStatus(200); | ||
*/ | ||
withGraphQLQuery(query) { | ||
@@ -410,18 +155,2 @@ if (typeof query !== 'string') { | ||
/** | ||
* appends graphQL variables to the request body | ||
* @param {object} variables - JSON object of graphQL variables | ||
* @example | ||
* await pactum | ||
* .post('http://www.graph.com/graphql') | ||
* .withGraphQLQuery(` | ||
* hero(episode: $episode) { | ||
* name | ||
* }` | ||
* ) | ||
* .withGraphQLVariables({ | ||
* "episode": "JEDI" | ||
* }) | ||
* .expectStatus(200); | ||
*/ | ||
withGraphQLVariables(variables) { | ||
@@ -438,15 +167,2 @@ if (!helper.isValidObject(variables)) { | ||
/** | ||
* attaches json object to the request body | ||
* @param {object} json - json object | ||
* @example | ||
* await pactum | ||
* .post('https://jsonplaceholder.typicode.com/posts') | ||
* .withJson({ | ||
* title: 'foo', | ||
* body: 'bar', | ||
* userId: 1 | ||
* }) | ||
* .expectStatus(201); | ||
*/ | ||
withJson(json) { | ||
@@ -460,13 +176,2 @@ if (typeof json !== 'object') { | ||
/** | ||
* appends header to the request | ||
* @param {string} key - header key | ||
* @param {string} value - header value | ||
* @example | ||
* await pactum | ||
* .post('') | ||
* .withHeader('Authorization', 'Basic xxx') | ||
* .withHeader('Accept', 'json') | ||
* .expectStatus(200) | ||
*/ | ||
withHeader(key, value) { | ||
@@ -480,18 +185,2 @@ if (!this._request.headers) { | ||
/** | ||
* attaches headers to the request | ||
* @param {object} headers - request headers with key-value pairs | ||
* @example | ||
* await pactum | ||
* .post('https://jsonplaceholder.typicode.com/posts') | ||
* .withHeaders({ | ||
* 'content-type': 'application/json' | ||
* }) | ||
* .withJson({ | ||
* title: 'foo', | ||
* body: 'bar', | ||
* userId: 1 | ||
* }) | ||
* .expectStatus(201); | ||
*/ | ||
withHeaders(headers) { | ||
@@ -508,13 +197,2 @@ if (!helper.isValidObject(headers)) { | ||
/** | ||
* attaches body to the request | ||
* @param {string|Buffer} body - request body | ||
* @example | ||
* await pactum | ||
* .post('https://jsonplaceholder.typicode.com/posts') | ||
* .withBody(JSON.stringify({ | ||
* title: 'foo', | ||
* })) | ||
* .expectStatus(201); | ||
*/ | ||
withBody(body) { | ||
@@ -528,13 +206,2 @@ if (typeof this._request.data !== 'undefined') { | ||
/** | ||
* attaches form data to the request with header - "application/x-www-form-urlencoded" | ||
* @param {object} form - form object | ||
* @example | ||
* await pactum | ||
* .post('https://jsonplaceholder.typicode.com/posts') | ||
* .withFormData({ | ||
* 'user': 'drake' | ||
* }) | ||
* .expectStatus(200); | ||
*/ | ||
withForm(form) { | ||
@@ -551,23 +218,2 @@ if (!helper.isValidObject(form)) { | ||
/** | ||
* attaches multi part form data to the request with header - "multipart/form-data" | ||
* @param {string|FormData} key - key to append | ||
* @param {string|Buffer|Array|ArrayBuffer} value - value to append | ||
* @param {FormData.AppendOptions} [options] - form data append options | ||
* @see https://www.npmjs.com/package/form-data | ||
* @example | ||
* await pactum | ||
* .post('https://jsonplaceholder.typicode.com/upload') | ||
* .withMultiPartFormData('file', fs.readFileSync(path), { contentType: 'application/xml', filename: 'jUnit.xml' }) | ||
* .withMultiPartFormData('user', 'drake') | ||
* .expectStatus(200); | ||
* | ||
* @example | ||
* const form = new pactum.request.FormData(); | ||
* form.append('my_file', fs.readFileSync(path), { contentType: 'application/xml', filename: 'jUnit.xml' }); | ||
* await pactum | ||
* .post('https://jsonplaceholder.typicode.com/upload') | ||
* .withMultiPartFormData(form) | ||
* .expectStatus(200); | ||
*/ | ||
withMultiPartFormData(key, value, options) { | ||
@@ -585,16 +231,2 @@ if (key instanceof FormData) { | ||
/** | ||
* retry request on specific conditions | ||
* @param {object} options - retry options | ||
* @param {number} [options.count=3] - maximum number of retries | ||
* @param {number} [options.delay=1000] - delay between each request in milliseconds | ||
* @param {function|string} options.strategy - retry strategy function (return true to retry) | ||
* @example | ||
* await pactum | ||
* .get('/some/url) | ||
* .retry({ | ||
* strategy: (req, res) => res.statusCode !== 200 | ||
* }) | ||
* .expectStatus(200); | ||
*/ | ||
retry(options) { | ||
@@ -639,17 +271,2 @@ if (!options) { | ||
/** | ||
* runs specified custom expect handler | ||
* @param {string|function} handler - name of the custom expect handler or function itself | ||
* @param {any} data - additional data | ||
* @example | ||
* pactum.handler.addExpectHandler('hasAddress', (req, res, data) => { | ||
* const json = res.json; | ||
* assert.strictEqual(json.type, data); | ||
* }); | ||
* await pactum | ||
* .get('https://jsonplaceholder.typicode.com/users/1') | ||
* .expect('isUser') | ||
* .expect('hasAddress', 'home') | ||
* .expect((req, res, data) => { -- assertion code -- }); | ||
*/ | ||
expect(handler, data) { | ||
@@ -660,10 +277,2 @@ this._expect.customExpectHandlers.push({ handler, data }); | ||
/** | ||
* expects a status code on the response | ||
* @param {number} statusCode - expected HTTP stats code | ||
* @example | ||
* await pactum | ||
* .delete('https://jsonplaceholder.typicode.com/posts/1') | ||
* .expectStatus(200); | ||
*/ | ||
expectStatus(statusCode) { | ||
@@ -674,12 +283,2 @@ this._expect.statusCode = statusCode; | ||
/** | ||
* expects a header in the response | ||
* @param {string} header - expected header key | ||
* @param {string} value - expected header value | ||
* @example | ||
* await pactum | ||
* .get('https://jsonplaceholder.typicode.com/posts/1') | ||
* .expectHeader('content-type', 'application/json; charset=utf-8') | ||
* .expectHeader('connection', /\w+/); | ||
*/ | ||
expectHeader(header, value) { | ||
@@ -693,11 +292,2 @@ this._expect.headers.push({ | ||
/** | ||
* expects a header in the response | ||
* @param {string} header - expected header value | ||
* @param {string} value - expected header value | ||
* @example | ||
* await pactum | ||
* .get('https://jsonplaceholder.typicode.com/comments') | ||
* .expectHeaderContains('content-type', 'application/json'); | ||
*/ | ||
expectHeaderContains(header, value) { | ||
@@ -721,13 +311,2 @@ this._expect.headerContains.push({ | ||
/** | ||
* expects a exact json object in the response | ||
* @param {object} json - expected json object | ||
* @example | ||
* await pactum | ||
* .get('https://jsonplaceholder.typicode.com/posts/1') | ||
* .expectJson({ | ||
* userId: 1, | ||
* user: 'frank' | ||
* }); | ||
*/ | ||
expectJson(json) { | ||
@@ -738,14 +317,2 @@ this._expect.json.push(json); | ||
/** | ||
* expects a partial json object in the response | ||
* @param {object} json - expected json object | ||
* @example | ||
* await pactum | ||
* .get('https://jsonplaceholder.typicode.com/comments') | ||
* .expectJsonLike([{ | ||
* postId: 1, | ||
* id: 1, | ||
* name: /\w+/g | ||
* }]); | ||
*/ | ||
expectJsonLike(json) { | ||
@@ -756,18 +323,2 @@ this._expect.jsonLike.push(json); | ||
/** | ||
* expects the response to match with json schema | ||
* @param {object} schema - expected JSON schema | ||
* @see https://json-schema.org/learn/ | ||
* @example | ||
* await pactum | ||
* .get('https://jsonplaceholder.typicode.com/posts/1') | ||
* .expectJsonSchema({ | ||
* "properties": { | ||
* "userId": { | ||
* "type": "number" | ||
* } | ||
* }, | ||
* "required": ["userId", "id"] | ||
* }); | ||
*/ | ||
expectJsonSchema(schema) { | ||
@@ -778,13 +329,2 @@ this._expect.jsonSchema.push(schema); | ||
/** | ||
* expects the json at path equals to the value | ||
* @param {string} path - json path | ||
* @param {any} value - value to be asserted | ||
* @see https://www.npmjs.com/package/json-query | ||
* @example | ||
* await pactum | ||
* .get('some-url') | ||
* .expectJsonQuery('[0].name', 'Matt') | ||
* .expectJsonQuery('[*].name', ['Matt', 'Pet', 'Don']); | ||
*/ | ||
expectJsonQuery(path, value) { | ||
@@ -795,12 +335,2 @@ this._expect.jsonQuery.push({ path, value }); | ||
/** | ||
* expects the json at path to be like the value | ||
* @param {string} path - json path | ||
* @param {any} value - value to be asserted | ||
* @see https://www.npmjs.com/package/json-query | ||
* @example | ||
* await pactum | ||
* .get('some-url') | ||
* .expectJsonQueryLike('[*].name', ['Matt', 'Pet', 'Don']); | ||
*/ | ||
expectJsonQueryLike(path, value) { | ||
@@ -811,6 +341,2 @@ this._expect.jsonQueryLike.push({ path, value }); | ||
/** | ||
* expects request completes within a specified duration (ms) | ||
* @param {number} value - response time in milliseconds | ||
*/ | ||
expectResponseTime(value) { | ||
@@ -821,19 +347,2 @@ this._expect.responseTime = value; | ||
/** | ||
* returns custom response | ||
* @param {string|function} handler - return handler (json-query/handler function) | ||
* @example | ||
* const id = await pactum | ||
* .get('some-url') | ||
* .expectStatus(200) | ||
* .returns('user.id') // json query | ||
* // 'id' will be equal to '123' if response is { user: { id: 123 }} | ||
* | ||
* const resp = await pactum | ||
* .get('some-url') | ||
* .expectStatus(200) | ||
* .returns([0].name) | ||
* .returns((req, res) => { return res.json[0].id }) // custom function | ||
* // 'resp' will be an array containing ['name', 'id'] | ||
*/ | ||
returns(handler) { | ||
@@ -844,5 +353,2 @@ this._returns.push(handler); | ||
/** | ||
* executes the test case | ||
*/ | ||
async toss() { | ||
@@ -849,0 +355,0 @@ const tosser = new Tosser(this); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40
4371
158416