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

Support response objects from ViewerRequest and OriginRequest #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,11 @@ function payloadVerifyResponse(payload) {
}

// confirm expected properties exist
payloadVerifyPropertyHeaders(payload,'headers');
payloadPropertyExistsString(payload,'status');
payloadPropertyExistsString(payload,'statusDescription');

// confirm optional properties have the right type:
if ('headers' in payload) payloadVerifyPropertyHeaders(payload,'headers');
if ('statusDescription' in payload) payloadPropertyExistsString(payload,'statusDescription');

// ensure `payload.status` is a valid/known HTTP status code
if (!HTTP_STATUS_CODE_DESCRIPTION.hasOwnProperty(payload.status)) {
Expand Down
22 changes: 19 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ class ViewerRequest extends lib.EdgeEventRequestBase {
}

_payloadVerify(payload) {
lib.payloadVerifyRequest(payload);
if ('status' in payload) {
// A payload with a 'status' key means the request handler yielded a
// response object (short-circuiting the origin):
lib.payloadVerifyResponse(payload);
} else {
// A payload without a 'status' key means the request handler yielded
// a request object headed for the origin:
lib.payloadVerifyRequest(payload);
}
}
}

Expand Down Expand Up @@ -70,8 +78,16 @@ class OriginRequest extends lib.EdgeEventRequestBase {
}

_payloadVerify(payload) {
lib.payloadVerifyRequest(payload);
lib.payloadVerifyRequestOrigin(payload);
if ('status' in payload) {
// A payload with a 'status' key means the request handler yielded a
// response object (short-circuiting the origin):
lib.payloadVerifyResponse(payload);
} else {
// A payload without a 'status' key means the request handler yielded
// a request object headed for the origin:
lib.payloadVerifyRequest(payload);
lib.payloadVerifyRequestOrigin(payload);
}
}
}

