Skip to content

Commit b67028b

Browse files
committed
Fixed a issue with GET and DELETE requests body over http
The request body wasn't present on the server side when executing the request. This issue is now resolved with this patch.
1 parent 1e6f12d commit b67028b

File tree

8 files changed

+130
-87
lines changed

8 files changed

+130
-87
lines changed

lib/createExpressRequest.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ import stripUrlSlashes from "./stripSlashes";
1313
export function createExpressRequest(req: ExpressRequest, res: ExpressResponse, method: string, settings: SettingsInterface) {
1414

1515
const name = stripUrlSlashes(req.params['0']);
16-
let newRequest = new ServerRequest(req.params.id, name, req.body, method, null);
17-
// call the metric start function
18-
settings.on.eventReceived(newRequest);
16+
17+
// get the request body contents
18+
const body = (req.method === "GET" || req.method === "DELETE") && req.query.body ? (req.query.body === "undefined" ? undefined : JSON.parse(req.query.body)) : req.body;
19+
20+
// create the new request
21+
let newRequest = new ServerRequest(req.params.id, name, body, method, null);
22+
// call the metric start function
23+
settings.on.eventReceived(newRequest);
1924

2025
newRequest.request = req;
2126

out/createExpressRequest.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

out/createExpressRequest.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "websocketapi",
3-
"version": "1.1.7",
3+
"version": "1.1.8",
44
"description": "",
55
"main": "out/index.js",
66
"types": "out/index.d.ts",

test/autoTest/public/client.js

Lines changed: 98 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ describe("browser", () => {
6969
it("should throw error with 'Unknown api request.'", function (done) {
7070

7171
WebSocketAPI.fetch("something404", undefined, {
72-
use: "ws"
73-
})
72+
use: "ws"
73+
})
7474
.then(_ => done(new Error("Was supposed to throw a error.")))
7575
.catch(err => {
7676
try {
@@ -88,8 +88,8 @@ describe("browser", () => {
8888
it("should throw error with 'test error'", function (done) {
8989

9090
WebSocketAPI.fetch("error", undefined, {
91-
use: "ws"
92-
})
91+
use: "ws"
92+
})
9393
.then(_ => done(new Error("Was supposed to throw a error.")))
9494
.catch(err => {
9595
try {
@@ -107,9 +107,9 @@ describe("browser", () => {
107107
it("should throw timeout error with 'Timeout Error'", function (done) {
108108

109109
WebSocketAPI.fetch("timeout", undefined, {
110-
use: "ws",
111-
timeout: 150
112-
})
110+
use: "ws",
111+
timeout: 150
112+
})
113113
.then(_ => done(new Error("Was supposed to throw a timeout error.")))
114114
.catch(err => {
115115
try {
@@ -148,8 +148,8 @@ describe("browser", () => {
148148
it("should throw error with 'Unknown api request.'", function (done) {
149149

150150
WebSocketAPI.fetch("something404", undefined, {
151-
use: "http"
152-
})
151+
use: "http"
152+
})
153153
.then(_ => done(new Error("Was supposed to throw a error.")))
154154
.catch(err => {
155155
try {
@@ -167,8 +167,8 @@ describe("browser", () => {
167167
it("should throw error with 'test error'", function (done) {
168168

169169
WebSocketAPI.fetch("error", undefined, {
170-
use: "http"
171-
})
170+
use: "http"
171+
})
172172
.then(_ => done(new Error("Was supposed to throw a error.")))
173173
.catch(err => {
174174
try {
@@ -186,9 +186,9 @@ describe("browser", () => {
186186
it("should throw timeout error with 'Timeout Error'", function (done) {
187187

188188
WebSocketAPI.fetch("timeout", undefined, {
189-
use: "http",
190-
timeout: 150
191-
})
189+
use: "http",
190+
timeout: 150
191+
})
192192
.then(_ => done(new Error("Was supposed to throw a timeout error.")))
193193
.catch(err => {
194194
try {
@@ -203,6 +203,31 @@ describe("browser", () => {
203203
this.timeout(200);
204204
});
205205

206+
// the get request's body should be readable from server and returned as a echo
207+
it("Echo with the request body", async function () {
208+
this.timeout(200);
209+
const response = await WebSocketAPI.fetch("echo", {
210+
a: 56,
211+
b: "c",
212+
d: null,
213+
nested: {
214+
a: 10
215+
}
216+
}, {
217+
use: "http",
218+
timeout: 150
219+
});
220+
221+
// check that the response is equal
222+
assert.deepEqual(response, {
223+
a: 56,
224+
b: "c",
225+
d: null,
226+
nested: {
227+
a: 10
228+
}
229+
});
230+
});
206231
});
207232

208233
});
@@ -238,9 +263,9 @@ describe("browser", () => {
238263
it("should throw error with 'Unknown api request.'", function (done) {
239264

240265
WebSocketAPI.fetch("something404", undefined, {
241-
use: "ws",
242-
method: "POST",
243-
})
266+
use: "ws",
267+
method: "POST",
268+
})
244269
.then(_ => done(new Error("Was supposed to throw a error.")))
245270
.catch(err => {
246271
try {
@@ -258,9 +283,9 @@ describe("browser", () => {
258283
it("should throw error with 'test error'", function (done) {
259284

260285
WebSocketAPI.fetch("error", postData, {
261-
use: "ws",
262-
method: "POST",
263-
})
286+
use: "ws",
287+
method: "POST",
288+
})
264289
.then(_ => done(new Error("Was supposed to throw a error.")))
265290
.catch(err => {
266291
try {
@@ -278,10 +303,10 @@ describe("browser", () => {
278303
it("should throw timeout error with 'Timeout Error'", function (done) {
279304

280305
WebSocketAPI.fetch("timeout", postData, {
281-
use: "ws",
282-
timeout: 150,
283-
method: "POST",
284-
})
306+
use: "ws",
307+
timeout: 150,
308+
method: "POST",
309+
})
285310
.then(_ => done(new Error("Was supposed to throw a timeout error.")))
286311
.catch(err => {
287312
try {
@@ -315,9 +340,9 @@ describe("browser", () => {
315340
it("should throw error with 'Unknown api request.'", function (done) {
316341

317342
WebSocketAPI.fetch("something404", postData, {
318-
use: "http",
319-
method: "POST",
320-
})
343+
use: "http",
344+
method: "POST",
345+
})
321346
.then(_ => done(new Error("Was supposed to throw a error.")))
322347
.catch(err => {
323348
try {
@@ -335,9 +360,9 @@ describe("browser", () => {
335360
it("should throw error with 'test error'", function (done) {
336361

337362
WebSocketAPI.fetch("error", postData, {
338-
use: "http",
339-
method: "POST",
340-
})
363+
use: "http",
364+
method: "POST",
365+
})
341366
.then(_ => done(new Error("Was supposed to throw a error.")))
342367
.catch(err => {
343368
try {
@@ -355,10 +380,10 @@ describe("browser", () => {
355380
it("should throw timeout error with 'Timeout Error'", function (done) {
356381

357382
WebSocketAPI.fetch("timeout", postData, {
358-
use: "http",
359-
timeout: 150,
360-
method: "POST",
361-
})
383+
use: "http",
384+
timeout: 150,
385+
method: "POST",
386+
})
362387
.then(_ => done(new Error("Was supposed to throw a timeout error.")))
363388
.catch(err => {
364389
try {
@@ -408,9 +433,9 @@ describe("browser", () => {
408433
it("should throw error with 'Unknown api request.'", function (done) {
409434

410435
WebSocketAPI.fetch("something404", undefined, {
411-
use: "ws",
412-
method: "PUT",
413-
})
436+
use: "ws",
437+
method: "PUT",
438+
})
414439
.then(_ => done(new Error("Was supposed to throw a error.")))
415440
.catch(err => {
416441
try {
@@ -428,9 +453,9 @@ describe("browser", () => {
428453
it("should throw error with 'test error'", function (done) {
429454

430455
WebSocketAPI.fetch("error", postData, {
431-
use: "ws",
432-
method: "PUT",
433-
})
456+
use: "ws",
457+
method: "PUT",
458+
})
434459
.then(_ => done(new Error("Was supposed to throw a error.")))
435460
.catch(err => {
436461
try {
@@ -448,10 +473,10 @@ describe("browser", () => {
448473
it("should throw timeout error with 'Timeout Error'", function (done) {
449474

450475
WebSocketAPI.fetch("timeout", postData, {
451-
use: "ws",
452-
timeout: 150,
453-
method: "PUT",
454-
})
476+
use: "ws",
477+
timeout: 150,
478+
method: "PUT",
479+
})
455480
.then(_ => done(new Error("Was supposed to throw a timeout error.")))
456481
.catch(err => {
457482
try {
@@ -485,9 +510,9 @@ describe("browser", () => {
485510
it("should throw error with 'Unknown api request.'", function (done) {
486511

487512
WebSocketAPI.fetch("something404", postData, {
488-
use: "http",
489-
method: "PUT",
490-
})
513+
use: "http",
514+
method: "PUT",
515+
})
491516
.then(_ => done(new Error("Was supposed to throw a error.")))
492517
.catch(err => {
493518
try {
@@ -505,9 +530,9 @@ describe("browser", () => {
505530
it("should throw error with 'test error'", function (done) {
506531

507532
WebSocketAPI.fetch("error", postData, {
508-
use: "http",
509-
method: "PUT",
510-
})
533+
use: "http",
534+
method: "PUT",
535+
})
511536
.then(_ => done(new Error("Was supposed to throw a error.")))
512537
.catch(err => {
513538
try {
@@ -525,10 +550,10 @@ describe("browser", () => {
525550
it("should throw timeout error with 'Timeout Error'", function (done) {
526551

527552
WebSocketAPI.fetch("timeout", postData, {
528-
use: "http",
529-
timeout: 150,
530-
method: "PUT",
531-
})
553+
use: "http",
554+
timeout: 150,
555+
method: "PUT",
556+
})
532557
.then(_ => done(new Error("Was supposed to throw a timeout error.")))
533558
.catch(err => {
534559
try {
@@ -571,8 +596,8 @@ describe("browser", () => {
571596
it("should throw error with 'Unknown api request.'", function (done) {
572597

573598
WebSocketAPI.fetch("something404", undefined, {
574-
use: "ws"
575-
})
599+
use: "ws"
600+
})
576601
.then(_ => done(new Error("Was supposed to throw a error.")))
577602
.catch(err => {
578603
try {
@@ -590,9 +615,9 @@ describe("browser", () => {
590615
it("should throw error with 'test error'", function (done) {
591616

592617
WebSocketAPI.fetch("error", undefined, {
593-
use: "ws",
594-
method: "DELETE"
595-
})
618+
use: "ws",
619+
method: "DELETE"
620+
})
596621
.then(_ => done(new Error("Was supposed to throw a error.")))
597622
.catch(err => {
598623
try {
@@ -610,10 +635,10 @@ describe("browser", () => {
610635
it("should throw timeout error with 'Timeout Error'", function (done) {
611636

612637
WebSocketAPI.fetch("timeout", undefined, {
613-
use: "ws",
614-
timeout: 150,
615-
method: "DELETE"
616-
})
638+
use: "ws",
639+
timeout: 150,
640+
method: "DELETE"
641+
})
617642
.then(_ => done(new Error("Was supposed to throw a timeout error.")))
618643
.catch(err => {
619644
try {
@@ -649,9 +674,9 @@ describe("browser", () => {
649674
it("should throw error with 'Unknown api request.'", function (done) {
650675

651676
WebSocketAPI.fetch("something404", undefined, {
652-
use: "http",
653-
method: "DELETE"
654-
})
677+
use: "http",
678+
method: "DELETE"
679+
})
655680
.then(_ => done(new Error("Was supposed to throw a error.")))
656681
.catch(err => {
657682
try {
@@ -669,9 +694,9 @@ describe("browser", () => {
669694
it("should throw error with 'test error'", function (done) {
670695

671696
WebSocketAPI.fetch("error", undefined, {
672-
use: "http",
673-
method: "DELETE"
674-
})
697+
use: "http",
698+
method: "DELETE"
699+
})
675700
.then(_ => done(new Error("Was supposed to throw a error.")))
676701
.catch(err => {
677702
try {
@@ -689,10 +714,10 @@ describe("browser", () => {
689714
it("should throw timeout error with 'Timeout Error'", function (done) {
690715

691716
WebSocketAPI.fetch("timeout", undefined, {
692-
use: "http",
693-
timeout: 150,
694-
method: "DELETE"
695-
})
717+
use: "http",
718+
timeout: 150,
719+
method: "DELETE"
720+
})
696721
.then(_ => done(new Error("Was supposed to throw a timeout error.")))
697722
.catch(err => {
698723
try {

0 commit comments

Comments
 (0)