Skip to content

Commit 2a6d4af

Browse files
Peter Wilhelmsson2hdddg
Peter Wilhelmsson
authored andcommitted
Custom user agent
1 parent 6ff009e commit 2a6d4af

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ import ServerAddress from './internal/server-address'
173173
* resolver: function(address) {
174174
* return ['127.0.0.1:8888', 'fallback.db.com:7687'];
175175
* },
176+
*
177+
* // Optionally override the default user agent name.
178+
* userAgent: USER_AGENT
176179
* }
177180
*
178181
* @param {string} url The URL for the Neo4j database, for instance "neo4j://localhost" and/or "bolt://localhost"
@@ -232,11 +235,14 @@ function driver (url, authToken, config = {}) {
232235
authToken = authToken || {}
233236
authToken.scheme = authToken.scheme || 'none'
234237

238+
// Use default user agent or user agent specified by user.
239+
config.userAgent = config.userAgent || USER_AGENT
240+
235241
if (routing) {
236242
return new RoutingDriver(
237243
ServerAddress.fromUrl(parsedUrl.hostAndPort),
238244
parsedUrl.query,
239-
USER_AGENT,
245+
config.userAgent,
240246
authToken,
241247
config
242248
)
@@ -248,7 +254,7 @@ function driver (url, authToken, config = {}) {
248254
}
249255
return new Driver(
250256
ServerAddress.fromUrl(parsedUrl.hostAndPort),
251-
USER_AGENT,
257+
config.userAgent,
252258
authToken,
253259
config
254260
)

test/internal/node/direct.driver.boltkit.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ describe('#stub-direct direct driver with stub server', () => {
6565
it('v4', () => verifyShouldRunQuery('v4'))
6666
})
6767

68+
it('should use custom user agent', async () => {
69+
if (!boltStub.supported) {
70+
return
71+
}
72+
const server = await boltStub.start(
73+
'./test/resources/boltstub/v4/hello_custom_user_agent.script',
74+
9001
75+
)
76+
const driver = boltStub.newDriver('bolt://127.0.0.1:9001', {
77+
userAgent: 'custom user agent'
78+
})
79+
const session = driver.session()
80+
await session.run('MATCH (n) RETURN n.name')
81+
await driver.close()
82+
await server.exit()
83+
})
84+
6885
describe('should not send any routing with hello to disable server routing', () => {
6986
async function verify (version) {
7087
if (!boltStub.supported) {

test/internal/node/routing.driver.boltkit.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ describe('#stub-routing routing driver with stub server', () => {
3838
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout
3939
})
4040

41+
it('should use custom user agent', async () => {
42+
if (!boltStub.supported) {
43+
return
44+
}
45+
const server = await boltStub.start(
46+
'./test/resources/boltstub/v4/hello_custom_user_agent.script',
47+
9001
48+
)
49+
const driver = boltStub.newDriver('bolt://127.0.0.1:9001', {
50+
userAgent: 'custom user agent'
51+
})
52+
const session = driver.session()
53+
await session.run('MATCH (n) RETURN n.name')
54+
await driver.close()
55+
await server.exit()
56+
})
57+
4158
it('should discover servers', async () => {
4259
if (!boltStub.supported) {
4360
return
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
!: BOLT 4
2+
!: AUTO RESET
3+
4+
C: HELLO {"credentials": "password", "scheme": "basic", "user_agent": "custom user agent", "principal": "neo4j"}
5+
S: SUCCESS {"server": "Neo4j/9.9.9", "connection_id": "bolt-123456789"}
6+
C: RUN "MATCH (n) RETURN n.name" {} {}
7+
PULL {"n": 1000}
8+
S: SUCCESS {"fields": ["n.name"]}
9+
SUCCESS {}
10+
S: <EXIT>

0 commit comments

Comments
 (0)