Skip to content

Commit 6ae9d24

Browse files
authored
Merge pull request #66 from elgentos/graphql
#17 makes graphql conform to default Magento and creates tests
2 parents a8cb65c + 40d6e8d commit 6ae9d24

File tree

3 files changed

+192
-4
lines changed

3 files changed

+192
-4
lines changed

.github/workflows/vcl_tests.yml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,24 @@ jobs:
2020
FILES=$(ls tests/varnish/*.vtc | jq -R -s -c 'split("\n")[:-1]')
2121
echo "matrix=${FILES}" >> $GITHUB_OUTPUT
2222
23+
prepare-cache:
24+
runs-on: ubuntu-latest
25+
steps:
26+
- name: Check Docker image cache
27+
id: cache-check
28+
uses: actions/cache@v3
29+
with:
30+
path: /tmp/varnish.tar
31+
key: docker-varnish
32+
33+
- name: Pull and cache Docker image
34+
if: steps.cache-check.outputs.cache-hit != 'true'
35+
run: |
36+
docker pull varnish:fresh
37+
docker save varnish:fresh > /tmp/varnish.tar
38+
2339
test:
24-
needs: get-test-files
40+
needs: [get-test-files, prepare-cache]
2541
runs-on: ubuntu-latest
2642
strategy:
2743
fail-fast: false
@@ -31,8 +47,17 @@ jobs:
3147
steps:
3248
- uses: actions/checkout@v4
3349

50+
- name: Load cached Docker image
51+
uses: actions/cache@v3
52+
with:
53+
path: /tmp/varnish.tar
54+
key: docker-varnish
55+
56+
- name: Load Docker image
57+
run: docker load < /tmp/varnish.tar
58+
3459
- name: Run Varnish test
3560
working-directory: tests/varnish
3661
run: |
3762
test_file=$(basename ${{ matrix.test_file }})
38-
make test_single TEST=${test_file}
63+
make test_single TEST=${test_file}

etc/varnish6.vcl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ sub vcl_recv {
184184
{{/if}}
185185
}
186186

187-
# Don't cache the authenticated GraphQL requests
188-
if (req.url ~ "/graphql" && req.http.Authorization ~ "^Bearer") {
187+
# Bypass authenticated GraphQL requests without a X-Magento-Cache-Id
188+
if (req.url ~ "/graphql" && !req.http.X-Magento-Cache-Id && req.http.Authorization ~ "^Bearer") {
189189
return (pass);
190190
}
191191

tests/varnish/graphql.vtc

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
varnishtest "GraphQL X-Magento-Cache-Id validation & cache bypassing"
2+
3+
server s1 {
4+
# first request will be the probe, handle it and be on our way
5+
rxreq
6+
expect req.url == "/health_check.php"
7+
txresp
8+
9+
# the probe expects the connection to close
10+
close
11+
accept
12+
13+
# 1. First MISS
14+
rxreq
15+
expect req.url == "/graphql"
16+
expect req.method == "GET"
17+
expect req.http.X-Magento-Cache-Id == <undef>
18+
txresp
19+
20+
# 3. UNCACHEABLE (Cache-Id mismatch)
21+
rxreq
22+
expect req.url == "/graphql"
23+
expect req.method == "GET"
24+
expect req.http.X-Magento-Cache-Id == "1234"
25+
txresp -hdr "X-Magento-Cache-Id: notmatching"
26+
27+
# 4. MISS with Cache-Id
28+
rxreq
29+
expect req.url == "/graphql"
30+
expect req.method == "GET"
31+
expect req.http.X-Magento-Cache-Id == "12345"
32+
txresp -hdr "X-Magento-Cache-Id: 12345"
33+
34+
# 6. MISS with different Cache-Id
35+
rxreq
36+
expect req.url == "/graphql"
37+
expect req.method == "GET"
38+
expect req.http.X-Magento-Cache-Id == "12346"
39+
txresp -hdr "X-Magento-Cache-Id: 12346"
40+
41+
# 7. MISS with Store header
42+
rxreq
43+
expect req.url == "/graphql"
44+
expect req.method == "GET"
45+
expect req.http.X-Magento-Cache-Id == "12345"
46+
expect req.http.Store == "1"
47+
txresp -hdr "X-Magento-Cache-Id: 12345" -hdr "Store: 1"
48+
49+
# 8. MISS with Store and Currency
50+
rxreq
51+
expect req.url == "/graphql"
52+
expect req.method == "GET"
53+
expect req.http.X-Magento-Cache-Id == "12345"
54+
expect req.http.Store == "1"
55+
expect req.http.Content-Currency == "1"
56+
txresp -hdr "X-Magento-Cache-Id: 12345" -hdr "Store: 1" -hdr "Content-Currency: 1"
57+
58+
# 9. UNCACHEABLE with Authorization
59+
rxreq
60+
expect req.url == "/graphql"
61+
expect req.method == "GET"
62+
expect req.http.X-Magento-Cache-Id == <undef>
63+
expect req.http.Authorization == "Bearer 12345"
64+
txresp
65+
66+
# 10. MISS with Authorization and Cache-Id
67+
rxreq
68+
expect req.url == "/graphql"
69+
expect req.method == "GET"
70+
expect req.http.X-Magento-Cache-Id == "12345"
71+
expect req.http.Authorization == "Bearer 12345"
72+
txresp -hdr "X-Magento-Cache-Id: 12345"
73+
} -start
74+
75+
# Generate the VCL file based on included variables and write it to output.vcl
76+
shell {
77+
export s1_addr="${s1_addr}"
78+
export s1_port="${s1_port}"
79+
${testdir}/helpers/parse_vcl.pl "${testdir}/../../etc/varnish6.vcl" "${tmpdir}/output.vcl"
80+
}
81+
82+
varnish v1 -arg "-f" -arg "${tmpdir}/output.vcl" -arg "-p" -arg "vsl_mask=+Hash" -start
83+
84+
# make sure the probe request fired
85+
delay 1
86+
87+
client c1 {
88+
txreq -method "GET" -url "/graphql"
89+
rxresp
90+
expect resp.http.X-Magento-Cache-Debug == "MISS"
91+
92+
txreq -method "GET" -url "/graphql"
93+
rxresp
94+
expect resp.http.X-Magento-Cache-Debug == "HIT"
95+
96+
txreq -method "GET" -url "/graphql" \
97+
-hdr "X-Magento-Cache-Id: 1234"
98+
rxresp
99+
expect resp.http.X-Magento-Cache-Debug == "UNCACHEABLE"
100+
101+
txreq -method "GET" -url "/graphql" \
102+
-hdr "X-Magento-Cache-Id: 12345"
103+
rxresp
104+
expect resp.http.X-Magento-Cache-Debug == "MISS"
105+
106+
txreq -method "GET" -url "/graphql" \
107+
-hdr "X-Magento-Cache-Id: 12345"
108+
rxresp
109+
expect resp.http.X-Magento-Cache-Debug == "HIT"
110+
111+
txreq -method "GET" -url "/graphql" \
112+
-hdr "X-Magento-Cache-Id: 12346"
113+
rxresp
114+
expect resp.http.X-Magento-Cache-Debug == "MISS"
115+
116+
txreq -method "GET" -url "/graphql" \
117+
-hdr "X-Magento-Cache-Id: 12346"
118+
rxresp
119+
expect resp.http.X-Magento-Cache-Debug == "HIT"
120+
121+
txreq -method "GET" -url "/graphql" \
122+
-hdr "X-Magento-Cache-Id: 12345" \
123+
-hdr "Store: 1"
124+
rxresp
125+
expect resp.http.X-Magento-Cache-Debug == "MISS"
126+
127+
txreq -method "GET" -url "/graphql" \
128+
-hdr "X-Magento-Cache-Id: 12345" \
129+
-hdr "Store: 1"
130+
rxresp
131+
expect resp.http.X-Magento-Cache-Debug == "HIT"
132+
133+
txreq -method "GET" -url "/graphql" \
134+
-hdr "X-Magento-Cache-Id: 12345" \
135+
-hdr "Store: 1" \
136+
-hdr "Content-Currency: 1"
137+
rxresp
138+
expect resp.http.X-Magento-Cache-Debug == "MISS"
139+
140+
txreq -method "GET" -url "/graphql" \
141+
-hdr "X-Magento-Cache-Id: 12345" \
142+
-hdr "Store: 1" \
143+
-hdr "Content-Currency: 1"
144+
rxresp
145+
expect resp.http.X-Magento-Cache-Debug == "HIT"
146+
147+
txreq -method "GET" -url "/graphql" \
148+
-hdr "Authorization: Bearer 12345"
149+
rxresp
150+
expect resp.http.X-Magento-Cache-Debug == "UNCACHEABLE"
151+
152+
txreq -method "GET" -url "/graphql" \
153+
-hdr "Authorization: Bearer 12345" \
154+
-hdr "X-Magento-Cache-Id: 12345"
155+
rxresp
156+
expect resp.http.X-Magento-Cache-Debug == "MISS"
157+
158+
txreq -method "GET" -url "/graphql" \
159+
-hdr "Authorization: Bearer 12345" \
160+
-hdr "X-Magento-Cache-Id: 12345"
161+
rxresp
162+
expect resp.http.X-Magento-Cache-Debug == "HIT"
163+
} -run

0 commit comments

Comments
 (0)