Skip to content

Commit

Permalink
wp-now: add /wp-admin redirection with trailing slash (#60)
Browse files Browse the repository at this point in the history
Add a middleware to redirect to `/wp-admin` to add the trailing slash `/wp-admin/`. Fixes core sidebar links.
  • Loading branch information
sejas authored Jun 2, 2023
1 parent 2404f5c commit 4a0ea12
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/wp-now/src/add-trailing-slash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Adds redirection adding a trailing slash, when a request matches a given path.
* @param path - The path to add a trailing slash to. E.g. '/wp-admin'
* @returns - Returns a middleware function that may redirect adding a trailing slash to the given path. E.g. '/wp-admin/'
*/
export function addTrailingSlash(path) {
return (req, res, next) => {
const urlParts = req.url.split('?');
const url = urlParts[0];
const queryString = req.url.substr(url.length);
if (url === path) {
res.redirect(301, `${path}/${queryString}`);
} else {
next();
}
};
}
2 changes: 2 additions & 0 deletions packages/wp-now/src/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { portFinder } from './port-finder';
import { NodePHP } from '@php-wasm/node';
import startWPNow from './wp-now';
import { output } from './output';
import { addTrailingSlash } from './add-trailing-slash';

function requestBodyToMultipartFormData(json, boundary) {
let multipartData = '';
Expand Down Expand Up @@ -49,6 +50,7 @@ export async function startServer(
}
const app = express();
app.use(fileUpload());
app.use(addTrailingSlash('/wp-admin'));
const port = await portFinder.getOpenPort();
const { php, options: wpNowOptions } = await startWPNow(options);

Expand Down
34 changes: 34 additions & 0 deletions packages/wp-now/src/tests/add-trailing-slash.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { addTrailingSlash } from '../add-trailing-slash';

describe('add trailing slash middleware', () => {
const middlewareTrailingSlash = addTrailingSlash('/wp-admin');
let res, next;

beforeEach(() => {
res = {
redirect: vi.fn(),
};
next = vi.fn();
});

test('adds a trailing slash to the given path', () => {
const req = { url: '/wp-admin' };
middlewareTrailingSlash(req, res, next);
expect(res.redirect).toHaveBeenCalledWith(301, '/wp-admin/');
expect(next).not.toHaveBeenCalled();
});

test('adds a trailing slash to the given path with parameters', () => {
const req = { url: '/wp-admin?foo=bar' };
middlewareTrailingSlash(req, res, next);
expect(res.redirect).toHaveBeenCalledWith(301, '/wp-admin/?foo=bar');
expect(next).not.toHaveBeenCalled();
});

test('does not add a trailing slash to the given path', () => {
const req = { url: '/wp-admin/' };
middlewareTrailingSlash(req, res, next);
expect(res.redirect).not.toHaveBeenCalled();
expect(next).toHaveBeenCalled();
});
});

0 comments on commit 4a0ea12

Please sign in to comment.