This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow urls with authentication (#28)
License: MIT Signed-off-by: Henrique Dias <[email protected]>
- Loading branch information
Showing
4 changed files
with
95 additions
and
17 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
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 |
---|---|---|
|
@@ -115,3 +115,41 @@ it('Should use the defaultApiAddress if location fails', async (done) => { | |
expect(config.protocol).toEqual('http') | ||
done() | ||
}) | ||
|
||
it('Should use the apiAddress (url)', async (done) => { | ||
const opts = { | ||
apiAddress: 'http://1.1.1.1:1111', | ||
defaultApiAddress: '/ip4/127.0.0.1/tcp/5001', | ||
location: new URL('http://localhost:5001'), | ||
IpfsApi: IpfsApi, | ||
ipfsConnectionTest: jest.fn().mockResolvedValueOnce(true) | ||
} | ||
const res = await tryApi(opts) | ||
expect(res.apiAddress).toEqual(opts.apiAddress) | ||
expect(res.provider).toEqual('js-ipfs-api') | ||
expect(opts.ipfsConnectionTest.mock.calls.length).toBe(1) | ||
const config = res.ipfs.getEndpointConfig() | ||
expect(config.host).toEqual('1.1.1.1') | ||
expect(config.port).toEqual('1111') | ||
expect(config.protocol).toEqual('http') | ||
done() | ||
}) | ||
|
||
it('Should use the apiAddress (url with basic auth)', async (done) => { | ||
const opts = { | ||
apiAddress: 'http://user:[email protected]:1111', | ||
defaultApiAddress: '/ip4/127.0.0.1/tcp/5001', | ||
location: new URL('http://localhost:5001'), | ||
IpfsApi: IpfsApi, | ||
ipfsConnectionTest: jest.fn().mockResolvedValueOnce(true) | ||
} | ||
const res = await tryApi(opts) | ||
expect(res.apiAddress).toEqual(opts.apiAddress) | ||
expect(res.provider).toEqual('js-ipfs-api') | ||
expect(opts.ipfsConnectionTest.mock.calls.length).toBe(1) | ||
const config = res.ipfs.getEndpointConfig() | ||
expect(config.host).toEqual('1.1.1.1') | ||
expect(config.port).toEqual('1111') | ||
expect(config.protocol).toEqual('http') | ||
done() | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
/* eslint-env browser, webextensions */ | ||
|
||
const multiaddr = require('multiaddr') | ||
|
||
function isMultiaddress (addr) { | ||
if (addr === null || addr === undefined || typeof addr === 'undefined') { | ||
return false | ||
} | ||
|
||
try { | ||
multiaddr(addr) | ||
return true | ||
} catch (_) { | ||
return false | ||
} | ||
} | ||
|
||
function isURL (addr) { | ||
try { | ||
// eslint-disable-next-line | ||
new URL(addr) | ||
return true | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
|
||
module.exports = { | ||
isMultiaddress, | ||
isURL | ||
} |