-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support username in URI (#1284)
- Loading branch information
Showing
4 changed files
with
92 additions
and
6 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 |
---|---|---|
|
@@ -135,6 +135,13 @@ You can also specify connection options as a [`redis://` URL](http://www.iana.or | |
```javascript | ||
// Connect to 127.0.0.1:6380, db 4, using password "authpassword": | ||
new Redis("redis://:[email protected]:6380/4"); | ||
|
||
// Username can also be passed via URI. | ||
// It's worth to noticing that for compatibility reasons `allowUsernameInURI` | ||
// need to be provided, otherwise the username part will be ignored. | ||
new Redis( | ||
"redis://username:[email protected]:6380/4?allowUsernameInURI=true" | ||
); | ||
``` | ||
|
||
See [API Documentation](API.md#new_Redis) for all available options. | ||
|
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 |
---|---|---|
|
@@ -170,6 +170,25 @@ describe("auth", function () { | |
redis = new Redis({ port: 17379, username, password }); | ||
}); | ||
|
||
it("should handle auth with Redis URL string with username and password (Redis >=6) (redis://foo:[email protected]/) correctly", function (done) { | ||
let username = "user"; | ||
let password = "pass"; | ||
let redis; | ||
new MockServer(17379, function (argv) { | ||
if ( | ||
argv[0] === "auth" && | ||
argv[1] === username && | ||
argv[2] === password | ||
) { | ||
redis.disconnect(); | ||
done(); | ||
} | ||
}); | ||
redis = new Redis( | ||
`redis://user:pass@localhost:17379/?allowUsernameInURI=true` | ||
); | ||
}); | ||
|
||
it('should not emit "error" when the Redis >=6 server doesn\'t need auth', function (done) { | ||
new MockServer(17379, function (argv) { | ||
if (argv[0] === "auth" && argv[1] === "pass") { | ||
|
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 |
---|---|---|
|
@@ -204,9 +204,7 @@ describe("utils", function () { | |
password: "pass:word", | ||
key: "value", | ||
}); | ||
expect( | ||
utils.parseURL("redis://[email protected]:6380/4?key=value") | ||
).to.eql({ | ||
expect(utils.parseURL("redis://[email protected]:6380/4?key=value")).to.eql({ | ||
host: "127.0.0.1", | ||
port: "6380", | ||
db: "4", | ||
|
@@ -226,6 +224,59 @@ describe("utils", function () { | |
key: "value", | ||
}); | ||
}); | ||
|
||
it("supports allowUsernameInURI", function () { | ||
expect( | ||
utils.parseURL( | ||
"redis://user:[email protected]:6380/4?allowUsernameInURI=true" | ||
) | ||
).to.eql({ | ||
host: "127.0.0.1", | ||
port: "6380", | ||
db: "4", | ||
username: "user", | ||
password: "pass", | ||
}); | ||
expect( | ||
utils.parseURL( | ||
"redis://user:[email protected]:6380/4?allowUsernameInURI=false" | ||
) | ||
).to.eql({ | ||
host: "127.0.0.1", | ||
port: "6380", | ||
db: "4", | ||
password: "pass", | ||
}); | ||
expect( | ||
utils.parseURL( | ||
"redis://user:pass:[email protected]:6380/4?key=value&allowUsernameInURI=true" | ||
) | ||
).to.eql({ | ||
host: "127.0.0.1", | ||
port: "6380", | ||
db: "4", | ||
username: "user", | ||
password: "pass:word", | ||
key: "value", | ||
}); | ||
expect( | ||
utils.parseURL( | ||
"redis://[email protected]:6380/4?key=value&allowUsernameInURI=true" | ||
) | ||
).to.eql({ | ||
host: "127.0.0.1", | ||
port: "6380", | ||
db: "4", | ||
username: "user", | ||
password: "", | ||
key: "value", | ||
}); | ||
expect( | ||
utils.parseURL("redis://127.0.0.1/?allowUsernameInURI=true") | ||
).to.eql({ | ||
host: "127.0.0.1", | ||
}); | ||
}); | ||
}); | ||
|
||
describe(".sample", function () { | ||
|