Very Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197// Copyright 2018-2020 the oak authors. All rights reserved. MIT license.
import { assert, assertEquals, BufWriter, StringWriter, test,} from "./test_deps.ts";
import { Application } from "./application.ts";import { ServerSentEvent, ServerSentEventTarget } from "./server_sent_event.ts";import { ServerRequest } from "./types.d.ts";
const preamble = `HTTP/1.1 200 OKconnection: Keep-Alivecontent-type: text/event-streamcache-control: no-cachekeep-alive: timeout=9007199254740991
`;
const env = { appTarget: new EventTarget(), appErrorEvents: [] as ErrorEvent[], outWriter: new StringWriter(), connCloseCalled: false,};
function createMockApp(): Application { env.appTarget = new EventTarget(); env.appErrorEvents = []; env.appTarget.addEventListener( "error", (evt) => { env.appErrorEvents.push(evt as ErrorEvent); }, ); return env.appTarget as any;}
function createMockServerRequest(): ServerRequest { env.outWriter = new StringWriter(); env.connCloseCalled = false; return { conn: { close() { env.connCloseCalled = true; }, }, w: new BufWriter(env.outWriter), } as any;}
test({ name: "ServerSentEvent - construction", fn() { const evt = new ServerSentEvent("message", "foobar"); assertEquals(evt.type, "message"); assertEquals(evt.data, "foobar"); assertEquals(evt.id, undefined); assertEquals(String(evt), `event: message\ndata: foobar\n\n`); },});
test({ name: "ServerSentEvent - data coercion", fn() { const evt = new ServerSentEvent("ping", { hello: true }); assertEquals(evt.type, "ping"); assertEquals(evt.data, `{"hello":true}`); assertEquals(evt.id, undefined); assertEquals(String(evt), `event: ping\ndata: {"hello":true}\n\n`); },});
test({ name: "ServerSentEvent - init id", fn() { const evt = new ServerSentEvent("ping", "foobar", { id: 1234 }); assertEquals(evt.type, "ping"); assertEquals(evt.data, `foobar`); assertEquals(evt.id, 1234); assertEquals( String(evt), `event: ping\nid: 1234\ndata: foobar\n\n`, ); },});
test({ name: "ServerSentEvent - data space", fn() { const evt = new ServerSentEvent("ping", { hello: [1, 2, 3] }, { space: 2 }); assertEquals(evt.type, "ping"); assertEquals(evt.data, `{\n "hello": [\n 1,\n 2,\n 3\n ]\n}`); assertEquals( String(evt), `event: ping\ndata: {\ndata: "hello": [\ndata: 1,\ndata: 2,\ndata: 3\ndata: ]\ndata: }\n\n`, ); },});
test({ name: "ServerSentEvent - __message", fn() { const evt = new ServerSentEvent("__message", { hello: "world" }); assertEquals(evt.type, "__message"); assertEquals(evt.data, `{"hello":"world"}`); assertEquals(String(evt), `data: {"hello":"world"}\n\n`); },});
test({ name: "ServerSentEventTarget - construction", async fn() { const sse = new ServerSentEventTarget( createMockApp(), createMockServerRequest(), ); assertEquals(sse.closed, false); await sse.close(); assert(env.connCloseCalled); assertEquals(sse.closed, true); const actual = String(env.outWriter); assertEquals(actual, preamble); assertEquals(env.appErrorEvents.length, 0); },});
test({ name: "ServerSentEventTaget - construction with headers", async fn() { const expected = `HTTP/1.1 200 OK\nconnection: Keep-Alive\ncontent-type: text/event-stream\ncache-control: special\nkeep-alive: timeout=9007199254740991\nx-oak: test\n\n`; const sse = new ServerSentEventTarget( createMockApp(), createMockServerRequest(), { headers: new Headers([["X-Oak", "test"], ["Cache-Control", "special"]]), }, ); await sse.close(); const actual = String(env.outWriter); assertEquals(actual, expected); },});
test({ name: "ServerSentEventTarget - dispatchEvent", async fn() { const sse = new ServerSentEventTarget( createMockApp(), createMockServerRequest(), ); const evt = new ServerSentEvent("message", "foobar"); sse.dispatchEvent(evt); await sse.close(); assert(env.connCloseCalled); const actual = env.outWriter.toString(); assert(actual.endsWith(`\n\nevent: message\ndata: foobar\n\n`)); assertEquals(env.appErrorEvents.length, 0); },});
test({ name: "ServerSentEventTarget - dispatchMessage", async fn() { const sse = new ServerSentEventTarget( createMockApp(), createMockServerRequest(), ); sse.dispatchMessage("foobar"); await sse.close(); assert(env.connCloseCalled); const actual = env.outWriter.toString(); assert(actual.endsWith(`\n\ndata: foobar\n\n`)); assertEquals(env.appErrorEvents.length, 0); },});
test({ name: "ServerSentEventTarget - dispatchComment", async fn() { const sse = new ServerSentEventTarget( createMockApp(), createMockServerRequest(), ); sse.dispatchComment("foobar"); await sse.close(); assert(env.connCloseCalled); const actual = env.outWriter.toString(); assert(actual.endsWith(`\n\n: foobar\n\n`)); assertEquals(env.appErrorEvents.length, 0); },});