From 5e2b362a28dec55a49a82b855e2727bab91d8ff7 Mon Sep 17 00:00:00 2001 From: danielpeintner Date: Mon, 8 Jul 2024 15:19:31 +0200 Subject: [PATCH] fix: SSE data reporting (#1298) * fix: SSE event data reporting * refactor: stabilize test --- packages/binding-http/src/subscription-protocols.ts | 5 +---- packages/binding-http/test/http-client-test.ts | 12 +++++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/binding-http/src/subscription-protocols.ts b/packages/binding-http/src/subscription-protocols.ts index 819d81265..024447be9 100644 --- a/packages/binding-http/src/subscription-protocols.ts +++ b/packages/binding-http/src/subscription-protocols.ts @@ -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) { diff --git a/packages/binding-http/test/http-client-test.ts b/packages/binding-http/test/http-client-test.ts index 14d7ad6a4..31ce88cfb 100644 --- a/packages/binding-http/test/http-client-test.ts +++ b/packages/binding-http/test/http-client-test.ts @@ -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"; @@ -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"); @@ -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(); @@ -445,6 +449,9 @@ class HttpClientTest2 { clearInterval(pusher); sseStream.unpipe(res); done(); + if (dataCheckError !== undefined) { + fail(dataCheckError); + } }); }); @@ -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();