Skip to content

Commit

Permalink
fix: pass own logger in historyApiFallback (#3373)
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed Jun 3, 2021
1 parent e3f366f commit 3ba2fa5
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/Server.js
Expand Up @@ -251,13 +251,22 @@ class Server {
setupHistoryApiFallbackFeature() {
const historyApiFallback = require('connect-history-api-fallback');

const fallback =
typeof this.options.historyApiFallback === 'object'
const options =
typeof this.options.historyApiFallback !== 'boolean'
? this.options.historyApiFallback
: null;
: {};

let logger;

if (typeof options.verbose === 'undefined') {
logger = this.logger.log.bind(
this.logger,
'[connect-history-api-fallback]'
);
}

// Fall back to /index.html if nothing else matches.
this.app.use(historyApiFallback(fallback));
this.app.use(historyApiFallback({ logger, ...options }));
}

setupStaticFeature() {
Expand Down
96 changes: 96 additions & 0 deletions test/server/historyApiFallback-option.test.js
Expand Up @@ -178,6 +178,102 @@ describe('historyApiFallback option', () => {
});
});

describe('as object with the "verbose" option', () => {
let consoleSpy;

beforeAll((done) => {
consoleSpy = jest.spyOn(global.console, 'log');

server = testServer.start(
config,
{
historyApiFallback: {
index: '/bar.html',
verbose: true,
},
port,
},
done
);
req = request(server.app);
});

afterAll(() => {
consoleSpy.mockRestore();
});

it('request to directory and log', (done) => {
req
.get('/foo')
.accept('html')
.expect(200, /Foobar/, (error) => {
if (error) {
done(error);

return;
}

expect(consoleSpy).toHaveBeenCalledWith(
'Rewriting',
'GET',
'/foo',
'to',
'/bar.html'
);

done();
});
});
});

describe('as object with the "logger" option', () => {
let consoleSpy;

beforeAll((done) => {
consoleSpy = jest.spyOn(global.console, 'log');

server = testServer.start(
config,
{
historyApiFallback: {
index: '/bar.html',
logger: consoleSpy,
},
port,
},
done
);
req = request(server.app);
});

afterAll(() => {
consoleSpy.mockRestore();
});

it('request to directory and log', (done) => {
req
.get('/foo')
.accept('html')
.expect(200, /Foobar/, (error) => {
if (error) {
done(error);

return;
}

expect(consoleSpy).toHaveBeenCalledWith(
'Rewriting',
'GET',
'/foo',
'to',
'/bar.html'
);

done();
});
});
});

describe('in-memory files', () => {
beforeAll((done) => {
server = testServer.start(
Expand Down

0 comments on commit 3ba2fa5

Please sign in to comment.