Skip to content

Commit

Permalink
Merge pull request #2 from qtomlinson/qt/pull_request_upgrade
Browse files Browse the repository at this point in the history
Support uri in options
  • Loading branch information
yashkohli88 authored Jul 16, 2024
2 parents 54b6c32 + 250d785 commit 7b0d444
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function buildRequestOptions(request) {

return {
method: request.method,
url: request.url,
url: request.url || request.uri,
responseType,
headers: request.headers,
data: request.body,
Expand Down
2 changes: 1 addition & 1 deletion providers/store/webhookDeltaStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class WebhookDeltaStore {
const uri = this.options.url
var options = {
method: 'POST',
url: uri,
uri,
json: true,
body: pick(document, '_metadata'),
headers: {
Expand Down
73 changes: 59 additions & 14 deletions test/unit/lib/fetchTests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { fail } = require('assert')
const { callFetch, withDefaults } = require('../../../lib/fetch')
const { expect } = require('chai')
const fs = require('fs')
Expand Down Expand Up @@ -39,15 +40,17 @@ describe('CallFetch', () => {
it('should throw error with error code', async () => {
const path = '/registry.npmjs.com/redis/0.1.'
await mockServer.forGet(path).thenReply(404)

await callFetch({
url: mockServer.urlFor(path),
method: 'GET',
json: 'true',
resolveWithFullResponse: true
}).catch(err => {
try {
await callFetch({
url: mockServer.urlFor(path),
method: 'GET',
json: 'true',
resolveWithFullResponse: true
})
fail('should have thrown')
} catch (err) {
expect(err.statusCode).to.be.equal(404)
})
}
})

it('checks if the response is text while sending GET request', async () => {
Expand All @@ -71,7 +74,7 @@ describe('CallFetch', () => {
encoding: null
})
const destination = 'test/fixtures/fetch/temp.json'
await new Promise((resolve) => {
await new Promise(resolve => {
response.pipe(fs.createWriteStream(destination).on('finish', () => resolve(true)))
})
const downloaded = JSON.parse(fs.readFileSync(destination))
Expand All @@ -97,16 +100,48 @@ describe('CallFetch', () => {
expect(requests[1].headers).to.include(defaultOptions.headers)
})

it('checks if the response is text with uri option in GET request', async () => {
const path = '/proxy.golang.org/rsc.io/quote/@v/v1.3.0.mod'
await mockServer.forGet(path).thenReply(200, 'done')

const response = await callFetch({
uri: mockServer.urlFor(path),
method: 'GET'
})
expect(response).to.be.equal('done')
})

it('should POST with JSON', async function () {
const path = '/webhook'
const endpointMock = await mockServer.forPost(path).thenReply(200, 'done')

const response = await callFetch({
method: 'POST',
uri: mockServer.urlFor(path),
json: true,
body: { test: 'test' },
headers: { 'x-crawler': 'secret' },
resolveWithFullResponse: true
})
expect(response.statusCode).to.be.equal(200)
const requests = await endpointMock.getSeenRequests()
expect(requests.length).to.equal(1)
const json = await requests[0].body.getJson()
expect(json).to.deep.equal({ test: 'test' })
expect(requests[0].headers).to.include({ 'x-crawler': 'secret' })
})

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 => {
try {
await callFetch({ url: mockServer.urlFor(path) })
fail('should have thrown')
} catch (err) {
expect(err.statusCode).to.be.equal(300)
})
}
})

it('should handle 300 with simple === false', async () => {
Expand Down Expand Up @@ -150,6 +185,11 @@ describe('CallFetch', () => {
// Validating the length of the content in order to verify the response is a crate package.
// JSON response would not return this header in response resulting in failing this test case.
expect(response.headers['content-length']).to.be.equal('15282')
return new Promise((resolve, reject) => {
response.on('data', () => {})
response.on('end', () => resolve(true))
response.on('error', reject)
})
})

it('should download the package when responseType is stream', async () => {
Expand All @@ -160,6 +200,11 @@ describe('CallFetch', () => {
})
// Validating the length of the content inorder to verify the response is a crate package.
expect(response.headers['content-length']).to.be.equal('15282')
return new Promise((resolve, reject) => {
response.on('data', () => {})
response.on('end', () => resolve(true))
response.on('error', reject)
})
})
})
})
})

0 comments on commit 7b0d444

Please sign in to comment.