forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_response_generator.cc
112 lines (91 loc) · 3.52 KB
/
test_response_generator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2013 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 "media/blink/test_response_generator.h"
#include "base/format_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "net/base/net_errors.h"
#include "third_party/blink/public/platform/web_string.h"
using blink::WebString;
using blink::WebURL;
using blink::WebURLError;
using blink::WebURLResponse;
namespace media {
TestResponseGenerator::TestResponseGenerator(const GURL& gurl,
int64_t content_length)
: gurl_(gurl), content_length_(content_length) {}
WebURLError TestResponseGenerator::GenerateError() {
return WebURLError(net::ERR_ABORTED, WebURL());
}
WebURLResponse TestResponseGenerator::Generate200() {
WebURLResponse response(gurl_);
response.SetHTTPStatusCode(200);
response.SetHTTPHeaderField(
WebString::FromUTF8("Content-Length"),
WebString::FromUTF8(base::Int64ToString(content_length_)));
response.SetExpectedContentLength(content_length_);
return response;
}
WebURLResponse TestResponseGenerator::Generate206(int64_t first_byte_offset) {
return GeneratePartial206(first_byte_offset, content_length_ - 1, kNormal);
}
WebURLResponse TestResponseGenerator::Generate206(int64_t first_byte_offset,
Flags flags) {
return GeneratePartial206(first_byte_offset, content_length_ - 1, flags);
}
WebURLResponse TestResponseGenerator::GeneratePartial206(
int64_t first_byte_offset,
int64_t last_byte_offset) {
return GeneratePartial206(first_byte_offset, last_byte_offset, kNormal);
}
WebURLResponse TestResponseGenerator::GeneratePartial206(
int64_t first_byte_offset,
int64_t last_byte_offset,
Flags flags) {
int64_t range_content_length = content_length_ - first_byte_offset;
WebURLResponse response(gurl_);
response.SetHTTPStatusCode(206);
if ((flags & kNoAcceptRanges) == 0) {
response.SetHTTPHeaderField(WebString::FromUTF8("Accept-Ranges"),
WebString::FromUTF8("bytes"));
}
if ((flags & kNoContentRange) == 0) {
std::string content_range = base::StringPrintf(
"bytes %" PRId64 "-%" PRId64 "/",
first_byte_offset, last_byte_offset);
if (flags & kNoContentRangeInstanceSize)
content_range += "*";
else
content_range += base::StringPrintf("%" PRId64, content_length_);
response.SetHTTPHeaderField(WebString::FromUTF8("Content-Range"),
WebString::FromUTF8(content_range));
}
if ((flags & kNoContentLength) == 0) {
response.SetHTTPHeaderField(
WebString::FromUTF8("Content-Length"),
WebString::FromUTF8(base::Int64ToString(range_content_length)));
response.SetExpectedContentLength(range_content_length);
}
return response;
}
WebURLResponse TestResponseGenerator::GenerateResponse(int code) {
WebURLResponse response(gurl_);
response.SetHTTPStatusCode(code);
return response;
}
WebURLResponse TestResponseGenerator::Generate404() {
return GenerateResponse(404);
}
WebURLResponse TestResponseGenerator::GenerateFileResponse(
int64_t first_byte_offset) {
WebURLResponse response(gurl_);
response.SetHTTPStatusCode(0);
if (first_byte_offset >= 0) {
response.SetExpectedContentLength(content_length_ - first_byte_offset);
} else {
response.SetExpectedContentLength(-1);
}
return response;
}
} // namespace media