From ad68214eb3009b68b957e884fcff7cd5e868cd8e Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Tue, 14 May 2024 20:59:44 +0800 Subject: [PATCH] Attempt to support QUERY method Prepare tests for supporting QUERY method. --- .../core/src/backend/servers/http/core.ts | 12 +++--- .../core/test/handlers/http/default.test.ts | 38 +++++++++++++++++++ packages/core/test/utils.ts | 6 +++ 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/packages/core/src/backend/servers/http/core.ts b/packages/core/src/backend/servers/http/core.ts index cd64e95..629344e 100644 --- a/packages/core/src/backend/servers/http/core.ts +++ b/packages/core/src/backend/servers/http/core.ts @@ -1,6 +1,6 @@ -import http from 'http'; -import https from 'https'; -import {constants} from 'http2'; +import http, { createServer as httpCreateServer } from 'http'; +import { createServer as httpCreateSecureServer } from 'https'; +import {constants,} from 'http2'; import * as v from 'valibot'; import { AllowedMiddlewareSpecification, @@ -196,12 +196,13 @@ export const createServer = (backendState: BackendState, serverParams = {} as Cr const theRes = new CqrsEventEmitter(); const server = isHttps - ? https.createServer({ + ? httpCreateSecureServer({ key: serverParams.key, cert: serverParams.cert, requestTimeout: serverParams.requestTimeout, + // TODO add custom methods }) - : http.createServer({ + : httpCreateServer({ requestTimeout: serverParams.requestTimeout, }); @@ -804,6 +805,5 @@ export const createServer = (backendState: BackendState, serverParams = {} as Cr return this; } } satisfies Server; - // return server; } diff --git a/packages/core/test/handlers/http/default.test.ts b/packages/core/test/handlers/http/default.test.ts index ab17e41..1222f98 100644 --- a/packages/core/test/handlers/http/default.test.ts +++ b/packages/core/test/handlers/http/default.test.ts @@ -105,6 +105,44 @@ describe('happy path', () => { }); })); + describe.skip('querying collections', () => { + beforeEach(() => { + vi + .spyOn(DummyDataSource.prototype, 'getMultiple') + .mockResolvedValueOnce([] as never); + }); + + beforeEach(() => { + Piano.canFetchCollection(); + }); + + afterEach(() => { + Piano.canFetchCollection(false); + }); + + it('returns data', async () => { + const [res, resData] = await client({ + method: 'QUERY', + path: `${BASE_PATH}/pianos`, + headers: { + 'content-type': 'application/x-www-form-urlencoded', + }, + body: 'foo=bar', + }); + + expect(res).toHaveProperty('statusCode', constants.HTTP_STATUS_OK); + expect(res).toHaveProperty('statusMessage', prepareStatusMessage(TEST_LANGUAGE.statusMessages.resourceCollectionFetched)); + expect(res.headers).toHaveProperty('content-type', expect.stringContaining(ACCEPT)); + + if (typeof resData === 'undefined') { + expect.fail('Response body must be defined.'); + return; + } + + expect(resData).toEqual([]); + }); + }); + describe('serving collections', () => { beforeEach(() => { vi diff --git a/packages/core/test/utils.ts b/packages/core/test/utils.ts index f328b1e..3211781 100644 --- a/packages/core/test/utils.ts +++ b/packages/core/test/utils.ts @@ -46,6 +46,11 @@ export const createTestClient = (options: Omit { + // if (req.method.toUpperCase() === 'QUERY') { + // res.statusMessage = ''; + // res.statusCode = 200; + // } + res.on('error', (err) => { reject(err); }); @@ -195,6 +200,7 @@ export class DummyDataSource implements DataSource { export const TEST_LANGUAGE: Language = { name: FALLBACK_LANGUAGE.name, statusMessages: { + resourceCollectionQueried: '$Resource Collection Queried', unableToSerializeResponse: 'Unable To Serialize Response', unableToEncodeResponse: 'Unable To Encode Response', unableToBindResourceDataSource: 'Unable To Bind $RESOURCE Data Source',