Skip to content

Commit

Permalink
Merge 0b569d6 into 51ff057
Browse files Browse the repository at this point in the history
  • Loading branch information
allevo committed Aug 23, 2018
2 parents 51ff057 + 0b569d6 commit 12ebc91
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ declare namespace customPlugin {
port?: number,
protocol?: 'http' | 'https',
headers?: object,
prefix?: string,
}
interface ServiceOptions {
returnAs: 'JSON' | 'BUFFER' | 'STREAM'
Expand Down
3 changes: 2 additions & 1 deletion lib/serviceBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ function makeCallAsJSON(endUrl, method, body, options) {
function makeCall(hostname, path, method, querystring, body, options = {}) {
const protocol = (options && options.protocol) ? options.protocol : 'http'
const port = (options && options.port) ? options.port : undefined
const pathWithPrefix = ((options && options.prefix) ? options.prefix : '') + path
const endUrl = url.format({
protocol,
hostname,
port,
pathname: path,
pathname: pathWithPrefix,
query: querystring,
})
const asJSON = !options || !options.returnAs || options.returnAs === 'JSON'
Expand Down
22 changes: 22 additions & 0 deletions tests/serviceBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,28 @@ t.test('serviceBuilder', t => {
myServiceNameScope.done()
})

t.test('with prefix', async t => {
t.plan(4)

const myServiceNameScope = nock('http://my-service-name')
.replyContentLength()
.get('/my-prefix/foo?aa=bar')
.reply(200, { the: 'response' }, {
some: 'response-header',
})

const service = serviceBuilder('my-service-name', { }, { prefix: '/my-prefix' })

const response = await service.get('/foo', { aa: 'bar' }, { returnAs: 'JSON' })

t.equal(response.statusCode, 200)
t.strictSame(response.payload, { the: 'response' })
t.strictSame(response.headers.some, 'response-header')
t.ok(response.headers['content-length'])

myServiceNameScope.done()
})

t.end()
})

Expand Down
3 changes: 3 additions & 0 deletions tests/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ a(async function (service) {
const proxiedServiceWithOptionsAndHeaders: cpl.Service = request.getServiceProxy({port: 3000, protocol: 'http', headers: { key: 'value1' }})
await invokeSomeApis(proxiedServiceWithOptionsAndHeaders)

const proxiedServiceWithPrefix: cpl.Service = request.getServiceProxy({port: 3000, protocol: 'http', prefix: '/my-prefix'})
await invokeSomeApis(proxiedServiceWithPrefix)

return { 'aa': 'boo' }
}, {
headers: {
Expand Down

0 comments on commit 12ebc91

Please sign in to comment.