-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSseTests.cs
More file actions
110 lines (102 loc) · 3.31 KB
/
Copy pathSseTests.cs
File metadata and controls
110 lines (102 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
namespace NpgsqlRestTests
{
public static partial class Database
{
public static void TsClientSseTests()
{
script.Append("""
create schema if not exists tsclient_test;
create function tsclient_test.sse_endpoint(_message text)
returns text
language plpgsql
as $$
begin
raise notice '%', _message;
return 'done';
end;
$$;
comment on function tsclient_test.sse_endpoint(text) is '
tsclient_module=sse_endpoint
HTTP POST
sse_events_path /api/tsclient-test/sse-endpoint/events
';
""");
}
}
}
namespace NpgsqlRestTests.TsClientTests
{
[Collection("TestFixture")]
public class SseTests
{
private const string Expected = """
const baseUrl = "";
export const createTsclientTestSseEndpointEventSource = (id: string = "") => new EventSource(baseUrl + "/api/tsclient-test/sse-endpoint/events?" + id);
interface ITsclientTestSseEndpointRequest {
message: string | null;
}
/**
* function tsclient_test.sse_endpoint(
* _message text
* )
* returns text
*
* @remarks
* comment on function tsclient_test.sse_endpoint is 'tsclient_module=sse_endpoint
* HTTP POST
* sse_events_path /api/tsclient-test/sse-endpoint/events';
*
* @param request - Object containing request parameters.
* @param onMessage - Optional callback function to handle incoming SSE messages.
* @param id - Optional execution ID for SSE connection. When supplied, only EventSource object with this ID in query string will will receive events.
* @param closeAfterMs - Time in milliseconds to wait before closing the EventSource connection. Used only when onMessage callback is provided.
* @param awaitConnectionMs - Time in milliseconds to wait after opening the EventSource connection before sending the request. Used only when onMessage callback is provided.
* @returns {string}
*
* @see FUNCTION tsclient_test.sse_endpoint
*/
export async function tsclientTestSseEndpoint(
request: ITsclientTestSseEndpointRequest,
onMessage?: (message: string) => void,
id: string | undefined = undefined,
closeAfterMs = 1000,
awaitConnectionMs: number | undefined = 0
) : Promise<string> {
const executionId = id ? id : window.crypto.randomUUID();
let eventSource: EventSource;
if (onMessage) {
eventSource = createTsclientTestSseEndpointEventSource(executionId);
eventSource.onmessage = (event: MessageEvent) => {
onMessage(event.data);
};
if (awaitConnectionMs !== undefined) {
await new Promise(resolve => setTimeout(resolve, awaitConnectionMs));
}
}
try {
const response = await fetch(baseUrl + "/api/tsclient-test/sse-endpoint", {
method: "POST",
headers: {
"X-NpgsqlRest-ID": executionId
},
body: JSON.stringify(request)
});
return await response.text();
}
finally {
if (onMessage) {
setTimeout(() => eventSource.close(), closeAfterMs);
}
}
}
""";
[Fact]
public void Test_SseEndpoint_GeneratedFile()
{
var filePath = Path.Combine(Setup.Program.TsClientOutputPath, "sse_endpoint.ts");
File.Exists(filePath).Should().BeTrue($"Expected file at {filePath}");
var content = File.ReadAllText(filePath);
content.Should().Be(Expected);
}
}
}