gateway: fix cors headers tests
License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Jan 4, 2019 at 13:18 UTC
b15cf0198f177be7c46813b81c4b31eb74001aaa
1 file changed
+42
-13
test/sharness/t0112-gateway-cors.sh
+42
-13
@@ -18,41 +18,70 @@ test_init_ipfs
18
test_config_ipfs_cors_headers
19
test_launch_ipfs_daemon
20
21
-gwport=$GWAY_PORT
22
-apiport=$API_PORT
21
thash='QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
22
23
# Gateway
24
25
# HTTP GET Request
26
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
29
'
30
32
-cat curl_output
31
# GET Response from Gateway should contain CORS headers
32
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
37
'
38
39
# HTTP OPTIONS Request
40
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
43
'
44
+
45
# OPTION Response from Gateway should contain CORS headers
46
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
78
'
79
80
# Read-Only API (at the Gateway Port)
81
82
# HTTP GET Request
83
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
85
'
86
# GET Response from the API should NOT contain CORS headers
87
# Blacklisting: https://git.io/vzaj2
@@ -63,7 +92,7 @@ test_expect_success "OPTIONS response for API looks good" '
92
93
# HTTP OPTIONS Request
94
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
96
'
97
# OPTIONS Response from the API should NOT contain CORS headers
98
test_expect_success "OPTIONS response for API looks good" '