1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/http/http_byte_range.h"
6 #include "testing/gtest/include/gtest/gtest.h"
8 TEST(HttpByteRangeTest
, ValidRanges
) {
10 int64 first_byte_position
;
11 int64 last_byte_position
;
20 { -1, -1, -1, false },
22 { 10, 10000, 0, true },
23 { -1, -1, 100000, true },
26 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
27 net::HttpByteRange range
;
28 range
.set_first_byte_position(tests
[i
].first_byte_position
);
29 range
.set_last_byte_position(tests
[i
].last_byte_position
);
30 range
.set_suffix_length(tests
[i
].suffix_length
);
31 EXPECT_EQ(tests
[i
].valid
, range
.IsValid());
35 TEST(HttpByteRangeTest
, SetInstanceSize
) {
37 int64 first_byte_position
;
38 int64 last_byte_position
;
41 bool expected_return_value
;
42 int64 expected_lower_bound
;
43 int64 expected_upper_bound
;
45 { -10, 0, -1, 0, false, -1, -1 },
46 { 10, 0, -1, 0, false, -1, -1 },
47 // Zero instance size is valid, this is the case that user has to handle.
48 { -1, -1, -1, 0, true, 0, -1 },
49 { -1, -1, 500, 0, true, 0, -1 },
50 { -1, 50, -1, 0, false, -1, -1 },
51 { -1, -1, 500, 300, true, 0, 299 },
52 { -1, -1, -1, 100, true, 0, 99 },
53 { 10, -1, -1, 100, true, 10, 99 },
54 { -1, -1, 500, 1000, true, 500, 999 },
55 { 10, 10000, -1, 1000000, true, 10, 10000 },
58 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
59 net::HttpByteRange range
;
60 range
.set_first_byte_position(tests
[i
].first_byte_position
);
61 range
.set_last_byte_position(tests
[i
].last_byte_position
);
62 range
.set_suffix_length(tests
[i
].suffix_length
);
64 bool return_value
= range
.ComputeBounds(tests
[i
].instance_size
);
65 EXPECT_EQ(tests
[i
].expected_return_value
, return_value
);
67 EXPECT_EQ(tests
[i
].expected_lower_bound
, range
.first_byte_position());
68 EXPECT_EQ(tests
[i
].expected_upper_bound
, range
.last_byte_position());
70 // Try to call SetInstanceSize the second time.
71 EXPECT_FALSE(range
.ComputeBounds(tests
[i
].instance_size
));
72 // And expect there's no side effect.
73 EXPECT_EQ(tests
[i
].expected_lower_bound
, range
.first_byte_position());
74 EXPECT_EQ(tests
[i
].expected_upper_bound
, range
.last_byte_position());
75 EXPECT_EQ(tests
[i
].suffix_length
, range
.suffix_length());
80 TEST(HttpByteRangeTest
, GetHeaderValue
) {
82 net::HttpByteRange range
;
84 } tests
[] = {{net::HttpByteRange::Bounded(0, 0), "bytes=0-0"},
85 {net::HttpByteRange::Bounded(0, 100), "bytes=0-100"},
86 {net::HttpByteRange::Bounded(0, -1), "bytes=0-"},
87 {net::HttpByteRange::RightUnbounded(100), "bytes=100-"},
88 {net::HttpByteRange::Suffix(100), "bytes=-100"}, };
89 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
90 EXPECT_EQ(tests
[i
].expected
, tests
[i
].range
.GetHeaderValue());