Skip to content

Commit

Permalink
Merge pull request #25 from jordanwalsh23/fix/error_with_no_path
Browse files Browse the repository at this point in the history
Fix/error with no path
  • Loading branch information
jordanwalsh23 authored Jul 12, 2024
2 parents 61012e5 + 09a49a6 commit d342093
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lib/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`))
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
52 changes: 52 additions & 0 deletions test/collections/test-collection-no-path.json
Original file line number Diff line number Diff line change
@@ -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}"
}
]
}
]
}
36 changes: 36 additions & 0 deletions test/noPathTest.js
Original file line number Diff line number Diff line change
@@ -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()
})
});

0 comments on commit d342093

Please sign in to comment.