From 88124ae90d9b21da99070a382cff464148354cab Mon Sep 17 00:00:00 2001 From: danielpeintner Date: Tue, 2 Jul 2024 16:40:54 +0200 Subject: [PATCH 1/2] fix: SSE event data reporting --- packages/binding-http/src/subscription-protocols.ts | 5 +---- packages/binding-http/test/http-client-test.ts | 5 ++++- 2 files changed, 5 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..eb419ffba 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"; @@ -463,7 +464,9 @@ class HttpClientTest2 { }; client - .subscribeResource(form, (data) => { + .subscribeResource(form, async (data) => { + const str = await text(data.body); // SHOULD be "Test event" + str.should.eql("Test event"); client.unlinkResource(form); server.close(); clock.uninstall(); From 2808a7f2ab67315ef55028007bf16f752dcb75c3 Mon Sep 17 00:00:00 2001 From: danielpeintner Date: Thu, 4 Jul 2024 11:10:31 +0200 Subject: [PATCH 2/2] refactor: stabilize test --- packages/binding-http/test/http-client-test.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/binding-http/test/http-client-test.ts b/packages/binding-http/test/http-client-test.ts index eb419ffba..31ce88cfb 100644 --- a/packages/binding-http/test/http-client-test.ts +++ b/packages/binding-http/test/http-client-test.ts @@ -38,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"); @@ -426,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(); @@ -446,6 +449,9 @@ class HttpClientTest2 { clearInterval(pusher); sseStream.unpipe(res); done(); + if (dataCheckError !== undefined) { + fail(dataCheckError); + } }); }); @@ -465,8 +471,9 @@ class HttpClientTest2 { client .subscribeResource(form, async (data) => { - const str = await text(data.body); // SHOULD be "Test event" - str.should.eql("Test event"); + if ((await text(data.body)) !== "Test event") { + dataCheckError = "Data should report 'Test event'"; + } client.unlinkResource(form); server.close(); clock.uninstall();