Skip to content

Commit

Permalink
fix(conditions): relax stickyness rules
Browse files Browse the repository at this point in the history
  • Loading branch information
dominique-pfister committed Feb 25, 2020
1 parent 8a9bbe0 commit 90cfe49
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ class BooleanCondition {
toJSON() {
return this._entry.jsonGen(this._items);
}

sticky() {
const items = Array.isArray(this._items) ? this._items : [this._items];
return items.some((item) => item.sticky());
}
}

/**
Expand Down Expand Up @@ -212,6 +217,10 @@ class PropertyCondition {
return json;
}

sticky() {
return !!this._prop.sticky;
}

evaluate(req) {
if (!this._prop.evaluate) {
return true;
Expand Down Expand Up @@ -316,6 +325,7 @@ const propertyMap = {
evaluate: (req) => req.get('referer'),
type: 'string',
allowed_ops: '=~',
sticky: true,
},
client_city: {
vcl: 'client.geo.city',
Expand Down Expand Up @@ -400,6 +410,7 @@ const propertyMap = {
},
evaluate: (req, property) => req.params[property.name],
allowed_ops: '~<=>',
sticky: true,
},
};

Expand Down Expand Up @@ -443,7 +454,6 @@ class Condition {
return '';
}

/* eslint-disable no-underscore-dangle */
match(req) {
if (this._top && this._top.evaluate) {
req.headers = req.headers || {};
Expand All @@ -456,6 +466,13 @@ class Condition {
return true;
}

sticky() {
if (this._top && this._top.sticky) {
return this._top.sticky();
}
return false;
}

toJSON(opts) {
const json = this._top ? this._top.toJSON() : null;
if (json && opts && opts.minimal) {
Expand Down
3 changes: 3 additions & 0 deletions test/condition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ set req.url = regsub(req.url, "^/oldpath", "${subpath}");
if (cfg.json !== undefined) {
assert.equal(cfg.json, cond.toJSON());
}
const sticky = cfg.sticky || false;
assert.equal(cond.sticky(), sticky);

const actual = cond.toJSON();
const expected = cfg.condition;
assert.deepEqual(actual, expected);
Expand Down
1 change: 1 addition & 0 deletions test/specs/conditions/null.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ condition:

vcl: ''
vcl_path: ''
sticky: false

samples:
- match: true
Expand Down
1 change: 1 addition & 0 deletions test/specs/conditions/referer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ condition:
referer=: www.example.com

vcl: 'req.http.referer == "www.example.com"'
sticky: true

samples:
- headers:
Expand Down
1 change: 1 addition & 0 deletions test/specs/conditions/url_param_number.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ condition:
- url_param.a<: 10

vcl: '(std.atoi(subfield(req.url.qs, "a", "&")) > 5 && std.atoi(subfield(req.url.qs, "a", "&")) < 10)'
sticky: true

samples:
- uri: https://www.example.com/index.html?a=7
Expand Down
1 change: 1 addition & 0 deletions test/specs/conditions/url_param_string.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ condition:
url_param.foo: bar

vcl: 'subfield(req.url.qs, "foo", "&") ~ "^bar"'
sticky: true

samples:
- uri: https://www.example.com/index.html?foo=bar
Expand Down

0 comments on commit 90cfe49

Please sign in to comment.