Browse Source

Attempt to support QUERY method

Prepare tests for supporting QUERY method.
master
TheoryOfNekomata 7 months ago
parent
commit
ad68214eb3
3 changed files with 50 additions and 6 deletions
  1. +6
    -6
      packages/core/src/backend/servers/http/core.ts
  2. +38
    -0
      packages/core/test/handlers/http/default.test.ts
  3. +6
    -0
      packages/core/test/utils.ts

+ 6
- 6
packages/core/src/backend/servers/http/core.ts View File

@@ -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 * as v from 'valibot';
import { import {
AllowedMiddlewareSpecification, AllowedMiddlewareSpecification,
@@ -196,12 +196,13 @@ export const createServer = (backendState: BackendState, serverParams = {} as Cr
const theRes = new CqrsEventEmitter(); const theRes = new CqrsEventEmitter();


const server = isHttps const server = isHttps
? https.createServer({
? httpCreateSecureServer({
key: serverParams.key, key: serverParams.key,
cert: serverParams.cert, cert: serverParams.cert,
requestTimeout: serverParams.requestTimeout, requestTimeout: serverParams.requestTimeout,
// TODO add custom methods
}) })
: http.createServer({
: httpCreateServer({
requestTimeout: serverParams.requestTimeout, requestTimeout: serverParams.requestTimeout,
}); });


@@ -804,6 +805,5 @@ export const createServer = (backendState: BackendState, serverParams = {} as Cr
return this; return this;
} }
} satisfies Server; } satisfies Server;

// return server; // return server;
} }

+ 38
- 0
packages/core/test/handlers/http/default.test.ts View File

@@ -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', () => { describe('serving collections', () => {
beforeEach(() => { beforeEach(() => {
vi vi


+ 6
- 0
packages/core/test/utils.ts View File

@@ -46,6 +46,11 @@ export const createTestClient = (options: Omit<RequestOptions, 'method' | 'path'
}); });


req.on('response', (res) => { req.on('response', (res) => {
// if (req.method.toUpperCase() === 'QUERY') {
// res.statusMessage = '';
// res.statusCode = 200;
// }

res.on('error', (err) => { res.on('error', (err) => {
reject(err); reject(err);
}); });
@@ -195,6 +200,7 @@ export class DummyDataSource implements DataSource {
export const TEST_LANGUAGE: Language = { export const TEST_LANGUAGE: Language = {
name: FALLBACK_LANGUAGE.name, name: FALLBACK_LANGUAGE.name,
statusMessages: { statusMessages: {
resourceCollectionQueried: '$Resource Collection Queried',
unableToSerializeResponse: 'Unable To Serialize Response', unableToSerializeResponse: 'Unable To Serialize Response',
unableToEncodeResponse: 'Unable To Encode Response', unableToEncodeResponse: 'Unable To Encode Response',
unableToBindResourceDataSource: 'Unable To Bind $RESOURCE Data Source', unableToBindResourceDataSource: 'Unable To Bind $RESOURCE Data Source',


Loading…
Cancel
Save