11import EchoServer from 'aegir/echo-server'
22import body from 'body-parser'
33
4+ // Special test CIDs that trigger specific fixtures
5+ const TEST_CIDS = {
6+ // Providers endpoint test CIDs
7+ PROVIDERS_404 : 'bafkreig3o4e7r4bpsc3hqirlzjeuie3w25tfjgmp6ufeaabwvuial3r4h4' , // return404providers
8+ PROVIDERS_NULL : 'bafkreicyicgkpqid2qs3kfc277f4tsx5tew3e63fgv7fn6t74sicjkv76i' , // returnnullproviders
9+
10+ // Peers endpoint test CIDs (libp2p-key format)
11+ PEERS_404 : 'k2k4r8pqu6ui9p0d0fewul7462tsb0pa57pi238gunrjxpfrg6zawrho' , // return404peers
12+ PEERS_NULL : 'k2k4r8nyb48mv6n6olsob1zsz77mhdrvwtjcryjil2qqqzye5jds4uur' , // returnnullpeers
13+
14+ // IPNS endpoint test CIDs (libp2p-key format)
15+ IPNS_404 : 'k2k4r8o3937xct4wma8gooitiip4mik0phkg8kt3b5x9y93a9dntvwjz' , // return404ipns
16+ IPNS_JSON : 'k2k4r8pajj9txni0h9nv9gxuj1mju4jmi94iq2r4jwhxk87hnuo94yom' , // returnjsonipns
17+ IPNS_HTML : 'k2k4r8kddkyieizgq7a32d9jc4nm99yupniet962vssrm34hamolquzk' , // returnhtmlipns
18+ IPNS_NO_CONTENT_TYPE : 'k2k4r8okqrya8gr449btdy5b6vw0q68dh7y3fps9qbi0zmcmybz7bjpu' // returnnocontentipns
19+ }
20+
421/** @type {import('aegir').PartialOptions } */
522const options = {
623 test : {
@@ -45,8 +62,24 @@ const options = {
4562 echo . polka . get ( '/routing/v1/providers/:cid' , ( req , res ) => {
4663 callCount ++
4764 try {
48- const providerData = providers . get ( req . params . cid ) || { Providers : [ ] }
65+ const providerData = providers . get ( req . params . cid )
66+
67+ // Support testing 404 responses for backward compatibility
68+ if ( req . params . cid === TEST_CIDS . PROVIDERS_404 ) {
69+ res . statusCode = 404
70+ res . end ( 'Not Found' )
71+ return
72+ }
73+
74+ // Support testing null Providers field
75+ if ( req . params . cid === TEST_CIDS . PROVIDERS_NULL ) {
76+ res . setHeader ( 'Content-Type' , 'application/json' )
77+ res . end ( JSON . stringify ( { Providers : null } ) )
78+ return
79+ }
80+
4981 const acceptHeader = req . headers . accept
82+ const data = providerData || { Providers : [ ] }
5083
5184 if ( providerData ?. Providers ?. length === 0 ) {
5285 res . statusCode = 404
@@ -56,11 +89,11 @@ const options = {
5689
5790 if ( acceptHeader ?. includes ( 'application/x-ndjson' ) ) {
5891 res . setHeader ( 'Content-Type' , 'application/x-ndjson' )
59- const providers = Array . isArray ( providerData . Providers ) ? providerData . Providers : [ ]
92+ const providers = Array . isArray ( data . Providers ) ? data . Providers : [ ]
6093 res . end ( providers . map ( p => JSON . stringify ( p ) ) . join ( '\n' ) )
6194 } else {
6295 res . setHeader ( 'Content-Type' , 'application/json' )
63- res . end ( JSON . stringify ( providerData ) )
96+ res . end ( JSON . stringify ( data ) )
6497 }
6598 } catch ( err ) {
6699 console . error ( 'Error in get providers:' , err )
@@ -75,10 +108,36 @@ const options = {
75108 } )
76109 echo . polka . get ( '/routing/v1/peers/:peerId' , ( req , res ) => {
77110 callCount ++
78- const records = peers . get ( req . params . peerId ) ?? '[]'
79- peers . delete ( req . params . peerId )
80111
81- res . end ( records )
112+ // Support testing 404 responses for backward compatibility
113+ if ( req . params . peerId === TEST_CIDS . PEERS_404 ) {
114+ res . statusCode = 404
115+ res . end ( 'Not Found' )
116+ return
117+ }
118+
119+ // Support testing null Peers field
120+ if ( req . params . peerId === TEST_CIDS . PEERS_NULL ) {
121+ res . setHeader ( 'Content-Type' , 'application/json' )
122+ res . end ( JSON . stringify ( { Peers : null } ) )
123+ return
124+ }
125+
126+ const records = peers . get ( req . params . peerId )
127+ if ( records ) {
128+ peers . delete ( req . params . peerId )
129+ res . end ( records )
130+ } else {
131+ // Return empty JSON response
132+ const acceptHeader = req . headers . accept
133+ if ( acceptHeader ?. includes ( 'application/x-ndjson' ) ) {
134+ res . setHeader ( 'Content-Type' , 'application/x-ndjson' )
135+ res . end ( '' )
136+ } else {
137+ res . setHeader ( 'Content-Type' , 'application/json' )
138+ res . end ( JSON . stringify ( { Peers : [ ] } ) )
139+ }
140+ }
82141 } )
83142 echo . polka . post ( '/add-ipns/:peerId' , ( req , res ) => {
84143 callCount ++
@@ -87,10 +146,42 @@ const options = {
87146 } )
88147 echo . polka . get ( '/routing/v1/ipns/:peerId' , ( req , res ) => {
89148 callCount ++
90- const record = ipnsGet . get ( req . params . peerId ) ?? ''
149+ const record = ipnsGet . get ( req . params . peerId )
91150 ipnsGet . delete ( req . params . peerId )
92151
93- res . end ( record )
152+ // Support testing different content-types
153+ if ( req . params . peerId === TEST_CIDS . IPNS_404 ) {
154+ res . statusCode = 404
155+ res . end ( 'Not Found' )
156+ return
157+ }
158+
159+ if ( req . params . peerId === TEST_CIDS . IPNS_JSON ) {
160+ res . setHeader ( 'Content-Type' , 'application/json' )
161+ res . end ( JSON . stringify ( { error : 'not found' } ) )
162+ return
163+ }
164+
165+ if ( req . params . peerId === TEST_CIDS . IPNS_HTML ) {
166+ res . setHeader ( 'Content-Type' , 'text/html' )
167+ res . end ( '<html>Not Found</html>' )
168+ return
169+ }
170+
171+ if ( req . params . peerId === TEST_CIDS . IPNS_NO_CONTENT_TYPE ) {
172+ // No content-type header
173+ res . end ( 'No record' )
174+ return
175+ }
176+
177+ if ( record ) {
178+ res . setHeader ( 'Content-Type' , 'application/vnd.ipfs.ipns-record' )
179+ res . end ( record )
180+ } else {
181+ // Per IPIP-0513: Return 200 with text/plain for no record found
182+ res . setHeader ( 'Content-Type' , 'text/plain; charset=utf-8' )
183+ res . end ( 'Record not found' )
184+ }
94185 } )
95186 echo . polka . put ( '/routing/v1/ipns/:peerId' , ( req , res ) => {
96187 callCount ++
0 commit comments