forked from ipfs/gateway-conformance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrustless_gateway_raw_test.go
159 lines (148 loc) · 4.66 KB
/
trustless_gateway_raw_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package tests
import (
"strconv"
"strings"
"testing"
"github.com/ipfs/gateway-conformance/tooling/helpers"
"github.com/ipfs/gateway-conformance/tooling"
"github.com/ipfs/gateway-conformance/tooling/car"
"github.com/ipfs/gateway-conformance/tooling/specs"
. "github.com/ipfs/gateway-conformance/tooling/test"
)
func TestTrustlessRaw(t *testing.T) {
tooling.LogTestGroup(t, GroupBlockCar)
tooling.LogSpecs(t, "https://specs.ipfs.tech/http-gateways/trustless-gateway/#block-responses-application-vnd-ipld-raw")
fixture := car.MustOpenUnixfsCar("gateway-raw-block.car")
tests := SugarTests{
{
Name: "GET with format=raw param returns a raw block",
Request: Request().
Path("/ipfs/{{cid}}", fixture.MustGetCid("dir")).
Query("format", "raw"),
Response: Expect().
Status(200).
Body(fixture.MustGetRawData("dir")),
},
{
Name: "GET with application/vnd.ipld.raw header returns a raw block",
Request: Request().
Path("/ipfs/{{cid}}", fixture.MustGetCid("dir")).
Headers(
Header("Accept", "application/vnd.ipld.raw"),
),
Response: Expect().
Status(200).
Body(fixture.MustGetRawData("dir")),
},
{
Name: "GET with application/vnd.ipld.raw header returns expected response headers",
Request: Request().
Path("/ipfs/{{cid}}", fixture.MustGetCid("dir", "ascii.txt")).
Headers(
Header("Accept", "application/vnd.ipld.raw"),
),
Response: Expect().
Status(200).
Headers(
Header("Content-Type").
Equals("application/vnd.ipld.raw"),
Header("Content-Length").
Equals("{{length}}", len(fixture.MustGetRawData("dir", "ascii.txt"))),
Header("Content-Disposition").
Contains("attachment;"),
Header("X-Content-Type-Options").
Equals("nosniff"),
).
Body(fixture.MustGetRawData("dir", "ascii.txt")),
},
{
Name: "GET with application/vnd.ipld.raw header and filename param returns expected Content-Disposition header with custom filename",
Request: Request().
Path("/ipfs/{{cid}}?filename=foobar.bin", fixture.MustGetCid("dir", "ascii.txt")).
Headers(
Header("Accept", "application/vnd.ipld.raw"),
),
Response: Expect().
Status(200).
Headers(
Header("Content-Disposition").
Contains(`attachment;`),
Header("Content-Disposition").
Contains(`filename="foobar.bin`),
),
},
{
Name: "GET with application/vnd.ipld.raw header returns expected caching headers",
Request: Request().
Path("/ipfs/{{cid}}", fixture.MustGetCid("dir", "ascii.txt")).
Headers(
Header("Accept", "application/vnd.ipld.raw"),
),
Response: Expect().
Status(200).
Headers(
Header("Etag").
Exists(),
Header("X-IPFS-Path").
Equals("/ipfs/{{cid}}", fixture.MustGetCid("dir", "ascii.txt")),
Header("X-IPFS-Roots").
Contains(fixture.MustGetCid("dir", "ascii.txt")),
Header("Cache-Control").
Hint("It should be public, immutable and have max-age of at least 29030400.").
Checks(func(v string) bool {
directives := strings.Split(strings.ReplaceAll(v, " ", ""), ",")
dir := make(map[string]string)
for _, directive := range directives {
parts := strings.Split(directive, "=")
if len(parts) == 2 {
dir[parts[0]] = parts[1]
} else {
dir[parts[0]] = ""
}
}
if _, ok := dir["public"]; !ok {
return false
}
if _, ok := dir["immutable"]; !ok {
return false
}
if maxAge, ok := dir["max-age"]; ok {
maxAgeInt, err := strconv.Atoi(maxAge)
if err != nil {
return false
}
if maxAgeInt < 29030400 {
return false
}
} else {
return false
}
return true
}),
),
},
}
RunWithSpecs(t, tests, specs.TrustlessGatewayRaw)
}
func TestTrustlessRawRanges(t *testing.T) {
tooling.LogTestGroup(t, GroupBlockCar)
// @lidel: "The optional entity-bytes=from:to parameter is available only for CAR requests."
// Multi-range requests MUST conform to the HTTP semantics. The server does not
// need to be able to support returning multiple ranges. However, it must respond
// correctly.
fixture := car.MustOpenUnixfsCar("gateway-raw-block.car")
tests := helpers.OnlyRandomRangeTests(t,
SugarTest{
Name: "GET with application/vnd.ipld.raw with range request includes correct bytes",
Request: Request().
Path("/ipfs/{{cid}}", fixture.MustGetCid("dir", "ascii.txt")).
Headers(
Header("Accept", "application/vnd.ipld.raw"),
),
Response: Expect(),
},
fixture.MustGetRawData("dir", "ascii.txt"),
"application/vnd.ipld.raw",
)
RunWithSpecs(t, tests, specs.TrustlessGatewayRaw)
}