-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1273 from yashkohli88/yk/add-default-headers-cent…
…rally Refactoring default header in fetch.js in Service
- Loading branch information
Showing
8 changed files
with
60 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,10 @@ | |
|
||
const axios = require('axios') | ||
|
||
const defaultHeaders = Object.freeze({ 'User-Agent': 'clearlydefined.io crawler ([email protected])' }) | ||
|
||
axios.defaults.headers = defaultHeaders | ||
|
||
function buildRequestOptions(request) { | ||
let responseType = 'text' | ||
if (request.json) { | ||
|
@@ -48,4 +52,4 @@ function withDefaults(opts) { | |
return request => callFetch(request, axiosInstance) | ||
} | ||
|
||
module.exports = { callFetch, withDefaults } | ||
module.exports = { callFetch, withDefaults, defaultHeaders } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
const { fail } = require('assert') | ||
const { callFetch, withDefaults } = require('../../lib/fetch') | ||
const { callFetch, withDefaults, defaultHeaders } = require('../../lib/fetch') | ||
const { expect } = require('chai') | ||
const fs = require('fs') | ||
const mockttp = require('mockttp') | ||
|
||
function checkDefaultHeaders(headers) { | ||
for (const [key, value] of Object.entries(defaultHeaders)) { | ||
expect(headers).to.have.property(key.toLowerCase()).that.equals(value) | ||
} | ||
} | ||
|
||
describe('CallFetch', () => { | ||
describe('with mock server', () => { | ||
const mockServer = mockttp.getLocal() | ||
|
@@ -23,6 +29,40 @@ describe('CallFetch', () => { | |
expect(response).to.be.deep.equal(JSON.parse(expected)) | ||
}) | ||
|
||
it('checks if all the default headers are present in requests', async () => { | ||
const path = '/search.maven.org/solrsearch/select' | ||
const exactQuery = '?q=g:org.httpcomponents+AND+a:httpcomponents&core=gav&rows=100&wt=json' | ||
|
||
const endpointMock = await mockServer.forGet(path).withExactQuery(exactQuery).thenReply(200, 'success') | ||
|
||
await callFetch({ | ||
url: mockServer.urlFor(path + exactQuery), | ||
method: 'GET', | ||
json: true | ||
}) | ||
|
||
const requests = await endpointMock.getSeenRequests() | ||
checkDefaultHeaders(requests[0].headers) | ||
}) | ||
|
||
it('checks if all the default headers and other specific header is present in crate component', async () => { | ||
const path = '/crates.io/api/v1/crates/name' | ||
const endpointMock = await mockServer.forGet(path).thenReply(200, 'success') | ||
|
||
await callFetch({ | ||
url: mockServer.urlFor(path), | ||
method: 'GET', | ||
json: true, | ||
encoding: null, | ||
headers: { | ||
Accept: 'text/html' | ||
} | ||
}) | ||
const requests = await endpointMock.getSeenRequests() | ||
checkDefaultHeaders(requests[0].headers) | ||
expect(requests[0].headers).to.include({ accept: 'text/html' }) | ||
}) | ||
|
||
it('checks if the full response is fetched', async () => { | ||
const path = '/registry.npmjs.com/redis/0.1.0' | ||
const expected = fs.readFileSync('test/fixtures/fetch/redis-0.1.0.json') | ||
|
@@ -87,17 +127,17 @@ describe('CallFetch', () => { | |
const url = mockServer.urlFor(path) | ||
const endpointMock = await mockServer.forGet(path).thenReply(200) | ||
|
||
const defaultOptions = { headers: { 'user-agent': 'clearlydefined.io crawler ([email protected])' } } | ||
const defaultOptions = { headers: defaultHeaders } | ||
const requestWithDefaults = withDefaults(defaultOptions) | ||
await requestWithDefaults({ url }) | ||
await requestWithDefaults({ url }) | ||
|
||
const requests = await endpointMock.getSeenRequests() | ||
expect(requests.length).to.equal(2) | ||
expect(requests[0].url).to.equal(url) | ||
expect(requests[0].headers).to.include(defaultOptions.headers) | ||
checkDefaultHeaders(requests[0].headers) | ||
expect(requests[1].url).to.equal(url) | ||
expect(requests[1].headers).to.include(defaultOptions.headers) | ||
checkDefaultHeaders(requests[1].headers) | ||
}) | ||
|
||
it('checks if the response is text with uri option in GET request', async () => { | ||
|
@@ -129,6 +169,7 @@ describe('CallFetch', () => { | |
const json = await requests[0].body.getJson() | ||
expect(json).to.deep.equal({ test: 'test' }) | ||
expect(requests[0].headers).to.include({ 'x-crawler': 'secret' }) | ||
expect(checkDefaultHeaders(requests[0].headers)) | ||
}) | ||
|
||
it('should GET with withCredentials set to false', async function () { | ||
|