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"
12 TEST(HttpByteRangeTest
, ValidRanges
) {
14 int64 first_byte_position
;
15 int64 last_byte_position
;
24 { -1, -1, -1, false },
26 { 10, 10000, 0, true },
27 { -1, -1, 100000, true },
30 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
32 range
.set_first_byte_position(tests
[i
].first_byte_position
);
33 range
.set_last_byte_position(tests
[i
].last_byte_position
);
34 range
.set_suffix_length(tests
[i
].suffix_length
);
35 EXPECT_EQ(tests
[i
].valid
, range
.IsValid());
39 TEST(HttpByteRangeTest
, SetInstanceSize
) {
41 int64 first_byte_position
;
42 int64 last_byte_position
;
45 bool expected_return_value
;
46 int64 expected_lower_bound
;
47 int64 expected_upper_bound
;
49 { -10, 0, -1, 0, false, -1, -1 },
50 { 10, 0, -1, 0, false, -1, -1 },
51 // Zero instance size is valid, this is the case that user has to handle.
52 { -1, -1, -1, 0, true, 0, -1 },
53 { -1, -1, 500, 0, true, 0, -1 },
54 { -1, 50, -1, 0, false, -1, -1 },
55 { -1, -1, 500, 300, true, 0, 299 },
56 { -1, -1, -1, 100, true, 0, 99 },
57 { 10, -1, -1, 100, true, 10, 99 },
58 { -1, -1, 500, 1000, true, 500, 999 },
59 { 10, 10000, -1, 1000000, true, 10, 10000 },
62 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
64 range
.set_first_byte_position(tests
[i
].first_byte_position
);
65 range
.set_last_byte_position(tests
[i
].last_byte_position
);
66 range
.set_suffix_length(tests
[i
].suffix_length
);
68 bool return_value
= range
.ComputeBounds(tests
[i
].instance_size
);
69 EXPECT_EQ(tests
[i
].expected_return_value
, return_value
);
71 EXPECT_EQ(tests
[i
].expected_lower_bound
, range
.first_byte_position());
72 EXPECT_EQ(tests
[i
].expected_upper_bound
, range
.last_byte_position());
74 // Try to call SetInstanceSize the second time.
75 EXPECT_FALSE(range
.ComputeBounds(tests
[i
].instance_size
));
76 // And expect there's no side effect.
77 EXPECT_EQ(tests
[i
].expected_lower_bound
, range
.first_byte_position());
78 EXPECT_EQ(tests
[i
].expected_upper_bound
, range
.last_byte_position());
79 EXPECT_EQ(tests
[i
].suffix_length
, range
.suffix_length());
84 TEST(HttpByteRangeTest
, GetHeaderValue
) {
89 {HttpByteRange::Bounded(0, 0), "bytes=0-0"},
90 {HttpByteRange::Bounded(0, 100), "bytes=0-100"},
91 {HttpByteRange::Bounded(0, -1), "bytes=0-"},
92 {HttpByteRange::RightUnbounded(100), "bytes=100-"},
93 {HttpByteRange::Suffix(100), "bytes=-100"},
95 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
96 EXPECT_EQ(tests
[i
].expected
, tests
[i
].range
.GetHeaderValue());