Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Exclude port from path if config.port not set. #162

Merged
merged 2 commits into from
Dec 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/request-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
// this option is only used internally, not passed to daemon
delete qs.followSymlinks

const port = config.port ? `:${config.port}` : ''

const opts = {
method: files ? 'POST' : 'GET',
uri: `${config.protocol}://${config.host}:${config.port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`,
uri: `${config.protocol}://${config.host}${port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handling this with the url module is probably best. It cleans up that template a lot. Not a huge issue though.

let uri = url.format({
  protocol: config.protocol,
  hostname: config.host,
  port: config.port,
  pathname: `${config['api-path']}/${path}`.replace(/\/+/g, '/'), // Double slashes don't matter, but it looks nicer.
  query: qs
})

It will handle the undefined port and not include it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

headers: {}
}

Expand Down
45 changes: 45 additions & 0 deletions test/request-api.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'

const ipfsAPI = require('../src/index.js')
const noop = () => {}

describe('ipfsAPI request tests', () => {
describe('requestAPI', () => {
const apiAddrs = require('./tmp-disposable-nodes-addrs.json')
const apiAddr = apiAddrs.a.split('/')

it('excludes port from URL if config.port is falsy', (done) => {
const Wreck = require('wreck')
const request = Wreck.request

Wreck.request = (method, uri, opts, cb) => {
Wreck.request = request
expect(uri).to.not.contain(/:\d/)
done()
}

ipfsAPI({
host: apiAddr[2],
port: null,
protocol: 'http'
}).id(noop)
})

it('includes port in URL if config.port is truthy', (done) => {
const Wreck = require('wreck')
const request = Wreck.request

Wreck.request = (method, uri, opts, cb) => {
Wreck.request = request
expect(uri).to.contain(':' + apiAddr[4])
done()
}

ipfsAPI({
host: apiAddr[2],
port: apiAddr[4],
protocol: 'http'
}).id(noop)
})
})
})