Skip to content

Commit

Permalink
Merge pull request #79 from marmelab/fix-id-detection-and-parsing
Browse files Browse the repository at this point in the history
Fix identifiers parsing
  • Loading branch information
fzaninotto authored Jun 17, 2024
2 parents b83cb68 + 47fb2e1 commit 77ff0ac
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 3 deletions.
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

0 comments on commit 77ff0ac

Please sign in to comment.