Skip to content

Commit 29ccab6

Browse files
committed
modified-contentType&-added-testcases
1 parent 6ce93f2 commit 29ccab6

File tree

5 files changed

+991
-591
lines changed

5 files changed

+991
-591
lines changed

packages/client/.aegir.js

Lines changed: 138 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,157 @@
1-
import EchoServer from 'aegir/echo-server'
2-
import body from 'body-parser'
1+
import EchoServer from "aegir/echo-server";
2+
import body from "body-parser";
33

44
/** @type {import('aegir').PartialOptions} */
55
const options = {
66
test: {
77
before: async () => {
8-
let callCount = 0
9-
let lastCalledUrl = ''
10-
const providers = new Map()
11-
const peers = new Map()
12-
const ipnsGet = new Map()
13-
const ipnsPut = new Map()
14-
const echo = new EchoServer()
15-
echo.polka.use(body.raw({ type: 'application/vnd.ipfs.ipns-record'}))
16-
echo.polka.use(body.text())
8+
let callCount = 0;
9+
let lastCalledUrl = "";
10+
const providers = new Map();
11+
const peers = new Map();
12+
const ipnsGet = new Map();
13+
const ipnsPut = new Map();
14+
const echo = new EchoServer();
15+
echo.polka.use(body.raw({ type: "application/vnd.ipfs.ipns-record" }));
16+
echo.polka.use(body.text());
17+
echo.polka.use(body.json());
1718
echo.polka.use((req, res, next) => {
18-
next()
19-
lastCalledUrl = req.url
20-
})
21-
echo.polka.post('/add-providers/:cid', (req, res) => {
22-
callCount++
23-
providers.set(req.params.cid, req.body)
24-
res.end()
25-
})
26-
echo.polka.get('/routing/v1/providers/:cid', (req, res) => {
27-
callCount++
28-
const records = providers.get(req.params.cid) ?? '[]'
29-
providers.delete(req.params.cid)
30-
res.end(records)
31-
})
32-
echo.polka.post('/add-peers/:peerId', (req, res) => {
33-
callCount++
34-
peers.set(req.params.peerId, req.body)
35-
res.end()
36-
})
37-
echo.polka.get('/routing/v1/peers/:peerId', (req, res) => {
38-
callCount++
39-
const records = peers.get(req.params.peerId) ?? '[]'
40-
peers.delete(req.params.peerId)
19+
next();
20+
lastCalledUrl = req.url;
21+
});
4122

42-
res.end(records)
43-
})
44-
echo.polka.post('/add-ipns/:peerId', (req, res) => {
45-
callCount++
46-
ipnsGet.set(req.params.peerId, req.body)
47-
res.end()
48-
})
49-
echo.polka.get('/routing/v1/ipns/:peerId', (req, res) => {
50-
callCount++
51-
const record = ipnsGet.get(req.params.peerId) ?? ''
52-
ipnsGet.delete(req.params.peerId)
23+
echo.polka.post("/add-providers/:cid", (req, res) => {
24+
callCount++;
25+
try {
26+
console.log("Received POST request body:", req.body);
27+
console.log("Content-Type:", req.headers["content-type"]);
5328

54-
res.end(record)
55-
})
56-
echo.polka.put('/routing/v1/ipns/:peerId', (req, res) => {
57-
callCount++
58-
ipnsPut.set(req.params.peerId, req.body)
59-
res.end()
60-
})
61-
echo.polka.get('/get-ipns/:peerId', (req, res) => {
62-
callCount++
63-
const record = ipnsPut.get(req.params.peerId) ?? ''
64-
ipnsPut.delete(req.params.peerId)
29+
// Only process if content-type is application/json
30+
if (req.headers["content-type"]?.includes("application/json")) {
31+
const data =
32+
typeof req.body === "string"
33+
? {
34+
Providers: req.body
35+
.split("\n")
36+
.map((line) => JSON.parse(line)),
37+
}
38+
: req.body;
39+
providers.set(req.params.cid, data);
40+
res.end(JSON.stringify({ success: true }));
41+
} else {
42+
res.statusCode = 400;
43+
res.end(
44+
JSON.stringify({
45+
error: "Invalid content type. Expected application/json",
46+
code: "ERR_INVALID_INPUT",
47+
})
48+
);
49+
// Clear any existing providers for this CID
50+
providers.delete(req.params.cid);
51+
}
52+
} catch (err) {
53+
console.error("Error in add-providers:", err);
54+
res.statusCode = 400;
55+
res.end(
56+
JSON.stringify({
57+
error: err.message,
58+
code: "ERR_INVALID_INPUT",
59+
})
60+
);
61+
providers.delete(req.params.cid);
62+
}
63+
});
6564

66-
res.end(record)
67-
})
68-
echo.polka.get('/get-call-count', (req, res) => {
69-
res.end(callCount.toString())
70-
})
71-
echo.polka.get('/reset-call-count', (req, res) => {
72-
callCount = 0
73-
res.end()
74-
})
75-
echo.polka.get('/last-called-url', (req, res) => {
76-
res.end(lastCalledUrl)
77-
})
65+
echo.polka.get("/routing/v1/providers/:cid", (req, res) => {
66+
callCount++;
67+
try {
68+
console.log("GET request for CID:", req.params.cid);
69+
console.log("Accept header:", req.headers.accept);
7870

79-
await echo.start()
71+
const providerData = providers.get(req.params.cid) || {
72+
Providers: [],
73+
};
74+
75+
const acceptHeader = req.headers.accept;
76+
if (acceptHeader?.includes("application/x-ndjson")) {
77+
res.setHeader("Content-Type", "application/x-ndjson");
78+
const providers = Array.isArray(providerData.Providers)
79+
? providerData.Providers
80+
: [];
81+
res.end(providers.map((p) => JSON.stringify(p)).join("\n"));
82+
} else {
83+
res.setHeader("Content-Type", "application/json");
84+
res.end(JSON.stringify(providerData));
85+
}
86+
} catch (err) {
87+
console.error("Error in get providers:", err);
88+
res.statusCode = 500;
89+
res.end(JSON.stringify({ error: err.message }));
90+
}
91+
});
92+
93+
echo.polka.post("/add-peers/:peerId", (req, res) => {
94+
callCount++;
95+
peers.set(req.params.peerId, req.body);
96+
res.end();
97+
});
98+
echo.polka.get("/routing/v1/peers/:peerId", (req, res) => {
99+
callCount++;
100+
const records = peers.get(req.params.peerId) ?? "[]";
101+
peers.delete(req.params.peerId);
102+
103+
res.end(records);
104+
});
105+
echo.polka.post("/add-ipns/:peerId", (req, res) => {
106+
callCount++;
107+
ipnsGet.set(req.params.peerId, req.body);
108+
res.end();
109+
});
110+
echo.polka.get("/routing/v1/ipns/:peerId", (req, res) => {
111+
callCount++;
112+
const record = ipnsGet.get(req.params.peerId) ?? "";
113+
ipnsGet.delete(req.params.peerId);
114+
115+
res.end(record);
116+
});
117+
echo.polka.put("/routing/v1/ipns/:peerId", (req, res) => {
118+
callCount++;
119+
ipnsPut.set(req.params.peerId, req.body);
120+
res.end();
121+
});
122+
echo.polka.get("/get-ipns/:peerId", (req, res) => {
123+
callCount++;
124+
const record = ipnsPut.get(req.params.peerId) ?? "";
125+
ipnsPut.delete(req.params.peerId);
126+
127+
res.end(record);
128+
});
129+
echo.polka.get("/get-call-count", (req, res) => {
130+
res.end(callCount.toString());
131+
});
132+
echo.polka.get("/reset-call-count", (req, res) => {
133+
callCount = 0;
134+
res.end();
135+
});
136+
echo.polka.get("/last-called-url", (req, res) => {
137+
res.end(lastCalledUrl);
138+
});
139+
140+
await echo.start();
80141

81142
return {
82143
env: {
83-
ECHO_SERVER: `http://${echo.host}:${echo.port}`
144+
ECHO_SERVER: `http://${echo.host}:${echo.port}`,
84145
},
85-
echo
86-
}
146+
echo,
147+
};
87148
},
88149
after: async (_, beforeResult) => {
89150
if (beforeResult.echo != null) {
90-
await beforeResult.echo.stop()
151+
await beforeResult.echo.stop();
91152
}
92-
}
93-
}
94-
}
153+
},
154+
},
155+
};
95156

96-
export default options
157+
export default options;

packages/client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
"@libp2p/logger": "^5.0.1",
143143
"@libp2p/peer-id": "^5.0.1",
144144
"@multiformats/multiaddr": "^12.3.1",
145+
"@playwright/test": "^1.50.1",
145146
"any-signal": "^4.1.1",
146147
"browser-readablestream-to-it": "^2.0.7",
147148
"ipns": "^10.0.0",

packages/client/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
139139
}
140140

141141
const contentType = res.headers.get('Content-Type')
142-
if (contentType === 'application/json') {
142+
if (contentType?.startsWith('application/json')) {
143143
const body = await res.json()
144144

145145
for (const provider of body.Providers) {

0 commit comments

Comments
 (0)