Skip to content

Commit

Permalink
fix: validate granada feed through new GOT user-agent (#177)
Browse files Browse the repository at this point in the history
* changed default GOT user-agent
  • Loading branch information
Alessandro100 authored Apr 16, 2024
1 parent bd870f6 commit 6588a77
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
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

0 comments on commit 6588a77

Please sign in to comment.