1 // Copyright 2013 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 "media/blink/test_response_generator.h"
7 #include "base/format_macros.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "net/base/net_errors.h"
11 #include "third_party/WebKit/public/platform/WebString.h"
13 using blink::WebString
;
14 using blink::WebURLError
;
15 using blink::WebURLResponse
;
19 TestResponseGenerator::TestResponseGenerator(const GURL
& gurl
,
22 content_length_(content_length
) {
25 WebURLError
TestResponseGenerator::GenerateError() {
27 error
.reason
= net::ERR_ABORTED
;
28 error
.domain
= WebString::fromUTF8(net::kErrorDomain
);
32 WebURLResponse
TestResponseGenerator::Generate200() {
33 WebURLResponse
response(gurl_
);
34 response
.setHTTPStatusCode(200);
36 response
.setHTTPHeaderField(
37 WebString::fromUTF8("Content-Length"),
38 WebString::fromUTF8(base::Int64ToString(content_length_
)));
39 response
.setExpectedContentLength(content_length_
);
43 WebURLResponse
TestResponseGenerator::Generate206(int64 first_byte_offset
) {
44 return GeneratePartial206(first_byte_offset
, content_length_
- 1, kNormal
);
47 WebURLResponse
TestResponseGenerator::Generate206(int64 first_byte_offset
,
49 return GeneratePartial206(first_byte_offset
, content_length_
- 1, flags
);
52 WebURLResponse
TestResponseGenerator::GeneratePartial206(
53 int64 first_byte_offset
,
54 int64 last_byte_offset
) {
55 return GeneratePartial206(first_byte_offset
, last_byte_offset
, kNormal
);
58 WebURLResponse
TestResponseGenerator::GeneratePartial206(
59 int64 first_byte_offset
,
60 int64 last_byte_offset
,
62 int64 range_content_length
= content_length_
- first_byte_offset
;
64 WebURLResponse
response(gurl_
);
65 response
.setHTTPStatusCode(206);
67 if ((flags
& kNoAcceptRanges
) == 0) {
68 response
.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"),
69 WebString::fromUTF8("bytes"));
72 if ((flags
& kNoContentRange
) == 0) {
73 std::string content_range
= base::StringPrintf(
74 "bytes %" PRId64
"-%" PRId64
"/",
75 first_byte_offset
, last_byte_offset
);
76 if (flags
& kNoContentRangeInstanceSize
)
79 content_range
+= base::StringPrintf("%" PRId64
, content_length_
);
80 response
.setHTTPHeaderField(WebString::fromUTF8("Content-Range"),
81 WebString::fromUTF8(content_range
));
84 if ((flags
& kNoContentLength
) == 0) {
85 response
.setHTTPHeaderField(
86 WebString::fromUTF8("Content-Length"),
87 WebString::fromUTF8(base::Int64ToString(range_content_length
)));
88 response
.setExpectedContentLength(range_content_length
);
93 WebURLResponse
TestResponseGenerator::Generate404() {
94 WebURLResponse
response(gurl_
);
95 response
.setHTTPStatusCode(404);
99 WebURLResponse
TestResponseGenerator::GenerateFileResponse(
100 int64 first_byte_offset
) {
101 WebURLResponse
response(gurl_
);
102 response
.setHTTPStatusCode(0);
104 if (first_byte_offset
>= 0) {
105 response
.setExpectedContentLength(content_length_
- first_byte_offset
);
107 response
.setExpectedContentLength(-1);