Skip to content

Commit 5e2b362

Browse files
fix: SSE data reporting (#1298)
* fix: SSE event data reporting * refactor: stabilize test
1 parent fc67ecd commit 5e2b362

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

packages/binding-http/src/subscription-protocols.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ export class SSESubscription implements InternalSubscription {
117117
};
118118
this.eventSource.onmessage = (event) => {
119119
debug(`HttpClient received ${JSON.stringify(event)} from ${this.form.href}`);
120-
const output = new Content(
121-
this.form.contentType ?? ContentSerdes.DEFAULT,
122-
Readable.from(JSON.stringify(event))
123-
);
120+
const output = new Content(this.form.contentType ?? ContentSerdes.DEFAULT, Readable.from(event.data));
124121
next(output);
125122
};
126123
this.eventSource.onerror = function (event) {

packages/binding-http/test/http-client-test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import * as http from "http";
2525
import { Content, DefaultContent, ContentSerdes, createLoggers, ProtocolServer } from "@node-wot/core";
2626

2727
import { Readable } from "stream";
28+
import { text } from "node:stream/consumers";
2829

2930
import HttpClient from "../src/http-client";
3031
import { HttpForm } from "../src/http";
@@ -37,6 +38,7 @@ import FakeTimers from "@sinonjs/fake-timers";
3738

3839
// Add spies
3940
import spies from "chai-spies";
41+
import { fail } from "assert";
4042

4143
const { debug } = createLoggers("binding-http", "http-client-test");
4244

@@ -425,6 +427,8 @@ class HttpClientTest1 {
425427
@suite("HTTP client subscriptions")
426428
class HttpClientTest2 {
427429
@test "should register to sse server and get server sent event"(done: Mocha.Done) {
430+
let dataCheckError: string | undefined;
431+
428432
// create sse server
429433
const clock = FakeTimers.install();
430434
const app = express();
@@ -445,6 +449,9 @@ class HttpClientTest2 {
445449
clearInterval(pusher);
446450
sseStream.unpipe(res);
447451
done();
452+
if (dataCheckError !== undefined) {
453+
fail(dataCheckError);
454+
}
448455
});
449456
});
450457

@@ -463,7 +470,10 @@ class HttpClientTest2 {
463470
};
464471

465472
client
466-
.subscribeResource(form, (data) => {
473+
.subscribeResource(form, async (data) => {
474+
if ((await text(data.body)) !== "Test event") {
475+
dataCheckError = "Data should report 'Test event'";
476+
}
467477
client.unlinkResource(form);
468478
server.close();
469479
clock.uninstall();

0 commit comments

Comments
 (0)