Skip to content

Commit

Permalink
fix(template): allow for colon to terminate a template
Browse files Browse the repository at this point in the history
  • Loading branch information
billxinli authored and Ahmad Nassri committed Dec 3, 2021
1 parent 68715e1 commit 28aa6d8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/parse-path-template.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// helper
module.exports = function (src, variables = {}, defaults = {}) {
// find all parameters
const results = src.matchAll(/\{(?<param>[^/]+)\}/g)
const results = src.matchAll(/\{(?<param>[^/:]+)\}/g)

for (const { groups: { param } } of results) {
const regex = new RegExp(`{${param}}`, 'g')
Expand Down
11 changes: 6 additions & 5 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('methods are callable', assert => {

fetch.callsFake((url, options) => {
assert.match(url, new URL('http://pets.com/pets/%7BpetId%7D'))
assert.deepEqual(options, {
assert.same(options, {
method: 'get',
headers: {}
})
Expand All @@ -62,7 +62,7 @@ test('methods options', assert => {

fetch.callsFake((url, options) => {
assert.match(url, new URL('https://pets.com/pets/1'))
assert.deepEqual(options, {
assert.same(options, {
method: 'get',
headers: {}
})
Expand All @@ -83,7 +83,7 @@ test('global defaults', assert => {

fetch.callsFake((url, options) => {
assert.match(url, new URL('https://pets.com/pets/1?name=ruby&is_good=yes'))
assert.deepEqual(options, {
assert.same(options, {
method: 'get',
headers: { 'x-pet-type': 'dog' }
})
Expand Down
22 changes: 22 additions & 0 deletions test/parse-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,25 @@ test('populates the server.url with server.variables', assert => {

assert.equal(parseServer({ url: 'localhost/{foo}', variables: { foo: 'bar' } }, spec), 'localhost/bar')
})

test('populates the server.url with server.variables with colons', assert => {
assert.plan(1)

const spec = {
servers: [{
url: 'localhost',
variables: { foo: {} }
}]
}

assert.equal(parseServer({
url: '{protocol}://{host}:{port}/{path1}/{path2}',
variables: {
protocol: 'ftp',
host: 'localhost',
port: '8080',
path1: 'path1',
path2: 'path2'
}
}, spec), 'ftp://localhost:8080/path1/path2')
})

0 comments on commit 28aa6d8

Please sign in to comment.