Skip to content

Commit b15cf01

Browse files
committed
gateway: fix cors headers tests
License: MIT Signed-off-by: Steven Allen <[email protected]>
1 parent 4bbf4cc commit b15cf01

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

test/sharness/t0112-gateway-cors.sh

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,70 @@ test_init_ipfs
1818
test_config_ipfs_cors_headers
1919
test_launch_ipfs_daemon
2020

21-
gwport=$GWAY_PORT
22-
apiport=$API_PORT
2321
thash='QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
2422

2523
# Gateway
2624

2725
# HTTP GET Request
2826
test_expect_success "GET to Gateway succeeds" '
29-
curl -svX GET "http://127.0.0.1:$gwport/ipfs/$thash" 2>curl_output
27+
curl -svX GET "http://127.0.0.1:$GWAY_PORT/ipfs/$thash" >/dev/null 2>curl_output &&
28+
cat curl_output
3029
'
3130

32-
cat curl_output
3331
# GET Response from Gateway should contain CORS headers
3432
test_expect_success "GET response for Gateway resource looks good" '
35-
grep "Access-Control-Allow-Origin:" curl_output | grep "\*" &&
36-
grep "Access-Control-Allow-Methods:" curl_output | grep " GET\b" &&
37-
grep "Access-Control-Allow-Headers:" curl_output
33+
grep "< Access-Control-Allow-Origin: \*" curl_output &&
34+
grep "< Access-Control-Allow-Methods: GET" curl_output &&
35+
grep "< Access-Control-Allow-Headers: Range" curl_output &&
36+
grep "< Access-Control-Expose-Headers: Content-Range" curl_output
3837
'
3938

4039
# HTTP OPTIONS Request
4140
test_expect_success "OPTIONS to Gateway succeeds" '
42-
curl -svX OPTIONS "http://127.0.0.1:$gwport/ipfs/$thash" 2>curl_output
41+
curl -svX OPTIONS "http://127.0.0.1:$GWAY_PORT/ipfs/$thash" 2>curl_output &&
42+
cat curl_output
4343
'
44+
4445
# OPTION Response from Gateway should contain CORS headers
4546
test_expect_success "OPTIONS response for Gateway resource looks good" '
46-
grep "Access-Control-Allow-Origin:" curl_output | grep "\*" &&
47-
grep "Access-Control-Allow-Methods:" curl_output | grep " GET\b" &&
48-
grep "Access-Control-Allow-Headers:" curl_output
47+
grep "< Access-Control-Allow-Origin: \*" curl_output &&
48+
grep "< Access-Control-Allow-Methods: GET" curl_output &&
49+
grep "< Access-Control-Allow-Headers: Range" curl_output &&
50+
grep "< Access-Control-Expose-Headers: Content-Range" curl_output
51+
'
52+
53+
test_kill_ipfs_daemon
54+
55+
# Change headers
56+
test_expect_success "Can configure gateway headers" '
57+
ipfs config --json Gateway.HTTPHeaders.Access-Control-Allow-Headers "[\"X-Custom1\"]" &&
58+
ipfs config --json Gateway.HTTPHeaders.Access-Control-Expose-Headers "[\"X-Custom2\"]" &&
59+
ipfs config --json Gateway.HTTPHeaders.Access-Control-Allow-Origin "[\"localhost\"]"
60+
'
61+
62+
test_launch_ipfs_daemon
63+
64+
test_expect_success "OPTIONS to Gateway succeeds" '
65+
curl -svX OPTIONS "http://127.0.0.1:$GWAY_PORT/ipfs/$thash" 2>curl_output &&
66+
cat curl_output
67+
'
68+
69+
test_expect_success "Access-Control-Allow-Headers extends" '
70+
grep "< Access-Control-Allow-Headers: Range" curl_output &&
71+
grep "< Access-Control-Allow-Headers: X-Custom1" curl_output &&
72+
grep "< Access-Control-Expose-Headers: Content-Range" curl_output &&
73+
grep "< Access-Control-Expose-Headers: X-Custom2" curl_output
74+
'
75+
76+
test_expect_success "Access-Control-Allow-Origin replaces" '
77+
grep "< Access-Control-Allow-Origin: localhost" curl_output
4978
'
5079

5180
# Read-Only API (at the Gateway Port)
5281

5382
# HTTP GET Request
5483
test_expect_success "GET to API succeeds" '
55-
curl -svX GET "http://127.0.0.1:$gwport/api/v0/cat?arg=$thash" 2>curl_output
84+
curl -svX GET "http://127.0.0.1:$GWAY_PORT/api/v0/cat?arg=$thash" >/dev/null 2>curl_output
5685
'
5786
# GET Response from the API should NOT contain CORS headers
5887
# Blacklisting: https://git.io/vzaj2
@@ -63,7 +92,7 @@ test_expect_success "OPTIONS response for API looks good" '
6392

6493
# HTTP OPTIONS Request
6594
test_expect_success "OPTIONS to API succeeds" '
66-
curl -svX OPTIONS "http://127.0.0.1:$gwport/api/v0/cat?arg=$thash" 2>curl_output
95+
curl -svX OPTIONS "http://127.0.0.1:$GWAY_PORT/api/v0/cat?arg=$thash" 2>curl_output
6796
'
6897
# OPTIONS Response from the API should NOT contain CORS headers
6998
test_expect_success "OPTIONS response for API looks good" '

0 commit comments

Comments
 (0)