return 206 status for partial sendfile responses
[rainbows.git] / t / byte-range-common.sh
blob514a024b628871ecd8e9b5bf0725ee1a03636c3d
1 t_begin "byte-range setup vars" && {
2 random_blob_size=$(wc -c < random_blob)
3 rb_1=$(( $random_blob_size - 1 ))
4 range_head=-r-365
5 range_tail=-r155-
6 range_mid=-r200-300
7 range_n1=-r0-$rb_1
8 range_n2=-r0-$(($rb_1 - 1))
9 range_1b_head=-r0-0
10 range_1b_tail=-r$rb_1-$rb_1
11 range_1b_mid=-r200-200
12 range_all=-r0-$random_blob_size
13 url=http://$listen/random_blob
16 check_content_range () {
17 grep '^< HTTP/1\.1 206 Partial Content' $err
18 grep 'Range:' $err
19 # Content-Range: bytes #{offset}-#{offset+count-1}/#{clen}
20 d='\([0-9]\+\)'
21 start= end= size=
22 eval $(< $err sed -n -e \
23 "s/^< Content-Range: bytes $d-$d\/$d"'.*$/start=\1 end=\2 size=\3/p')
24 test -n "$start"
25 test -n "$end"
26 test -n "$size"
28 # ensure we didn't screw up the sed invocation
29 expect="< Content-Range: bytes $start-$end/$size"
30 test x"$(grep -F "$expect" $err)" = x"$(grep '^< Content-Range:' $err)"
32 test $start -le $end
33 test $end -lt $size
36 t_begin "read random blob sha1s" && {
37 sha1_head=$(curl -sSff $range_head file://random_blob | rsha1)
38 sha1_tail=$(curl -sSff $range_tail file://random_blob | rsha1)
39 sha1_mid=$(curl -sSff $range_mid file://random_blob | rsha1)
40 sha1_n1=$(curl -sSff $range_n1 file://random_blob | rsha1)
41 sha1_n2=$(curl -sSff $range_n2 file://random_blob | rsha1)
42 sha1_1b_head=$(curl -sSff $range_1b_head file://random_blob | rsha1)
43 sha1_1b_tail=$(curl -sSff $range_1b_tail file://random_blob | rsha1)
44 sha1_1b_mid=$(curl -sSff $range_1b_mid file://random_blob | rsha1)
45 sha1_all=$(rsha1 < random_blob)
46 echo "$sha1_all=$sha1_n1"
49 t_begin "normal full request matches" && {
50 sha1="$(curl -v 2>$err -sSf $url | rsha1)"
51 test x"$sha1_all" = x"$sha1"
52 grep 'Content-Range:' $err && die "Content-Range unexpected"
53 grep 'HTTP/1.1 200 OK' $err || die "200 response expected"
56 t_begin "crazy offset goes over" && {
57 range_insane=-r$(($random_blob_size * 2))-$(($random_blob_size * 4))
58 curl -vsS 2>$err $range_insane $url
59 grep 'HTTP/1\.[01] 416 ' $err || die "expected 416 error"
62 t_begin "full request matches with explicit ranges" && {
63 sha1="$(curl -v 2>$err $range_all -sSf $url | rsha1)"
64 check_content_range
65 test x"$sha1_all" = x"$sha1"
67 sha1="$(curl -v 2>$err $range_n1 -sSf $url | rsha1)"
68 check_content_range
69 test x"$sha1_all" = x"$sha1"
71 range_over=-r0-$(($random_blob_size * 2))
72 sha1="$(curl -v 2>$err $range_over -sSf $url | rsha1)"
73 check_content_range
74 test x"$sha1_all" = x"$sha1"
77 t_begin "no fence post errors" && {
78 sha1="$(curl -v 2>$err $range_n2 -sSf $url | rsha1)"
79 check_content_range
80 test x"$sha1_n2" = x"$sha1"
82 sha1="$(curl -v 2>$err $range_1b_head -sSf $url | rsha1)"
83 check_content_range
84 test x"$sha1_1b_head" = x"$sha1"
86 sha1="$(curl -v 2>$err $range_1b_tail -sSf $url | rsha1)"
87 check_content_range
88 test x"$sha1_1b_tail" = x"$sha1"
90 sha1="$(curl -v 2>$err $range_1b_mid -sSf $url | rsha1)"
91 check_content_range
92 test x"$sha1_1b_mid" = x"$sha1"
95 t_begin "head range matches" && {
96 sha1="$(curl -sSfv 2>$err $range_head $url | rsha1)"
97 check_content_range
98 test x"$sha1_head" = x"$sha1"
101 t_begin "tail range matches" && {
102 sha1="$(curl -sSfv 2>$err $range_tail $url | rsha1)"
103 check_content_range
104 test x"$sha1_tail" = x"$sha1"
107 t_begin "mid range matches" && {
108 sha1="$(curl -sSfv 2>$err $range_mid $url | rsha1)"
109 check_content_range
110 test x"$sha1_mid" = x"$sha1"
113 t_begin "shutdown server" && {
114 kill -QUIT $rainbows_pid
117 t_begin "check stderr" && check_stderr
119 t_done