1
1
/* eslint-env mocha */
2
2
'use strict'
3
3
4
+ const multiaddr = require ( 'multiaddr' )
4
5
const chai = require ( 'chai' )
5
6
const dirtyChai = require ( 'dirty-chai' )
6
7
const expect = chai . expect
@@ -9,18 +10,73 @@ chai.use(dirtyChai)
9
10
const f = require ( './utils/factory' )
10
11
const ipfsClient = require ( '../src/index.js' )
11
12
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 host = 'localhost'
17
+ const port = '5001'
18
+ const protocol = 'http'
19
+ const ipfs = ipfsClient ( )
20
+ expectConfig ( ipfs , { host, port, protocol } )
21
+ } )
15
22
16
- expect ( id ) . to . have . a . property ( 'id' )
17
- expect ( id ) . to . have . a . property ( 'publicKey' )
18
- done ( )
23
+ it ( 'opts' , ( ) => {
24
+ const host = 'wizard.world'
25
+ const port = '999'
26
+ const protocol = 'https'
27
+ const ipfs = ipfsClient ( { host, port, protocol } )
28
+ expectConfig ( ipfs , { host, port } )
29
+ } )
30
+
31
+ it ( 'mutliaddr dns4 string, opts' , ( ) => {
32
+ const host = 'foo.com'
33
+ const port = '1001'
34
+ const protocol = 'https'
35
+ const addr = `/dns4/${ host } /tcp/${ port } `
36
+ const ipfs = ipfsClient ( addr , { protocol } )
37
+ expectConfig ( ipfs , { host, port, protocol } )
38
+ } )
39
+
40
+ it ( 'mutliaddr ipv4 string' , ( ) => {
41
+ const host = '101.101.101.101'
42
+ const port = '1001'
43
+ const addr = `/ip4/${ host } /tcp/${ port } `
44
+ const ipfs = ipfsClient ( addr )
45
+ expectConfig ( ipfs , { host, port } )
46
+ } )
47
+
48
+ it ( 'mutliaddr instance' , ( ) => {
49
+ const host = 'ace.place'
50
+ const port = '1001'
51
+ const addr = multiaddr ( `/dns4/${ host } /tcp/${ port } ` )
52
+ const ipfs = ipfsClient ( addr )
53
+ expectConfig ( ipfs , { host, port } )
54
+ } )
55
+
56
+ it ( 'host and port strings' , ( ) => {
57
+ const host = '1.1.1.1'
58
+ const port = '9999'
59
+ const ipfs = ipfsClient ( host , port )
60
+ expectConfig ( ipfs , { host, port } )
61
+ } )
62
+
63
+ // TOOD: need to add `api-path` to `getEndpointConfig()` so we can test that setting a non-default value works
64
+ it . skip ( 'host, port and api path' , ( ) => {
65
+ const host = '10.100.100.255'
66
+ const port = '9999'
67
+ const apiPath = '/future/api/v1'
68
+ const ipfs = ipfsClient ( host , port , { 'api-path' : apiPath } )
69
+ expectConfig ( ipfs , { host, port, apiPath } )
70
+ } )
71
+
72
+ it ( 'throws on invalid mutliaddr' , ( ) => {
73
+ expect ( ( ) => ipfsClient ( '/dns4' ) ) . to . throw ( 'invalid address' )
74
+ expect ( ( ) => ipfsClient ( '/hello' ) ) . to . throw ( 'no protocol with name' )
75
+ expect ( ( ) => ipfsClient ( '/dns4/ipfs.io' ) ) . to . throw ( 'multiaddr must have a valid format' )
76
+ } )
19
77
} )
20
- }
21
78
22
- describe ( 'ipfs-http-client constructor tests' , ( ) => {
23
- describe ( 'parameter permuations' , ( ) => {
79
+ describe ( 'integration' , ( ) => {
24
80
let apiAddr
25
81
let ipfsd
26
82
@@ -40,45 +96,31 @@ describe('ipfs-http-client constructor tests', () => {
40
96
ipfsd . stop ( done )
41
97
} )
42
98
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 )
99
+ it ( 'can connect to an ipfs http api' , ( done ) => {
100
+ clientWorks ( ipfsClient ( apiAddr ) , done )
70
101
} )
102
+ } )
103
+ } )
71
104
72
- it ( 'host, port, opts' , ( done ) => {
73
- const splitted = apiAddr . split ( '/' )
74
-
75
- clientWorks ( ipfsClient ( splitted [ 2 ] , splitted [ 4 ] , { protocol : 'http' } ) , done )
76
- } )
105
+ function clientWorks ( client , done ) {
106
+ client . id ( ( err , id ) => {
107
+ expect ( err ) . to . not . exist ( )
77
108
78
- it ( 'error in invalid mutliaddr' , ( ) => {
79
- expect ( ( ) => ipfsClient ( '/dns4' ) ) . to . throw ( 'invalid address' )
80
- expect ( ( ) => ipfsClient ( '/hello' ) ) . to . throw ( 'no protocol with name' )
81
- expect ( ( ) => ipfsClient ( '/dns4/ipfs.io' ) ) . to . throw ( 'multiaddr must have a valid format' )
82
- } )
109
+ expect ( id ) . to . have . a . property ( 'id' )
110
+ expect ( id ) . to . have . a . property ( 'publicKey' )
111
+ done ( )
83
112
} )
84
- } )
113
+ }
114
+
115
+ function expectConfig ( ipfs , { host, port, protocol, apiPath} ) {
116
+ const conf = ipfs . util . getEndpointConfig ( )
117
+ expect ( conf . host ) . to . equal ( host )
118
+ expect ( conf . port ) . to . equal ( port )
119
+ // TODO: needs https://github.com/ipfs/js-ipfs-http-client/pull/935
120
+ if ( protocol ) {
121
+ expect ( conf . protocol ) . to . equal ( protocol )
122
+ }
123
+ if ( apiPath ) {
124
+ expect ( conf [ 'api-path' ] ) . to . equal ( apiPath )
125
+ }
126
+ }
0 commit comments