Skip to content

Commit

Permalink
Merge pull request #247 from mia-platform/fix/baseURL
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebianchi committed Jul 8, 2022
2 parents bd03a01 + b0920b9 commit befa27f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- fixed http client typings
- base url with path prefix

## v5.0.0 - 2022-05-13

Expand Down
14 changes: 4 additions & 10 deletions lib/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
'use strict'

const axios = require('axios')
const nodeUrl = require('url')
const httpsClient = require('https')
const Pino = require('pino')

class HttpClient {
constructor(baseUrl, requestHeadersToProxy, baseOptions = {}) {
this.baseUrl = baseUrl
this.baseURL = baseUrl
this.requestHeadersToProxy = requestHeadersToProxy
this.baseOptions = baseOptions

const httpsAgent = getHttpsAgent(baseOptions)
this.axios = axios.create({
...httpsAgent ? { httpsAgent } : {},
baseURL: baseUrl,
timeout: baseOptions.timeout,
})
}
Expand Down Expand Up @@ -81,15 +81,14 @@ class HttpClient {
}
}

async makeCall(path, payload, options) {
async makeCall(url, payload, options) {
const logger = this.getLogger(options)
const headers = getHeaders(options, this.requestHeadersToProxy, this.baseOptions, payload)
const url = getUrl(this.baseUrl, path, this.baseOptions)
const httpsAgent = getHttpsAgent(options)
const errorMessageKey = getErrorMessageKey(options, this.baseOptions)
try {
const validateStatus = getValidateStatus(options)
logger.trace({ url, headers, payload }, 'make call')
logger.trace({ baseURL: this.baseURL, url, headers, payload }, 'make call')
const response = await this.axios({
url,
method: options.method,
Expand Down Expand Up @@ -191,11 +190,6 @@ function getResponseType({ returnAs = 'JSON' }) {
}
}

function getUrl(baseUrl, path) {
const urlObj = new nodeUrl.URL(path, baseUrl)
return urlObj.toString()
}

function isMiaHeaderToInject(baseOptions, options) {
if (options.isMiaHeaderInjected !== undefined) {
return options.isMiaHeaderInjected
Expand Down
34 changes: 30 additions & 4 deletions tests/httpClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,28 @@ tap.test('httpClient', test => {
assert.end()
})

innerTest.test('returnAs: JSON with prefix', async assert => {
const basePrefix = '/prefix'
const myServiceNameScope = nock(MY_AWESOME_SERVICE_PROXY_HTTP_URL)
.replyContentLength()
.get(`${basePrefix}/foo`)
.reply(200, { the: 'response' }, {
some: 'response-header',
})

const service = new HttpClient(`${MY_AWESOME_SERVICE_PROXY_HTTP_URL}${basePrefix}`)

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

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

myServiceNameScope.done()
assert.end()
})

innerTest.test('returnAs: JSON default', async assert => {
const myServiceNameScope = nock(MY_AWESOME_SERVICE_PROXY_HTTP_URL)
.replyContentLength()
Expand Down Expand Up @@ -1811,9 +1833,10 @@ tap.test('httpClient', test => {

stream.once('data', beforeRequest => {
assert.match(beforeRequest, {
baseURL: MY_AWESOME_SERVICE_PROXY_HTTP_URL,
level: 10,
msg: /^make call$/,
url: new RegExp(`^${MY_AWESOME_SERVICE_PROXY_HTTP_URL}/foo$`),
url: '/foo',
time: /[0-9]+/,
headers: {
some: 'value',
Expand Down Expand Up @@ -1865,8 +1888,9 @@ tap.test('httpClient', test => {
stream.once('data', beforeRequest => {
assert.match(beforeRequest, {
level: 10,
baseURL: MY_AWESOME_SERVICE_PROXY_HTTP_URL,
msg: /^make call$/,
url: new RegExp(`^${MY_AWESOME_SERVICE_PROXY_HTTP_URL}/foo$`),
url: '/foo',
time: /[0-9]+/,
headers: {
some: 'value',
Expand Down Expand Up @@ -1919,8 +1943,9 @@ tap.test('httpClient', test => {
stream.once('data', beforeRequest => {
assert.match(beforeRequest, {
level: 10,
baseURL: MY_AWESOME_SERVICE_PROXY_HTTP_URL,
msg: /^make call$/,
url: new RegExp(`^${MY_AWESOME_SERVICE_PROXY_HTTP_URL}/foo$`),
url: '/foo',
time: /[0-9]+/,
headers: {
some: 'value',
Expand Down Expand Up @@ -1971,8 +1996,9 @@ tap.test('httpClient', test => {
stream.once('data', beforeRequest => {
assert.match(beforeRequest, {
level: 10,
baseURL: MY_AWESOME_SERVICE_PROXY_HTTP_URL,
msg: /^make call$/,
url: new RegExp(`^${MY_AWESOME_SERVICE_PROXY_HTTP_URL}/foo$`),
url: '/foo',
time: /[0-9]+/,
headers: {
some: 'value',
Expand Down

0 comments on commit befa27f

Please sign in to comment.