[sync] Upstream the code that triggers the InvalidationController
[chromium-blink-merge.git] / webkit / media / test_response_generator.cc
blobfaab513ed48c3003c2198e909c9728c3c5cc5b3d
1 // Copyright (c) 2012 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 "webkit/media/test_response_generator.h"
7 #include "base/format_macros.h"
8 #include "base/string_number_conversions.h"
9 #include "base/stringprintf.h"
10 #include "net/base/net_errors.h"
11 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
12 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h"
14 using WebKit::WebString;
15 using WebKit::WebURLError;
16 using WebKit::WebURLResponse;
18 namespace webkit_media {
20 TestResponseGenerator::TestResponseGenerator(const GURL& gurl,
21 int64 content_length)
22 : gurl_(gurl),
23 content_length_(content_length) {
26 WebURLError TestResponseGenerator::GenerateError() {
27 WebURLError error;
28 error.reason = net::ERR_ABORTED;
29 error.domain = WebString::fromUTF8(net::kErrorDomain);
30 return error;
33 WebURLResponse TestResponseGenerator::Generate200() {
34 WebURLResponse response(gurl_);
35 response.setHTTPStatusCode(200);
37 response.setHTTPHeaderField(
38 WebString::fromUTF8("Content-Length"),
39 WebString::fromUTF8(base::Int64ToString(content_length_)));
40 response.setExpectedContentLength(content_length_);
41 return response;
44 WebURLResponse TestResponseGenerator::Generate206(int64 first_byte_offset) {
45 return Generate206(first_byte_offset, kNormal);
48 WebURLResponse TestResponseGenerator::Generate206(int64 first_byte_offset,
49 Flags flags) {
50 int64 range_content_length = content_length_ - first_byte_offset;
51 int64 last_byte_offset = content_length_ - 1;
53 WebURLResponse response(gurl_);
54 response.setHTTPStatusCode(206);
56 if ((flags & kNoAcceptRanges) == 0) {
57 response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"),
58 WebString::fromUTF8("bytes"));
61 if ((flags & kNoContentRange) == 0) {
62 std::string content_range = base::StringPrintf(
63 "bytes %" PRId64 "-%" PRId64 "/",
64 first_byte_offset, last_byte_offset);
65 if (flags & kNoContentRangeInstanceSize)
66 content_range += "*";
67 else
68 content_range += base::StringPrintf("%" PRId64, content_length_);
69 response.setHTTPHeaderField(WebString::fromUTF8("Content-Range"),
70 WebString::fromUTF8(content_range));
73 if ((flags & kNoContentLength) == 0) {
74 response.setHTTPHeaderField(
75 WebString::fromUTF8("Content-Length"),
76 WebString::fromUTF8(base::Int64ToString(range_content_length)));
77 response.setExpectedContentLength(range_content_length);
79 return response;
82 WebURLResponse TestResponseGenerator::Generate404() {
83 WebURLResponse response(gurl_);
84 response.setHTTPStatusCode(404);
85 return response;
88 WebURLResponse TestResponseGenerator::GenerateFileResponse(
89 int64 first_byte_offset) {
90 WebURLResponse response(gurl_);
91 response.setHTTPStatusCode(0);
93 if (first_byte_offset >= 0) {
94 response.setExpectedContentLength(content_length_ - first_byte_offset);
95 } else {
96 response.setExpectedContentLength(-1);
98 return response;
101 } // namespace webkit_media