Skip to content

Commit e80d82a

Browse files
committed
refactor: improve error handling and streamline node configuration in HTTP server tests
1 parent 5c3ebc1 commit e80d82a

File tree

1 file changed

+31
-66
lines changed

1 file changed

+31
-66
lines changed

packages/protocol-http/test/006-http-server.spec.ts

+31-66
Original file line numberDiff line numberDiff line change
@@ -3,93 +3,58 @@
33

44
import { noise } from '@chainsafe/libp2p-noise'
55
import { yamux } from '@chainsafe/libp2p-yamux'
6-
import { circuitRelayTransport } from '@libp2p/circuit-relay-v2'
76
import { identify } from '@libp2p/identify'
8-
import { tcp } from '@libp2p/tcp'
9-
import { webRTC } from '@libp2p/webrtc'
107
import { expect } from 'aegir/chai'
118
import { createLibp2p } from 'libp2p'
129
import { createSandbox } from 'sinon'
1310
import { HttpServerFactory } from '../src/http-server-factory.js'
1411
import { Router } from '../src/router.js'
12+
import { getNodeConfig } from './common/node-config.js'
1513
import type { http } from '../src/http-proto-api.js'
1614
import type { HttpServerInterface } from '../src/interfaces/http-server-interface.js'
1715
import type { RequestHandler } from '../src/interfaces/request-handler-interface.js'
1816
import type { Libp2p, ServiceMap } from '@libp2p/interface'
1917

20-
// Configure node based on environment
21-
interface NodeConfig {
22-
addresses: {
23-
listen: string[]
24-
}
25-
transports: any[]
26-
connectionGater?: {
27-
denyDialMultiaddr(): Promise<boolean>
28-
}
29-
}
30-
31-
const getNodeConfig = (): NodeConfig => {
32-
if (typeof window === 'undefined') {
33-
// Node.js configuration
34-
return {
35-
addresses: {
36-
listen: ['/ip4/127.0.0.1/tcp/0']
37-
},
38-
transports: [tcp()]
39-
}
40-
}
41-
42-
// Browser configuration
43-
return {
44-
addresses: {
45-
listen: ['/webrtc']
46-
},
47-
transports: [
48-
webRTC({
49-
rtcConfiguration: {
50-
iceServers: [{ urls: ['stun:stun.l.google.com:19302'] }]
51-
}
52-
}),
53-
circuitRelayTransport()
54-
],
55-
connectionGater: {
56-
denyDialMultiaddr: async () => false
57-
}
58-
}
59-
}
60-
6118
describe('006-HTTP Server Implementation', () => {
6219
const sandbox = createSandbox()
6320
let node: Libp2p<ServiceMap>
6421
let server: HttpServerInterface
6522

6623
beforeEach(async () => {
67-
// Create a libp2p node with appropriate configuration
68-
const config = getNodeConfig()
69-
node = await createLibp2p({
70-
...config,
71-
streamMuxers: [yamux()],
72-
connectionEncrypters: [noise()],
73-
services: {
74-
identify: identify()
75-
}
76-
})
77-
78-
// Create an HTTP server using real components from the node
79-
const createServer = HttpServerFactory.createServer()
80-
server = createServer({
81-
logger: node.logger,
82-
connectionManager: (node as any).components.connectionManager,
83-
registrar: (node as any).components.registrar
84-
})
85-
86-
// Start the node
87-
await node.start()
24+
try {
25+
// Use the existing node config utility which properly handles different environments
26+
const nodeConfig = getNodeConfig()
27+
28+
node = await createLibp2p({
29+
...nodeConfig,
30+
streamMuxers: [yamux()],
31+
connectionEncrypters: [noise()],
32+
services: {
33+
identify: identify()
34+
}
35+
})
36+
37+
// Create an HTTP server using real components from the node
38+
const createServer = HttpServerFactory.createServer()
39+
server = createServer({
40+
logger: node.logger,
41+
connectionManager: (node as any).components.connectionManager,
42+
registrar: (node as any).components.registrar
43+
})
44+
45+
// Start the node
46+
await node.start()
47+
} catch (err) {
48+
console.error('Error in beforeEach:', err)
49+
throw err
50+
}
8851
})
8952

9053
afterEach(async () => {
9154
sandbox.restore()
92-
await node.stop()
55+
if (node != null) {
56+
await node.stop()
57+
}
9358
})
9459

9560
it('should create server instance', () => {

0 commit comments

Comments
 (0)