Skip to content

Commit

Permalink
working on 8.0, vitest, maybe no browserify
Browse files Browse the repository at this point in the history
  • Loading branch information
Dashron committed Jan 2, 2025
1 parent 3568477 commit 4f8ae35
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 74 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "roads",
"version": "7.4.1",
"version": "8.0.0-alpha.0",
"author": {
"name": "Aaron Hedges",
"email": "[email protected]",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import Client from '../../../client/request';
import Client from '../../../src/client/request';
import createServer, { port } from '../../resources/mockServer';
import { Server as HttpServer } from 'http';
import { Server as HttpsServer } from 'https';
import Response from '../../../core/response';
import fetch, { Headers, Request, Response as FetchResponse } from 'node-fetch';
import Response from '../../../src/core/response';


if (!globalThis.fetch) {
// @ts-ignore
globalThis.fetch = fetch;
// @ts-ignore
globalThis.Headers = Headers;
// @ts-ignore
globalThis.Request = Request;
// @ts-ignore
globalThis.Response = FetchResponse;
}
import { afterAll, beforeAll, describe, expect, test } from 'vitest';

describe('request', () => {
let server: HttpServer | HttpsServer;
Expand Down Expand Up @@ -50,20 +39,22 @@ describe('request', () => {
one : 'two'
}).then(function (response: Response) {
expect(response.status).toEqual(200);
expect(response.body).toEqual(JSON.stringify({
expect(JSON.parse(response.body as string)).toEqual({
url: '/',
method: 'GET',
body: '',
headers: {
one: 'two',
accept: '*/*',
'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
'accept-encoding': 'gzip,deflate',
connection: 'close',
'user-agent': 'node',
'accept-encoding': 'gzip, deflate',
'accept-language': '*',
'sec-fetch-mode': 'cors',
connection: 'keep-alive',
host: `127.0.0.1:${port}`,
},
message: 'hello!'
}));
});
expect(response.headers['this-is']).toEqual('for real');
}));
});
Expand All @@ -78,21 +69,23 @@ describe('request', () => {
one : ['two', 'three']
}).then(function (response: Response) {
expect(response.status).toEqual(200);
expect(response.body).toEqual(JSON.stringify({
expect(JSON.parse(response.body as string)).toEqual({
url: '/',
method: 'GET',
body: '',
headers: {
// node fetch doesn't seem to retain dupe arrays, this is what we get.
one: 'two, three',
accept: '*/*',
'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
'accept-encoding': 'gzip,deflate',
connection: 'close',
'user-agent': 'node',
'accept-encoding': 'gzip, deflate',
'accept-language': '*',
connection: 'keep-alive',
host: `127.0.0.1:${port}`,
'sec-fetch-mode': 'cors',
},
message: 'hello!'
}));
});
// I don't think this is correct for dupliate headers, it seems to be something node-fetch is doing,
// not sure if it's spec accurate: https://github.com/node-fetch/node-fetch/issues/771
expect(response.headers['cache-control']).toEqual('no-cache, no-store');
Expand All @@ -114,7 +107,7 @@ describe('request', () => {
}).then(function (response: Response) {
expect(response.status).toEqual(200);

expect(response.body).toEqual(JSON.stringify({
expect(JSON.parse(response.body as string)).toMatchObject({
url: '/',
method: 'POST',
body: '{"yeah": "what"}',
Expand All @@ -123,13 +116,13 @@ describe('request', () => {
'content-type': 'text/plain;charset=UTF-8',
accept: '*/*',
'content-length': '16',
'user-agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
'accept-encoding': 'gzip,deflate',
connection: 'close',
'user-agent': 'node',
'accept-encoding': 'gzip, deflate',
connection: 'keep-alive',
host: `127.0.0.1:${port}`,
},
message: 'hello!'
}));
});

expect(response.headers['content-type']).toEqual('application/json');
}));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { build } from '../../../middleware/applyToContext';
import { build } from '../../../src/middleware/applyToContext';
import { describe, expect, test } from 'vitest';

describe('ApplyToContext tests', () => {
test('test apply to context applies context', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as url_module from 'url';

import { BasicRouter, Route, BasicRouterURL } from '../../../middleware/basicRouter';
import Road from '../../../core/road';
import Response from '../../../core/response';
import { Context, NextCallback } from '../../../core/road';
import { BasicRouter, Route, BasicRouterURL } from '../../../src/middleware/basicRouter';
import Road from '../../../src/core/road';
import Response from '../../../src/core/response';
import { Context, NextCallback } from '../../../src/core/road';

import { describe, expect, test, assert } from 'vitest';

const router_file_test_path = `${__dirname }/../../resources/_router_file_test.js`;

Expand Down Expand Up @@ -82,9 +84,7 @@ describe('Basic Router Tests', () => {
};

router.addRoute('PUT', path, fn);
router.addRoute('POST', path, () => {
fail('POST route should not run');
});
router.addRoute('POST', path, () => assert.fail('POST route should not run'));
router['_middleware'](router['_routes'], method, path, '', {
'x-http-method-override': 'PUT'
}, next);
Expand All @@ -109,9 +109,7 @@ describe('Basic Router Tests', () => {
};

router.addRoute('GET', path, fn);
router.addRoute('PUT', path, () => {
fail('PUT route should not run');
});
router.addRoute('PUT', path, () => assert.fail('PUT route should not run'));
router['_middleware'](router['_routes'], method, path, '', {
'x-http-method-override': 'PUT'
}, next);
Expand All @@ -136,9 +134,7 @@ describe('Basic Router Tests', () => {
};

router.addRoute('PUT', path, fn);
router.addRoute('POST', path, () => {
fail('POST route should not run');
});
router.addRoute('POST', path, () => assert.fail('POST route should not run'));
router['_middleware'](router['_routes'], method, `${path}?_method=PUT`, '', {}, next);

expect(route_hit).toEqual(true);
Expand All @@ -161,9 +157,7 @@ describe('Basic Router Tests', () => {
};

router.addRoute('GET', path, fn);
router.addRoute('PUT', path, () => {
fail('PUT route should not run');
});
router.addRoute('PUT', path, () => assert.fail('PUT route should not run'));

router['_middleware'](router['_routes'], method, `${path}?_method=PUT`, '', {}, next);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { serverMiddleware, buildClientMiddleware } from '../../../middleware/cookieMiddleware';
import { serverMiddleware, buildClientMiddleware } from '../../../src/middleware/cookieMiddleware';

import { CookieContext } from '../../../middleware/cookieMiddleware';
import Response from '../../../core/response';
import { CookieContext } from '../../../src/middleware/cookieMiddleware';
import Response from '../../../src/core/response';

import { describe, expect, test } from 'vitest';

describe('cookie tests', () => {
test('test cookie middleware parses cookies into context', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { build } from '../../../middleware/cors';
import { build } from '../../../src/middleware/cors';
import { describe, expect, test } from 'vitest';

describe('Cors tests', () => {
test('test cors middleware doesn\'t break normal', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { middleware, ModifiedSinceContext } from '../../../middleware/modifiedSince';
import Response from '../../../core/response';
import { middleware, ModifiedSinceContext } from '../../../src/middleware/modifiedSince';
import Response from '../../../src/core/response';

import { describe, expect, test } from 'vitest';

describe('modified sine tests', () => {
test('test not-yet-updated endpoints return 304', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { middleware } from '../../../middleware/parseBody';
import { middleware } from '../../../src/middleware/parseBody';

import { Context, Middleware as MiddlewareType } from '../../../core/road';
import { Road } from '../../../index';
import Response from '../../../core/response';
import { Context, Middleware as MiddlewareType } from '../../../src/core/road';
import { Road } from '../../../src/index';
import Response from '../../../src/core/response';

import { describe, expect, test, assert } from 'vitest';

describe('Parse Request Body tests', () => {
test('test request with valid json body', () => {
Expand All @@ -26,7 +28,7 @@ describe('Parse Request Body tests', () => {

// eslint-disable-next-line @typescript-eslint/no-empty-function
const response = middleware.call(context, '', '', body, {'content-type': 'application/json'}, () => {
fail('Next should not be called if the request body can not be parsed');
assert.fail('Next should not be called if the request body can not be parsed');
});

expect(context.body).toBe(undefined);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { middleware } from '../../../middleware/removeTrailingSlash';
import { middleware } from '../../../src/middleware/removeTrailingSlash';

import Response from '../../../core/response';
import Response from '../../../src/core/response';

import { describe, expect, test } from 'vitest';

describe('KillSlashes tests', () => {
test('test remove slash doesn\'t break normal', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { build } from '../../../middleware/reroute';
import Road from '../../../core/road';
import { Context, Middleware as MiddlewareType } from '../../../core/road';
import Response from '../../../core/response';
import { build } from '../../../src/middleware/reroute';
import Road from '../../../src/core/road';
import { Context, Middleware as MiddlewareType } from '../../../src/core/road';
import Response from '../../../src/core/response';

import { describe, expect, test } from 'vitest';

describe('Reroute middleware tests', () => {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { middleware } from '../../../middleware/storeVals';
import { middleware } from '../../../src/middleware/storeVals';

import { describe, expect, test } from 'vitest';

describe('Store Values', () => {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Response } from '../../index';
import { Response } from '../../src/index';

import { describe, expect, test } from 'vitest';

describe('response tests', () => {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Road } from '../../../index';
import { Response } from '../../../index';
import { Road } from '../../../src/index';
import { Response } from '../../../src/index';

import { describe, expect, test } from 'vitest';

// Note: This file used to have many more tests, but a recent roads change invalidated most of them, and the
// migration to jest made it clear that many of them were
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

import { Road } from '../../../index';
import { Context } from '../../../core/road';
import { Road } from '../../../src/index';
import { Context } from '../../../src/core/road';

import { describe, expect, test } from 'vitest';

interface confirmContext {
confirmString: () => string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Road } from '../../../index';
import { Context, Middleware } from '../../../core/road';
import { Response } from '../../../index';
import { Road } from '../../../src/index';
import { Context, Middleware } from '../../../src/core/road';
import { Response } from '../../../src/index';

import { describe, expect, test } from 'vitest';

describe('road request', () => {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Road } from '../../../index';
import { Road } from '../../../src/index';

import { describe, expect, test } from 'vitest';

describe('road use', () => {
/**
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 4f8ae35

Please sign in to comment.