summaryrefslogtreecommitdiffstats
path: root/net/http/http_byte_range_unittest.cc
diff options
context:
space:
mode:
authorhclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-28 21:23:39 +0000
committerhclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-28 21:23:39 +0000
commitfc48db85fecb5889fbfc5de5c9b8951dce547546 (patch)
treed7a0e01a06404219af4c05da4a2833ed9072859e /net/http/http_byte_range_unittest.cc
parent4684b1223a711baa9b4e37996c797600060b6f2e (diff)
downloadchromium_src-fc48db85fecb5889fbfc5de5c9b8951dce547546.zip
chromium_src-fc48db85fecb5889fbfc5de5c9b8951dce547546.tar.gz
chromium_src-fc48db85fecb5889fbfc5de5c9b8951dce547546.tar.bz2
Implement a parser that parses the "Range" HTTP header
Parses "Range" HTTP request header so this request information can be used in URLRequestFileJob and HttpCache. Review URL: http://codereview.chromium.org/92006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14784 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/http/http_byte_range_unittest.cc')
-rw-r--r--net/http/http_byte_range_unittest.cc78
1 files changed, 78 insertions, 0 deletions
diff --git a/net/http/http_byte_range_unittest.cc b/net/http/http_byte_range_unittest.cc
new file mode 100644
index 0000000..6629a7c
--- /dev/null
+++ b/net/http/http_byte_range_unittest.cc
@@ -0,0 +1,78 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/http/http_byte_range.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(HttpByteRangeTest, ValidRanges) {
+ const struct {
+ int64 first_byte_position;
+ int64 last_byte_position;
+ int64 suffix_length;
+ bool valid;
+ } tests[] = {
+ { -1, -1, 0, false },
+ { 0, 0, 0, true },
+ { -10, 0, 0, false },
+ { 10, 0, 0, false },
+ { 10, -1, 0, true },
+ { -1, -1, -1, false },
+ { -1, 50, 0, false },
+ { 10, 10000, 0, true },
+ { -1, -1, 100000, true },
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
+ net::HttpByteRange range;
+ range.set_first_byte_position(tests[i].first_byte_position);
+ range.set_last_byte_position(tests[i].last_byte_position);
+ range.set_suffix_length(tests[i].suffix_length);
+ EXPECT_EQ(tests[i].valid, range.IsValid());
+ }
+}
+
+TEST(HttpByteRangeTest, SetInstanceSize) {
+ const struct {
+ int64 first_byte_position;
+ int64 last_byte_position;
+ int64 suffix_length;
+ int64 instance_size;
+ bool expected_return_value;
+ int64 expected_lower_bound;
+ int64 expected_upper_bound;
+ } tests[] = {
+ { -10, 0, -1, 0, false, -1, -1 },
+ { 10, 0, -1, 0, false, -1, -1 },
+ // Zero instance size is valid, this is the case that user has to handle.
+ { -1, -1, -1, 0, true, 0, -1 },
+ { -1, -1, 500, 0, true, 0, -1 },
+ { -1, 50, -1, 0, false, -1, -1 },
+ { -1, -1, 500, 300, true, 0, 299 },
+ { -1, -1, -1, 100, true, 0, 99 },
+ { 10, -1, -1, 100, true, 10, 99 },
+ { -1, -1, 500, 1000, true, 500, 999 },
+ { 10, 10000, -1, 1000000, true, 10, 10000 },
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
+ net::HttpByteRange range;
+ range.set_first_byte_position(tests[i].first_byte_position);
+ range.set_last_byte_position(tests[i].last_byte_position);
+ range.set_suffix_length(tests[i].suffix_length);
+
+ bool return_value = range.ComputeBounds(tests[i].instance_size);
+ EXPECT_EQ(tests[i].expected_return_value, return_value);
+ if (return_value) {
+ EXPECT_EQ(tests[i].expected_lower_bound, range.first_byte_position());
+ EXPECT_EQ(tests[i].expected_upper_bound, range.last_byte_position());
+
+ // Try to call SetInstanceSize the second time.
+ EXPECT_FALSE(range.ComputeBounds(tests[i].instance_size));
+ // And expect there's no side effect.
+ EXPECT_EQ(tests[i].expected_lower_bound, range.first_byte_position());
+ EXPECT_EQ(tests[i].expected_upper_bound, range.last_byte_position());
+ EXPECT_EQ(tests[i].suffix_length, range.suffix_length());
+ }
+ }
+}