Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix identifiers parsing #79

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
263 changes: 263 additions & 0 deletions src/SimpleRestServer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
import { SimpleRestServer } from './SimpleRestServer.ts';

describe('SimpleRestServer', () => {
describe('getAll', () => {
it('should return list results according to the request parameters', async () => {
const data = {
posts: [
{
id: 1,
title: 'bazingaaa',
},
{
id: 2,
title: 'bazinga',
},
{
id: 3,
title: 'nope',
},
],
};

const server = new SimpleRestServer({
baseUrl: 'http://localhost:4000',
data,
});

const response = await server.handleRequest({
url: 'http://localhost:4000/posts',
method: 'GET',
params: {
filter: { q: 'bazin' },
range: [0, 1],
sort: 'title',
},
requestBody: undefined,
});

expect(response).toEqual(
expect.objectContaining({
status: 200,
body: [
{
id: 2,
title: 'bazinga',
},
{
id: 1,
title: 'bazingaaa',
},
],
}),
);
});
});
describe('getOne', () => {
it('should correctly get records with a numeric identifier', async () => {
const data = {
posts: [
{
id: 1,
title: 'test',
},
],
};

const server = new SimpleRestServer({
baseUrl: 'http://localhost:4000',
data,
});

const response = await server.handleRequest({
url: 'http://localhost:4000/posts/1',
method: 'GET',
params: {},
requestBody: undefined,
});

expect(response).toEqual(
expect.objectContaining({
status: 200,
body: {
id: 1,
title: 'test',
},
}),
);
});
it('should correctly get records with a string identifier', async () => {
const data = {
posts: [
{
id: 'bazinga',
title: 'test',
},
],
};

const server = new SimpleRestServer({
baseUrl: 'http://localhost:4000',
data,
});

const response = await server.handleRequest({
url: 'http://localhost:4000/posts/bazinga',
method: 'GET',
params: {},
requestBody: undefined,
});

expect(response).toEqual(
expect.objectContaining({
status: 200,
body: {
id: 'bazinga',
title: 'test',
},
}),
);
});
});
describe('update', () => {
it('should correctly update records with a numeric identifier', async () => {
const data = {
posts: [
{
id: 1,
title: 'test',
},
],
};

const server = new SimpleRestServer({
baseUrl: 'http://localhost:4000',
data,
});

const response = await server.handleRequest({
url: 'http://localhost:4000/posts/1',
method: 'PUT',
params: {},
requestBody: {
id: 1,
title: 'test42',
},
});

expect(response).toEqual(
expect.objectContaining({
status: 200,
body: {
id: 1,
title: 'test42',
},
}),
);
});
it('should correctly update records with a string identifier', async () => {
const data = {
posts: [
{
id: 'bazinga',
title: 'test',
},
],
};

const server = new SimpleRestServer({
baseUrl: 'http://localhost:4000',
data,
});

const response = await server.handleRequest({
url: 'http://localhost:4000/posts/bazinga',
method: 'PUT',
params: {},
requestBody: {
id: 'bazinga',
title: 'test42',
},
});

expect(response).toEqual(
expect.objectContaining({
status: 200,
body: {
id: 'bazinga',
title: 'test42',
},
}),
);
});
});
describe('delete', () => {
it('should correctly delete records with a numeric identifier', async () => {
const data = {
posts: [
{
id: 1,
title: 'test',
},
],
};

const server = new SimpleRestServer({
baseUrl: 'http://localhost:4000',
data,
});

const response = await server.handleRequest({
url: 'http://localhost:4000/posts/1',
method: 'DELETE',
params: {},
requestBody: undefined,
});

expect(response).toEqual(
expect.objectContaining({
status: 200,
body: {
id: 1,
title: 'test',
},
}),
);
});
it('should correctly delete records with a string identifier', async () => {
const data = {
posts: [
{
id: 'bazinga',
title: 'test',
},
],
};

const server = new SimpleRestServer({
baseUrl: 'http://localhost:4000',
data,
});

const response = await server.handleRequest({
url: 'http://localhost:4000/posts/bazinga',
method: 'DELETE',
params: {},
requestBody: {
id: 'bazinga',
title: 'test',
},
});

expect(response).toEqual(
expect.objectContaining({
status: 200,
body: {
id: 'bazinga',
title: 'test',
},
}),
);
});
});
});
10 changes: 7 additions & 3 deletions src/SimpleRestServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export class SimpleRestServer implements APIServer {
}

const matches = normalizedRequest.url?.match(
new RegExp(`^${this.baseUrl}\\/([^\\/?]+)(\\/(\\w))?(\\?.*)?$`),
new RegExp(
`^${this.baseUrl}\\/([^\\/?]+)(\\/(\\w+|\\d+))?(\\?.*)?$`,
),
);
if (matches) {
const name = matches[1];
Expand Down Expand Up @@ -178,7 +180,9 @@ export class SimpleRestServer implements APIServer {

// handle collections
const matches = context.url?.match(
new RegExp(`^${this.baseUrl}\\/([^\\/?]+)(\\/(\\w))?(\\?.*)?$`),
new RegExp(
`^${this.baseUrl}\\/([^\\/?]+)(\\/(\\w+|\\d+))?(\\?.*)?$`,
),
);
if (!matches) {
return { status: 404, headers: {} };
Expand Down Expand Up @@ -259,7 +263,7 @@ export class SimpleRestServer implements APIServer {
if (!this.database.getCollection(name)) {
return { status: 404, headers: {} };
}
const id = Number.parseInt(matches[3]);
const id = matches[3];
if (context.method === 'GET') {
try {
return {
Expand Down
Loading