Skip to content

Commit

Permalink
fix: SSE data reporting (#1298)
Browse files Browse the repository at this point in the history
* fix: SSE event data reporting

* refactor: stabilize test
  • Loading branch information
danielpeintner authored Jul 8, 2024
1 parent fc67ecd commit 5e2b362
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 1 addition & 4 deletions packages/binding-http/src/subscription-protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ export class SSESubscription implements InternalSubscription {
};
this.eventSource.onmessage = (event) => {
debug(`HttpClient received ${JSON.stringify(event)} from ${this.form.href}`);
const output = new Content(
this.form.contentType ?? ContentSerdes.DEFAULT,
Readable.from(JSON.stringify(event))
);
const output = new Content(this.form.contentType ?? ContentSerdes.DEFAULT, Readable.from(event.data));
next(output);
};
this.eventSource.onerror = function (event) {
Expand Down
12 changes: 11 additions & 1 deletion packages/binding-http/test/http-client-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import * as http from "http";
import { Content, DefaultContent, ContentSerdes, createLoggers, ProtocolServer } from "@node-wot/core";

import { Readable } from "stream";
import { text } from "node:stream/consumers";

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

// Add spies
import spies from "chai-spies";
import { fail } from "assert";

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

Expand Down Expand Up @@ -425,6 +427,8 @@ class HttpClientTest1 {
@suite("HTTP client subscriptions")
class HttpClientTest2 {
@test "should register to sse server and get server sent event"(done: Mocha.Done) {
let dataCheckError: string | undefined;

// create sse server
const clock = FakeTimers.install();
const app = express();
Expand All @@ -445,6 +449,9 @@ class HttpClientTest2 {
clearInterval(pusher);
sseStream.unpipe(res);
done();
if (dataCheckError !== undefined) {
fail(dataCheckError);
}
});
});

Expand All @@ -463,7 +470,10 @@ class HttpClientTest2 {
};

client
.subscribeResource(form, (data) => {
.subscribeResource(form, async (data) => {
if ((await text(data.body)) !== "Test event") {
dataCheckError = "Data should report 'Test event'";
}
client.unlinkResource(form);
server.close();
clock.uninstall();
Expand Down

0 comments on commit 5e2b362

Please sign in to comment.