@@ -6,12 +6,14 @@ import { ipniContentRouting } from '../src/index.js'
66import { CID } from 'multiformats/cid'
77import all from 'it-all'
88
9+ if ( process . env . ECHO_SERVER == null ) {
10+ throw new Error ( 'Echo server not configured correctly' )
11+ }
12+
13+ const serverUrl = process . env . ECHO_SERVER
14+
915describe ( 'IPNIContentRouting' , function ( ) {
1016 it ( 'should find providers' , async ( ) => {
11- if ( process . env . ECHO_SERVER == null ) {
12- throw new Error ( 'Echo server not configured correctly' )
13- }
14-
1517 const providers = [ {
1618 Metadata : 'gBI=' ,
1719 ContextID : '' ,
@@ -36,7 +38,7 @@ describe('IPNIContentRouting', function () {
3638 body : providers . map ( prov => JSON . stringify ( prov ) ) . join ( '\n' )
3739 } )
3840
39- const routing = ipniContentRouting ( new URL ( process . env . ECHO_SERVER ) ) ( )
41+ const routing = ipniContentRouting ( serverUrl ) ( )
4042
4143 const provs = await all ( routing . findProviders ( cid ) )
4244 expect ( provs . map ( prov => ( {
@@ -47,4 +49,55 @@ describe('IPNIContentRouting', function () {
4749 addrs : prov . Provider . Addrs
4850 } ) ) )
4951 } )
52+
53+ it ( 'should handle non-json input' , async ( ) => {
54+ const cid = CID . parse ( 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' )
55+
56+ // load providers for the router to fetch
57+ await fetch ( `${ process . env . ECHO_SERVER } /add-providers/${ cid . toString ( ) } ` , {
58+ method : 'POST' ,
59+ body : 'not json'
60+ } )
61+
62+ const routing = ipniContentRouting ( serverUrl ) ( )
63+
64+ const provs = await all ( routing . findProviders ( cid ) )
65+ expect ( provs ) . to . be . empty ( )
66+ } )
67+
68+ it ( 'should handle bad input providers' , async ( ) => {
69+ const providers = [ {
70+ Metadata : 'gBI=' ,
71+ Provider : {
72+ Bad : 'field'
73+ }
74+ } , {
75+ Metadata : 'gBI=' ,
76+ ContextID : '' ,
77+ Another : {
78+ Bad : 'field'
79+ }
80+ } ]
81+
82+ const cid = CID . parse ( 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' )
83+
84+ // load providers for the router to fetch
85+ await fetch ( `${ process . env . ECHO_SERVER } /add-providers/${ cid . toString ( ) } ` , {
86+ method : 'POST' ,
87+ body : providers . map ( prov => JSON . stringify ( prov ) ) . join ( '\n' )
88+ } )
89+
90+ const routing = ipniContentRouting ( serverUrl ) ( )
91+
92+ const provs = await all ( routing . findProviders ( cid ) )
93+ expect ( provs ) . to . be . empty ( )
94+ } )
95+
96+ it ( 'should handle empty input' , async ( ) => {
97+ const cid = CID . parse ( 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' )
98+ const routing = ipniContentRouting ( serverUrl ) ( )
99+
100+ const provs = await all ( routing . findProviders ( cid ) )
101+ expect ( provs ) . to . be . empty ( )
102+ } )
50103} )
0 commit comments