From b8f1cabb52c412de7fe95ab538b15302dea145bf Mon Sep 17 00:00:00 2001 From: Jordan Walsh Date: Fri, 12 Jul 2024 13:28:56 +1000 Subject: [PATCH 1/2] fixed an issue where if the mock server had no path it was throwing an error --- lib/responses.js | 8 +-- test/collections/test-collection-no-path.json | 52 +++++++++++++++++++ test/noPathTest.js | 36 +++++++++++++ 3 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 test/collections/test-collection-no-path.json create mode 100644 test/noPathTest.js diff --git a/lib/responses.js b/lib/responses.js index 3dee611..b2541fa 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -72,10 +72,12 @@ function getMatchedResponse(req, responses, debug) { score: 100 }; + let thisResponsePath = ""; + //get the path for this response. - let thisResponsePath = `${response.originalRequest.url.path.join( - '/' - )}` + if(response && response.originalRequest && response.originalRequest.url && response.originalRequest.url.path) { + thisResponsePath = `${response.originalRequest.url.path.join('/')}` + } //3.1.1 Perfect Score debug && console.log(`Perfect Match: ${req.path} eq /${thisResponsePath}`, _.isEqual(req.path, `/${thisResponsePath}`)) diff --git a/test/collections/test-collection-no-path.json b/test/collections/test-collection-no-path.json new file mode 100644 index 0000000..d1ab9c1 --- /dev/null +++ b/test/collections/test-collection-no-path.json @@ -0,0 +1,52 @@ +{ + "info": { + "_postman_id": "39a1d6e2-8a17-436b-886c-f87c3b2f1248", + "name": "Test Request with No Path", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "18475687" + }, + "item": [ + { + "name": "No Path", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{baseUrl}}", + "host": [ + "{{baseUrl}}" + ] + } + }, + "response": [ + { + "name": "No Path", + "originalRequest": { + "method": "GET", + "header": [], + "url": { + "raw": "{{baseUrl}}", + "host": [ + "{{baseUrl}}" + ] + } + }, + "status": "OK", + "code": 200, + "_postman_previewlanguage": "json", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "name": "Content-Type", + "description": "", + "type": "text" + } + ], + "cookie": [], + "body": "{\n \"status\": \"OK\"\n}" + } + ] + } + ] +} \ No newline at end of file diff --git a/test/noPathTest.js b/test/noPathTest.js new file mode 100644 index 0000000..1e0850c --- /dev/null +++ b/test/noPathTest.js @@ -0,0 +1,36 @@ +const fs = require('fs') +const PostmanLocalMockServer = require('../index.js') +const axios = require('axios').default +const assert = require('assert') + +const PORT = 3561; + +var options = { + port: PORT, + debug: true +} + +let server + +describe('Postman Local Mock Server Tests', () => { + beforeEach(() => { + options.collection = JSON.parse( + fs.readFileSync('./test/collections/test-collection-no-path.json', 'utf8') + ) + + server = new PostmanLocalMockServer(options) + server.start() + }) + + describe('Get with no path', () => { + it('Default GET response test with no path', async () => { + return await axios.get(`http://localhost:${PORT}`).then(res => { + assert(res.data.status === 'OK') + }) + }) + }) + + afterEach(() => { + server.stop() + }) +}); \ No newline at end of file From 09a49a6b5b51cf5634c05ff8694b5612c9b2b270 Mon Sep 17 00:00:00 2001 From: Jordan Walsh Date: Fri, 12 Jul 2024 13:29:01 +1000 Subject: [PATCH 2/2] 0.2.2 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0efafbd..38fa226 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@jordanwalsh23/postman-local-mock-server", - "version": "0.2.1", + "version": "0.2.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@jordanwalsh23/postman-local-mock-server", - "version": "0.2.1", + "version": "0.2.2", "license": "MIT", "dependencies": { "@faker-js/faker": "^7.4.0", diff --git a/package.json b/package.json index 4d7ff85..e3bd711 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jordanwalsh23/postman-local-mock-server", - "version": "0.2.1", + "version": "0.2.2", "description": "Provides the ability to run Postman collections as a local mock server.", "main": "index.js", "scripts": {