Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validate granada feed through new GOT user-agent #177

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions gbfs-validator/__test__/__snapshots__/gbfs.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
exports[`initialization should correctly initialize validator per default 1`] = `
GBFS {
"auth": Object {},
"gotOptions": Object {},
"gotOptions": Object {
"headers": Object {
"user-agent": "MobilityData GBFS-Validator/1.1.1 (Node 16.0.1)",
},
},
"options": Object {
"docked": false,
"freefloating": false,
Expand All @@ -16,7 +20,11 @@ GBFS {
exports[`initialization should correctly initialize validator with options 1`] = `
GBFS {
"auth": Object {},
"gotOptions": Object {},
"gotOptions": Object {
"headers": Object {
"user-agent": "MobilityData GBFS-Validator/1.1.1 (Node 16.0.1)",
},
},
"options": Object {
"docked": true,
"freefloating": true,
Expand Down Expand Up @@ -73,20 +81,31 @@ GBFS {
}
`;

exports[`initialization with auth should correctly initialize with one \`headers\` 1`] = `
exports[`initialization with auth should correctly initialize with multiple \`headers\` 1`] = `
GBFS {
"auth": Object {
"headers": Array [
Object {
"key": "mykey",
"value": "myvalue",
},
Object {
"key": "mysecondkey",
"value": "mysecondvalue",
},
Object {
"key": "mythirdkey",
"value": "mythirdvalue",
},
],
"type": "headers",
},
"gotOptions": Object {
"headers": Object {
"mykey": "myvalue",
"mysecondkey": "mysecondvalue",
"mythirdkey": "mythirdvalue",
"user-agent": "MobilityData GBFS-Validator/1.1.1 (Node 16.0.1)",
},
},
"options": Object {
Expand All @@ -98,30 +117,21 @@ GBFS {
}
`;

exports[`initialization with auth should correctly initialize with multiple \`headers\` 1`] = `
exports[`initialization with auth should correctly initialize with one \`headers\` 1`] = `
GBFS {
"auth": Object {
"headers": Array [
Object {
"key": "mykey",
"value": "myvalue",
},
Object {
"key": "mysecondkey",
"value": "mysecondvalue",
},
Object {
"key": "mythirdkey",
"value": "mythirdvalue",
},
],
"type": "headers",
},
"gotOptions": Object {
"headers": Object {
"mykey": "myvalue",
"mysecondkey": "mysecondvalue",
"mythirdkey": "mythirdvalue",
"user-agent": "MobilityData GBFS-Validator/1.1.1 (Node 16.0.1)",
},
},
"options": Object {
Expand All @@ -131,4 +141,4 @@ GBFS {
},
"url": "http://localhost:8888/gbfs.json",
}
`;
`;
9 changes: 9 additions & 0 deletions gbfs-validator/__test__/gbfs.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
const GBFS = require('../gbfs')
const pjson = require('../package.json')

const serverOpts = {
port: 0,
host: '127.0.0.1',
}

describe('initialization', () => {

beforeAll(() => {
// mocks process and pacakge.json for consistent tests
const originalProcess = process
global.process = {...originalProcess, versions: {node: '16.0.1'}}
pjson.version = '1.1.1'
})

test('should correctly initialize validator per default', () => {
expect(new GBFS('http://localhost:8888/gbfs.json')).toMatchSnapshot()
})
Expand Down
13 changes: 11 additions & 2 deletions gbfs-validator/gbfs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const got = require('got')
const validate = require('./validate')
var packageJson = require('./package.json');
const validatorVersion = process.env.COMMIT_REF
? process.env.COMMIT_REF.substring(0, 7)
: require('./package.json').version
Expand Down Expand Up @@ -281,6 +282,8 @@ class GBFS {
throw new Error('Missing URL')
}

const userAgent = `MobilityData GBFS-Validator/${packageJson?.version} (Node ${process?.versions['node']})`

this.url = url
this.options = {
docked,
Expand All @@ -289,7 +292,11 @@ class GBFS {
}
this.auth = auth

this.gotOptions = {}
this.gotOptions = {
headers: {
'user-agent': userAgent
}
}

if (this.auth && this.auth.type) {
if (this.auth.type === 'basic_auth' && this.auth.basicAuth) {
Expand All @@ -308,7 +315,9 @@ class GBFS {
}

if (this.auth.type === 'headers') {
this.gotOptions.headers = {}
this.gotOptions.headers = {
'user-agent': userAgent
}
for (var header of this.auth.headers) {
if (header && header.value) {
this.gotOptions.headers[header.key] = header.value
Expand Down
Loading