11/* eslint-env mocha */
22'use strict'
33
4+ const multiaddr = require ( 'multiaddr' )
45const chai = require ( 'chai' )
56const dirtyChai = require ( 'dirty-chai' )
67const expect = chai . expect
@@ -9,18 +10,74 @@ chai.use(dirtyChai)
910const f = require ( './utils/factory' )
1011const ipfsClient = require ( '../src/index.js' )
1112
12- function clientWorks ( client , done ) {
13- client . id ( ( err , id ) => {
14- expect ( err ) . to . not . exist ( )
13+ describe ( 'ipfs-http-client constructor tests' , ( ) => {
14+ describe ( 'parameter permuations' , ( ) => {
15+ it ( 'none' , ( ) => {
16+ const ipfs = ipfsClient ( )
17+ if ( typeof self !== 'undefined' ) {
18+ const { hostname, port } = self . location
19+ expectConfig ( ipfs , { host : hostname , port } )
20+ } else {
21+ expectConfig ( ipfs , { } )
22+ }
23+ } )
1524
16- expect ( id ) . to . have . a . property ( 'id' )
17- expect ( id ) . to . have . a . property ( 'publicKey' )
18- done ( )
25+ it ( 'opts' , ( ) => {
26+ const host = 'wizard.world'
27+ const port = '999'
28+ const protocol = 'https'
29+ const ipfs = ipfsClient ( { host, port, protocol } )
30+ expectConfig ( ipfs , { host, port, protocol } )
31+ } )
32+
33+ it ( 'mutliaddr dns4 string, opts' , ( ) => {
34+ const host = 'foo.com'
35+ const port = '1001'
36+ const protocol = 'https'
37+ const addr = `/dns4/${ host } /tcp/${ port } `
38+ const ipfs = ipfsClient ( addr , { protocol } )
39+ expectConfig ( ipfs , { host, port, protocol } )
40+ } )
41+
42+ it ( 'mutliaddr ipv4 string' , ( ) => {
43+ const host = '101.101.101.101'
44+ const port = '1001'
45+ const addr = `/ip4/${ host } /tcp/${ port } `
46+ const ipfs = ipfsClient ( addr )
47+ expectConfig ( ipfs , { host, port } )
48+ } )
49+
50+ it ( 'mutliaddr instance' , ( ) => {
51+ const host = 'ace.place'
52+ const port = '1001'
53+ const addr = multiaddr ( `/dns4/${ host } /tcp/${ port } ` )
54+ const ipfs = ipfsClient ( addr )
55+ expectConfig ( ipfs , { host, port } )
56+ } )
57+
58+ it ( 'host and port strings' , ( ) => {
59+ const host = '1.1.1.1'
60+ const port = '9999'
61+ const ipfs = ipfsClient ( host , port )
62+ expectConfig ( ipfs , { host, port } )
63+ } )
64+
65+ it ( 'host, port and api path' , ( ) => {
66+ const host = '10.100.100.255'
67+ const port = '9999'
68+ const apiPath = '/future/api/v1/'
69+ const ipfs = ipfsClient ( host , port , { 'api-path' : apiPath } )
70+ expectConfig ( ipfs , { host, port, apiPath } )
71+ } )
72+
73+ it ( 'throws on invalid mutliaddr' , ( ) => {
74+ expect ( ( ) => ipfsClient ( '/dns4' ) ) . to . throw ( 'invalid address' )
75+ expect ( ( ) => ipfsClient ( '/hello' ) ) . to . throw ( 'no protocol with name' )
76+ expect ( ( ) => ipfsClient ( '/dns4/ipfs.io' ) ) . to . throw ( 'multiaddr must have a valid format' )
77+ } )
1978 } )
20- }
2179
22- describe ( 'ipfs-http-client constructor tests' , ( ) => {
23- describe ( 'parameter permuations' , ( ) => {
80+ describe ( 'integration' , ( ) => {
2481 let apiAddr
2582 let ipfsd
2683
@@ -40,39 +97,26 @@ describe('ipfs-http-client constructor tests', () => {
4097 ipfsd . stop ( done )
4198 } )
4299
43- it ( 'opts' , ( done ) => {
44- const splitted = apiAddr . split ( '/' )
45- clientWorks ( ipfsClient ( {
46- host : splitted [ 2 ] ,
47- port : splitted [ 4 ] ,
48- protocol : 'http'
49- } ) , done )
50- } )
51-
52- it ( 'mutliaddr, opts' , ( done ) => {
53- clientWorks ( ipfsClient ( apiAddr , { protocol : 'http' } ) , done )
54- } )
55-
56- it ( 'host, port' , ( done ) => {
57- const splitted = apiAddr . split ( '/' )
58-
59- clientWorks ( ipfsClient ( splitted [ 2 ] , splitted [ 4 ] ) , done )
60- } )
61-
62- it ( 'specify host, port and api path' , ( done ) => {
63- const splitted = apiAddr . split ( '/' )
64-
65- clientWorks ( ipfsClient ( {
66- host : splitted [ 2 ] ,
67- port : splitted [ 4 ] ,
68- 'api-path' : '/api/v0/'
69- } ) , done )
100+ it ( 'can connect to an ipfs http api' , ( done ) => {
101+ clientWorks ( ipfsClient ( apiAddr ) , done )
70102 } )
103+ } )
104+ } )
71105
72- it ( 'host, port, opts' , ( done ) => {
73- const splitted = apiAddr . split ( '/' )
106+ function clientWorks ( client , done ) {
107+ client . id ( ( err , id ) => {
108+ expect ( err ) . to . not . exist ( )
74109
75- clientWorks ( ipfsClient ( splitted [ 2 ] , splitted [ 4 ] , { protocol : 'http' } ) , done )
76- } )
110+ expect ( id ) . to . have . a . property ( 'id' )
111+ expect ( id ) . to . have . a . property ( 'publicKey' )
112+ done ( )
77113 } )
78- } )
114+ }
115+
116+ function expectConfig ( ipfs , { host, port, protocol, apiPath } ) {
117+ const conf = ipfs . util . getEndpointConfig ( )
118+ expect ( conf . host ) . to . equal ( host || 'localhost' )
119+ expect ( conf . port ) . to . equal ( port || '5001' )
120+ expect ( conf . protocol ) . to . equal ( protocol || 'http' )
121+ expect ( conf [ 'api-path' ] ) . to . equal ( apiPath || '/api/v0/' )
122+ }
0 commit comments