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.
Depends on - multiformats/js-uri-to-multiaddr#3 - ipfs-inactive/js-ipfs-http-client#935 Fixes ipfs/ipfs-webui#925 License: MIT Signed-off-by: Oli Evans <oli@tableflip.io>
- Loading branch information
Showing
2 changed files
with
29 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,80 @@ | ||
/* global it, expect, jest */ | ||
const tryApi = require('./index.js') | ||
const { URL } = require('url') | ||
const IpfsApi = require('ipfs-http-client') | ||
|
||
it('Should use the apiAddress', async (done) => { | ||
const opts = { | ||
apiAddress: '/ip4/1.1.1.1/tcp/1111', | ||
defaultApiAddress: '/ip4/127.0.0.1/tcp/5001', | ||
location: new URL('http://localhost:5001'), | ||
IpfsApi: jest.fn(), | ||
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) | ||
expect(opts.IpfsApi.mock.calls.length).toBe(1) | ||
const config = res.ipfs.util.getEndpointConfig() | ||
expect(config.host).toEqual('1.1.1.1') | ||
expect(config.port).toEqual('1111') | ||
expect(config.protocol).toEqual('http') | ||
done() | ||
}) | ||
|
||
it('Should use the location where hostname not localhost', async (done) => { | ||
const opts = { | ||
defaultApiAddress: '/ip4/127.0.0.1/tcp/5001', | ||
location: new URL('http://dev.local:5001'), | ||
IpfsApi: jest.fn(), | ||
IpfsApi: IpfsApi, | ||
ipfsConnectionTest: jest.fn().mockResolvedValueOnce(true) | ||
} | ||
const res = await tryApi(opts) | ||
expect(res.apiAddress).toEqual('/dnsaddr/dev.local/tcp/5001/http') | ||
expect(res.apiAddress).toEqual('/dns4/dev.local/tcp/5001/http') | ||
expect(res.provider).toEqual('js-ipfs-api') | ||
expect(opts.ipfsConnectionTest.mock.calls.length).toBe(1) | ||
expect(opts.IpfsApi.mock.calls.length).toBe(1) | ||
const config = res.ipfs.util.getEndpointConfig() | ||
expect(config.host).toEqual('dev.local') | ||
expect(config.port).toEqual('5001') | ||
expect(config.protocol).toEqual('http') | ||
done() | ||
}) | ||
|
||
it('Should use the location where port not 5001', async (done) => { | ||
const opts = { | ||
defaultApiAddress: '/ip4/127.0.0.1/tcp/5001', | ||
location: new URL('http://localhost:9999'), | ||
IpfsApi: jest.fn(), | ||
location: new URL('https://webui.ipfs.io'), | ||
IpfsApi: IpfsApi, | ||
ipfsConnectionTest: jest.fn().mockResolvedValueOnce(true) | ||
} | ||
const res = await tryApi(opts) | ||
expect(res.apiAddress).toEqual('/dnsaddr/localhost/tcp/9999/http') | ||
expect(res.apiAddress).toEqual('/dns4/webui.ipfs.io/tcp/443/https') | ||
expect(res.provider).toEqual('js-ipfs-api') | ||
expect(opts.ipfsConnectionTest.mock.calls.length).toBe(1) | ||
expect(opts.IpfsApi.mock.calls.length).toBe(1) | ||
const config = res.ipfs.util.getEndpointConfig() | ||
expect(config.host).toEqual('webui.ipfs.io') | ||
expect(config.port).toEqual('443') | ||
expect(config.protocol).toEqual('https') | ||
done() | ||
}) | ||
|
||
it('Should use the defaultApiAddress if location fails', async (done) => { | ||
const opts = { | ||
defaultApiAddress: '/ip4/127.0.0.1/tcp/5001', | ||
location: new URL('http://astro.cat:5001'), | ||
IpfsApi: jest.fn(), | ||
IpfsApi: IpfsApi, | ||
// location call fails, default ok | ||
ipfsConnectionTest: jest.fn() | ||
.mockRejectedValueOnce(new Error('nope')) | ||
.mockResolvedValueOnce(true) | ||
} | ||
const res = await tryApi(opts) | ||
console.log('res', res) | ||
expect(res.apiAddress).toEqual(opts.defaultApiAddress) | ||
expect(res.provider).toEqual('js-ipfs-api') | ||
expect(opts.ipfsConnectionTest.mock.calls.length).toBe(2) | ||
expect(opts.IpfsApi.mock.calls.length).toBe(2) | ||
const config = res.ipfs.util.getEndpointConfig() | ||
expect(config.host).toEqual('127.0.0.1') | ||
expect(config.port).toEqual('5001') | ||
expect(config.protocol).toEqual('http') | ||
done() | ||
}) |