Skip to content

Commit

Permalink
Add handling of simple
Browse files Browse the repository at this point in the history
  • Loading branch information
qtomlinson committed Jul 7, 2024
1 parent e109cac commit d512582
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
16 changes: 12 additions & 4 deletions lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,31 @@ function buildRequestOptions(request) {
responseType = 'stream'
}

const validateOptions = {}
if (!request.simple) {
validateOptions.validateStatus = () => true
}

return {
method: request.method,
url: request.url,
responseType,
headers: request.headers,
data: request.body
data: request.body,
...validateOptions
}
}

async function callFetch(request, axiosInstance = axios) {
try {
const options = buildRequestOptions(request)
const response = await axiosInstance(options)
if (request.resolveWithFullResponse) return response
return response.data
if (!request.resolveWithFullResponse) return response.data
response.statusCode = response.status
response.statusMessage = response.statusText
return response
} catch (error) {
if (error.response && request.resolveWithFullResponse) return error.response
error.statusCode = error.response?.status
throw error
}
}
Expand Down
49 changes: 43 additions & 6 deletions test/unit/lib/fetchTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ describe('CallFetch', () => {
method: 'GET',
resolveWithFullResponse: true
})
expect(response.status).to.be.equal(200)
expect(response.statusText).to.be.equal('OK')
expect(response.statusCode).to.be.equal(200)
expect(response.statusMessage).to.be.equal('OK')
})

it('checks if the full response is fetched with error code', async () => {
it('should throw error with error code', async () => {
const path = '/registry.npmjs.com/redis/0.1.'
await mockServer.forGet(path).thenReply(404)

const response = await callFetch({
await callFetch({
url: mockServer.urlFor(path),
method: 'GET',
json: 'true',
resolveWithFullResponse: true
}).catch(err => {
expect(err.statusCode).to.be.equal(404)
})
expect(response.status).to.be.equal(404)
expect(response.statusText).to.be.equal('Not Found')
})

it('checks if the response is text while sending GET request', async () => {
Expand Down Expand Up @@ -96,6 +96,43 @@ describe('CallFetch', () => {
expect(requests[1].url).to.equal(url)
expect(requests[1].headers).to.include(defaultOptions.headers)
})

describe('test simple', () => {
it('should handle 300 when simple is true by default', async () => {
const path = '/registry.npmjs.com/redis/0.1.0'
await mockServer.forGet(path).thenReply(300, 'test')

await callFetch({
url: mockServer.urlFor(path)
}).catch(err => {
expect(err.statusCode).to.be.equal(300)
})
})

it('should handle 300 with simple === false', async () => {
const path = '/registry.npmjs.com/redis/0.1.0'
await mockServer.forGet(path).thenReply(300, 'test')

const response = await callFetch({
url: mockServer.urlFor(path),
simple: false
})
expect(response).to.be.equal('test')
})

it('should return status 300 with simple === false', async () => {
const path = '/registry.npmjs.com/redis/0.1.0'
await mockServer.forGet(path).thenReply(300, 'test')

const response = await callFetch({
url: mockServer.urlFor(path),
simple: false,
resolveWithFullResponse: true
})
expect(response.statusCode).to.be.equal(300)
expect(response.statusMessage).to.be.equal('Multiple Choices')
})
})
})

describe('test crate download', () => {
Expand Down

0 comments on commit d512582

Please sign in to comment.