Expand Down
114 changes: 97 additions & 17 deletions test/main-execute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ runner.add(async function testExecuteViewerRequest() {
.setHttpMethod('POST')
.setQuerystring('?test=viewer-request-async');

const asyncResp = await vReq.execute(
// Test for ViewerRequest resulting in a _request object_ to proceed with
// to the origin:

const asyncRequestObjectResult = await vReq.execute(
buildEdgeFunctionRequestAsync(function(payload) {
payload.uri = '/test/viewer-request-async';
})
);

assert.deepEqual(asyncResp,
assert.deepEqual(asyncRequestObjectResult,
{
clientIp: '1.2.3.4',
headers: {
Expand All @@ -70,13 +73,13 @@ runner.add(async function testExecuteViewerRequest() {
);

vReq.setQuerystring('?test=viewer-request-callback');
const callbackResp = await vReq.execute(
const callbackRequestObjectResult = await vReq.execute(
buildEdgeFunctionRequestCallback(function(payload) {
payload.uri = '/test/viewer-request-callback';
})
);

assert.deepEqual(callbackResp,
assert.deepEqual(callbackRequestObjectResult,
{
clientIp: '1.2.3.4',
headers: {
Expand All @@ -92,6 +95,49 @@ runner.add(async function testExecuteViewerRequest() {
uri: '/test/viewer-request-callback',
}
);

// Test for ViewerRequest resulting in a _response object_ to short-circuit
// any request to the origin:

const asyncResponseObjectResult = await vReq.execute(
buildEdgeFunctionRequestAsync(function(payload) {
for (let k in payload) delete payload[k]
Object.assign(payload, { 'status': '302', 'statusDescription': 'Redirect' })
})
);

assert.deepEqual(asyncResponseObjectResult,
{
'status': '302',
'statusDescription': 'Redirect',
}
);

vReq.setQuerystring('?test=viewer-request-callback');
const callbackResponseObjectResult = await vReq.execute(
buildEdgeFunctionRequestCallback(function(payload) {
for (let k in payload) delete payload[k]
Object.assign(payload, { 'status': '302', 'statusDescription': 'Redirect' })
})
);

assert.deepEqual(callbackResponseObjectResult,
{
'status': '302',
'statusDescription': 'Redirect',
}
);
});

runner.add(async function testExecuteViewerRequest() {
const vReq = new main.ViewerRequest();

vReq
.setClientIp('1.2.3.4')
.addRequestHttpHeader('X-My-Header','viewer-request')
.setHttpMethod('POST')
.setQuerystring('?test=viewer-request-async');

});


Expand Down Expand Up @@ -135,13 +181,16 @@ runner.add(async function testExecuteOriginRequest() {
.setHttpMethod('POST')
.setQuerystring('?test=origin-request-async');

const asyncResp = await oReq.execute(
// Test for OriginRequest resulting in a _request object_ to proceed with
// to the origin:

const asyncRequestObjectResult = await oReq.execute(
buildEdgeFunctionRequestAsync(function(payload) {
payload.uri = '/test/origin-request-async';
})
);

assert.deepEqual(asyncResp,
assert.deepEqual(asyncRequestObjectResult,
{
clientIp: '1.2.3.4',
headers: {
Expand Down Expand Up @@ -171,13 +220,13 @@ runner.add(async function testExecuteOriginRequest() {
);

oReq.setQuerystring('?test=origin-request-callback');
const callbackResp = await oReq.execute(
const callbackRequestObjectResult = await oReq.execute(
buildEdgeFunctionRequestCallback(function(payload) {
payload.uri = '/test/origin-request-callback';
})
);

assert.deepEqual(callbackResp,
assert.deepEqual(callbackRequestObjectResult,
{
clientIp: '1.2.3.4',
headers: {
Expand Down Expand Up @@ -205,6 +254,38 @@ runner.add(async function testExecuteOriginRequest() {
uri: '/test/origin-request-callback',
}
);

// Test for OriginRequest resulting in a _response object_ to short-circuit
// any request to the origin:

const asyncResponseObjectResult = await oReq.execute(
buildEdgeFunctionRequestAsync(function(payload) {
for (let k in payload) delete payload[k]
Object.assign(payload, { 'status': '302', 'statusDescription': 'Redirect' })
})
);

assert.deepEqual(asyncResponseObjectResult,
{
'status': '302',
'statusDescription': 'Redirect',
}
);

oReq.setQuerystring('?test=origin-request-callback');
const callbackResponseObjectResult = await oReq.execute(
buildEdgeFunctionRequestCallback(function(payload) {
for (let k in payload) delete payload[k]
Object.assign(payload, { 'status': '302', 'statusDescription': 'Redirect' })
})
);

assert.deepEqual(callbackResponseObjectResult,
{
'status': '302',
'statusDescription': 'Redirect',
}
);
});


Expand Down Expand Up @@ -245,13 +326,13 @@ runner.add(async function testExecuteOriginResponse() {
.addResponseHttpHeader('X-My-Header','origin-response')
.setResponseHttpStatusCode(304);

const asyncResp = await oRsp.execute(
const asyncResult = await oRsp.execute(
buildEdgeFunctionResponseAsync(function(payload) {
payload.statusDescription = 'Mutated async';
})
);

assert.deepEqual(asyncResp,
assert.deepEqual(asyncResult,
{
headers: {
'x-my-header': [
Expand All @@ -266,13 +347,13 @@ runner.add(async function testExecuteOriginResponse() {
}
);

const callbackResp = await oRsp.execute(
const callbackResult = await oRsp.execute(
buildEdgeFunctionResponseCallback(function(payload) {
payload.statusDescription = 'Mutated callback';
})
);

assert.deepEqual(callbackResp,
assert.deepEqual(callbackResult,
{
headers: {
'x-my-header': [
Expand Down Expand Up @@ -326,13 +407,13 @@ runner.add(async function testExecuteViewerResponse() {
.addResponseHttpHeader('X-My-Header','viewer-response')
.setResponseHttpStatusCode(304);

const asyncResp = await vRsp.execute(
const asyncResult = await vRsp.execute(
buildEdgeFunctionResponseAsync(function(payload) {
payload.statusDescription = 'Mutated async';
})
);

assert.deepEqual(asyncResp,
assert.deepEqual(asyncResult,
{
headers: {
'x-my-header': [
Expand All @@ -347,13 +428,13 @@ runner.add(async function testExecuteViewerResponse() {
}
);

const callbackResp = await vRsp.execute(
const callbackResult = await vRsp.execute(
buildEdgeFunctionResponseCallback(function(payload) {
payload.statusDescription = 'Mutated callback';
})
);

assert.deepEqual(callbackResp,
assert.deepEqual(callbackResult,
{
headers: {
'x-my-header': [
Expand Down Expand Up @@ -434,5 +515,4 @@ function buildEdgeFunctionResponseCallback(mutate) {
};
}


runner.execute();
4 changes: 2 additions & 2 deletions test/main-payloadverify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ function testPayloadVerifyResponse(inst) {
assert.throws(function() { callVerify(undefined); });

// test: payload missing/invalid property values
assert.throws(function() {
assert.doesNotThrow(function() {
callVerify(makePayload(function(payload) {
delete payload.headers;
}));
Expand Down Expand Up @@ -949,7 +949,7 @@ function testPayloadVerifyResponse(inst) {
}));
});

assert.throws(function() {
assert.doesNotThrow(function() {
callVerify(makePayload(function(payload) {
delete payload.statusDescription;
}));
Expand Down