-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from comake/feat/websockets
feat/websockets
- Loading branch information
Showing
4 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { WebSocket } from 'ws'; | ||
import { WebSocketHandler } from '../../src/http/handler/WebSocketHandler'; | ||
import type { WebSocketHandlerInput } from '../../src/http/handler/WebSocketHandler'; | ||
import type { App } from '../../src/init/App'; | ||
import { getPort } from '../util/Util'; | ||
import { | ||
getDefaultVariables, | ||
getTestConfigPath, | ||
instantiateFromConfig, | ||
} from './Config'; | ||
|
||
const port = getPort('WebSockets'); | ||
const baseUrl = `http://localhost:${port}`; | ||
const message = 'Hello there!'; | ||
|
||
class SimpleWebSocketHandler extends WebSocketHandler { | ||
public async handle(input: WebSocketHandlerInput): Promise<void> { | ||
input.webSocket.send(message); | ||
} | ||
} | ||
|
||
describe('A http server with websocket endpoint handlers', (): void => { | ||
let app: App; | ||
|
||
beforeAll(async(): Promise<void> => { | ||
const instances = await instantiateFromConfig( | ||
'urn:solid-on-rails:test:Instances', | ||
getTestConfigPath('websocket-server.json'), | ||
{ | ||
...getDefaultVariables(port, baseUrl), | ||
'urn:solid-on-rails:default:ExampleWebSocketHandler': new SimpleWebSocketHandler(), | ||
}, | ||
) as Record<string, any>; | ||
({ app } = instances); | ||
await app.start(); | ||
}); | ||
|
||
afterAll(async(): Promise<void> => { | ||
await app.stop(); | ||
}); | ||
|
||
it('handles websocket connections.', async(): Promise<void> => { | ||
let parsedMessage: string | undefined; | ||
await new Promise<void>((resolve, reject): void => { | ||
const webSocket = new WebSocket(`ws://localhost:${port}/websocket-path`); | ||
webSocket.on('message', (data): void => { | ||
if (typeof data === 'string') { | ||
parsedMessage = data; | ||
} else if (data instanceof Buffer) { | ||
parsedMessage = data.toString(); | ||
} | ||
webSocket.close(); | ||
resolve(); | ||
}); | ||
webSocket.on('error', (error): void => { | ||
reject(error); | ||
}); | ||
}); | ||
|
||
expect(parsedMessage).toEqual(message); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@comake/solid-on-rails/^0.0.0/components/context.jsonld", | ||
"import": [ | ||
"files-sor:config/app/main/default.json", | ||
"files-sor:config/app/initialize/default.json", | ||
"files-sor:config/app/finalize/default.json", | ||
"files-sor:config/app/variables/default.json", | ||
"files-sor:config/app/path/default.json", | ||
"files-sor:config/http/static/default.json", | ||
"files-sor:config/http/error-handler.json", | ||
"files-sor:config/server/server-factory/default.json", | ||
"files-sor:config/storage/key-value/memory.json", | ||
"files-sor:config/util/variables/default.json", | ||
"files-sor:config/util/logging/winston.json" | ||
], | ||
"@graph": [ | ||
{ | ||
"comment": "Attempt to resolve requests first as static assets, then as normal routes.", | ||
"@id": "urn:solid-on-rails:default:HttpHandler", | ||
"@type": "SequenceHandler", | ||
"handlers": [ | ||
{ "@id": "urn:solid-on-rails:default:StaticAssetHandler" } | ||
] | ||
}, | ||
{ | ||
"@id": "urn:solid-on-rails:default:WebSocketHandler", | ||
"WaterfallHandler:_handlers": [ | ||
{ "@id": "urn:solid-on-rails:default:ExampleWebSocketHandler" } | ||
] | ||
}, | ||
{ | ||
"@id": "urn:solid-on-rails:default:ExampleWebSocketHandler", | ||
"@type": "Variable" | ||
}, | ||
{ | ||
"@id": "urn:solid-on-rails:test:Instances", | ||
"@type": "RecordObject", | ||
"RecordObject:_record": [ | ||
{ | ||
"RecordObject:_record_key": "app", | ||
"RecordObject:_record_value": { "@id": "urn:solid-on-rails:default:App" } | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters