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 : {
@@ -47,16 +64,32 @@ const options = {
4764 echo . polka . get ( '/routing/v1/providers/:cid' , ( req , res ) => {
4865 callCount ++
4966 try {
50- const providerData = providers . get ( req . params . cid ) || { Providers : [ ] }
67+ const providerData = providers . get ( req . params . cid )
68+
69+ // Support testing 404 responses for backward compatibility
70+ if ( req . params . cid === TEST_CIDS . PROVIDERS_404 ) {
71+ res . statusCode = 404
72+ res . end ( 'Not Found' )
73+ return
74+ }
75+
76+ // Support testing null Providers field
77+ if ( req . params . cid === TEST_CIDS . PROVIDERS_NULL ) {
78+ res . setHeader ( 'Content-Type' , 'application/json' )
79+ res . end ( JSON . stringify ( { Providers : null } ) )
80+ return
81+ }
82+
5183 const acceptHeader = req . headers . accept
84+ const data = providerData || { Providers : [ ] }
5285
5386 if ( acceptHeader ?. includes ( 'application/x-ndjson' ) ) {
5487 res . setHeader ( 'Content-Type' , 'application/x-ndjson' )
55- const providers = Array . isArray ( providerData . Providers ) ? providerData . Providers : [ ]
88+ const providers = Array . isArray ( data . Providers ) ? data . Providers : [ ]
5689 res . end ( providers . map ( p => JSON . stringify ( p ) ) . join ( '\n' ) )
5790 } else {
5891 res . setHeader ( 'Content-Type' , 'application/json' )
59- res . end ( JSON . stringify ( providerData ) )
92+ res . end ( JSON . stringify ( data ) )
6093 }
6194 } catch ( err ) {
6295 console . error ( 'Error in get providers:' , err )
@@ -71,10 +104,36 @@ const options = {
71104 } )
72105 echo . polka . get ( '/routing/v1/peers/:peerId' , ( req , res ) => {
73106 callCount ++
74- const records = peers . get ( req . params . peerId ) ?? '[]'
75- peers . delete ( req . params . peerId )
76107
77- res . end ( records )
108+ // Support testing 404 responses for backward compatibility
109+ if ( req . params . peerId === TEST_CIDS . PEERS_404 ) {
110+ res . statusCode = 404
111+ res . end ( 'Not Found' )
112+ return
113+ }
114+
115+ // Support testing null Peers field
116+ if ( req . params . peerId === TEST_CIDS . PEERS_NULL ) {
117+ res . setHeader ( 'Content-Type' , 'application/json' )
118+ res . end ( JSON . stringify ( { Peers : null } ) )
119+ return
120+ }
121+
122+ const records = peers . get ( req . params . peerId )
123+ if ( records ) {
124+ peers . delete ( req . params . peerId )
125+ res . end ( records )
126+ } else {
127+ // Return empty JSON response
128+ const acceptHeader = req . headers . accept
129+ if ( acceptHeader ?. includes ( 'application/x-ndjson' ) ) {
130+ res . setHeader ( 'Content-Type' , 'application/x-ndjson' )
131+ res . end ( '' )
132+ } else {
133+ res . setHeader ( 'Content-Type' , 'application/json' )
134+ res . end ( JSON . stringify ( { Peers : [ ] } ) )
135+ }
136+ }
78137 } )
79138 echo . polka . post ( '/add-ipns/:peerId' , ( req , res ) => {
80139 callCount ++
@@ -83,10 +142,42 @@ const options = {
83142 } )
84143 echo . polka . get ( '/routing/v1/ipns/:peerId' , ( req , res ) => {
85144 callCount ++
86- const record = ipnsGet . get ( req . params . peerId ) ?? ''
145+ const record = ipnsGet . get ( req . params . peerId )
87146 ipnsGet . delete ( req . params . peerId )
88147
89- res . end ( record )
148+ // Support testing different content-types
149+ if ( req . params . peerId === TEST_CIDS . IPNS_404 ) {
150+ res . statusCode = 404
151+ res . end ( 'Not Found' )
152+ return
153+ }
154+
155+ if ( req . params . peerId === TEST_CIDS . IPNS_JSON ) {
156+ res . setHeader ( 'Content-Type' , 'application/json' )
157+ res . end ( JSON . stringify ( { error : 'not found' } ) )
158+ return
159+ }
160+
161+ if ( req . params . peerId === TEST_CIDS . IPNS_HTML ) {
162+ res . setHeader ( 'Content-Type' , 'text/html' )
163+ res . end ( '<html>Not Found</html>' )
164+ return
165+ }
166+
167+ if ( req . params . peerId === TEST_CIDS . IPNS_NO_CONTENT_TYPE ) {
168+ // No content-type header
169+ res . end ( 'No record' )
170+ return
171+ }
172+
173+ if ( record ) {
174+ res . setHeader ( 'Content-Type' , 'application/vnd.ipfs.ipns-record' )
175+ res . end ( record )
176+ } else {
177+ // Per IPIP-0513: Return 200 with text/plain for no record found
178+ res . setHeader ( 'Content-Type' , 'text/plain; charset=utf-8' )
179+ res . end ( 'Record not found' )
180+ }
90181 } )
91182 echo . polka . put ( '/routing/v1/ipns/:peerId' , ( req , res ) => {
92183 callCount ++
0 commit comments