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

[FEATURE] Replay last request #713

Merged
merged 1 commit into from Jun 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/index.js
Expand Up @@ -236,6 +236,7 @@ class Offline {
_buildServer() {
// Maps a request id to the request's state (done: bool, timeout: timer)
this.requests = {};
this.lastRequestOptions = null;

// Methods
this._setOptions(); // Will create meaningful options from cli options
Expand Down Expand Up @@ -541,6 +542,18 @@ class Offline {
path: fullPath,
config: routeConfig,
handler: (request, h) => { // Here we go
// Store current request as the last one
this.lastRequestOptions = {
method: request.method,
url: request.url.href,
headers: request.headers,
payload: request.payload,
};

if (request.auth.credentials && request.auth.strategy) {
this.lastRequestOptions.auth = request.auth;
}

// Payload processing
const encoding = detectEncoding(request);

Expand Down Expand Up @@ -1128,6 +1141,16 @@ class Offline {

this.printBlankLine();
this.serverlessLog(`Offline listening on http${this.options.httpsProtocol ? 's' : ''}://${this.options.host}:${this.options.port}`);
this.serverlessLog('Enter "rp" to replay the last request');

process.openStdin().addListener('data', data => {
// note: data is an object, and when converted to a string it will
// end with a linefeed. so we (rather crudely) account for that
// with toString() and then trim()
if (data.toString().trim() === 'rp') {
this._injectLastRequest();
}
});

return this.server;
}
Expand All @@ -1139,6 +1162,16 @@ class Offline {
.then(() => process.exit(this.exitCode));
}

_injectLastRequest() {
if (this.lastRequestOptions) {
this.serverlessLog('Replaying last request');
this.server.inject(this.lastRequestOptions);
}
else {
this.serverlessLog('No last request to replay!');
}
}

// Bad news
_replyError(responseCode, response, message, err) {
const stackTrace = this._getArrayStackTrace(err.stack);
Expand Down