Home | History | Annotate | Download | only in blink
      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.
      4 
      5 #include "media/blink/test_response_generator.h"
      6 
      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"
     12 #include "third_party/WebKit/public/platform/WebURLResponse.h"
     13 
     14 using blink::WebString;
     15 using blink::WebURLError;
     16 using blink::WebURLResponse;
     17 
     18 namespace media {
     19 
     20 TestResponseGenerator::TestResponseGenerator(const GURL& gurl,
     21                                              int64 content_length)
     22     : gurl_(gurl),
     23       content_length_(content_length) {
     24 }
     25 
     26 WebURLError TestResponseGenerator::GenerateError() {
     27   WebURLError error;
     28   error.reason = net::ERR_ABORTED;
     29   error.domain = WebString::fromUTF8(net::kErrorDomain);
     30   return error;
     31 }
     32 
     33 WebURLResponse TestResponseGenerator::Generate200() {
     34   WebURLResponse response(gurl_);
     35   response.setHTTPStatusCode(200);
     36 
     37   response.setHTTPHeaderField(
     38       WebString::fromUTF8("Content-Length"),
     39       WebString::fromUTF8(base::Int64ToString(content_length_)));
     40   response.setExpectedContentLength(content_length_);
     41   return response;
     42 }
     43 
     44 WebURLResponse TestResponseGenerator::Generate206(int64 first_byte_offset) {
     45   return Generate206(first_byte_offset, kNormal);
     46 }
     47 
     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;
     52 
     53   WebURLResponse response(gurl_);
     54   response.setHTTPStatusCode(206);
     55 
     56   if ((flags & kNoAcceptRanges) == 0) {
     57     response.setHTTPHeaderField(WebString::fromUTF8("Accept-Ranges"),
     58                                 WebString::fromUTF8("bytes"));
     59   }
     60 
     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));
     71   }
     72 
     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);
     78   }
     79   return response;
     80 }
     81 
     82 WebURLResponse TestResponseGenerator::Generate404() {
     83   WebURLResponse response(gurl_);
     84   response.setHTTPStatusCode(404);
     85   return response;
     86 }
     87 
     88 WebURLResponse TestResponseGenerator::GenerateFileResponse(
     89     int64 first_byte_offset) {
     90   WebURLResponse response(gurl_);
     91   response.setHTTPStatusCode(0);
     92 
     93   if (first_byte_offset >= 0) {
     94     response.setExpectedContentLength(content_length_ - first_byte_offset);
     95   } else {
     96     response.setExpectedContentLength(-1);
     97   }
     98   return response;
     99 }
    100 
    101 }  // namespace media
    102