1 // Copyright 2014 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.
7 #include "base/memory/ref_counted.h"
8 #include "net/base/net_errors.h"
9 #include "net/http/http_response_headers.h"
10 #include "net/http/http_version.h"
11 #include "net/url_request/url_request_data_job.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 TEST(BuildResponseTest
, Simple
) {
18 std::string mime_type
;
21 scoped_refptr
<net::HttpResponseHeaders
> headers(
22 new net::HttpResponseHeaders(std::string()));
26 URLRequestDataJob::BuildResponse(
27 GURL("data:,Hello"), &mime_type
, &charset
, &data
, headers
.get()));
29 EXPECT_EQ("text/plain", mime_type
);
30 EXPECT_EQ("US-ASCII", charset
);
31 EXPECT_EQ("Hello", data
);
33 const net::HttpVersion
& version
= headers
->GetParsedHttpVersion();
34 EXPECT_EQ(1, version
.major_value());
35 EXPECT_EQ(1, version
.minor_value());
36 EXPECT_EQ("OK", headers
->GetStatusText());
38 EXPECT_TRUE(headers
->GetNormalizedHeader("Content-Type", &value
));
39 EXPECT_EQ(value
, "text/plain;charset=US-ASCII");
42 headers
->GetNormalizedHeader("Access-Control-Allow-Origin", &value
));
43 EXPECT_EQ(value
, "*");
46 TEST(BuildResponseTest
, InvalidInput
) {
47 std::string mime_type
;
50 scoped_refptr
<net::HttpResponseHeaders
> headers(
51 new net::HttpResponseHeaders(std::string()));
55 URLRequestDataJob::BuildResponse(
56 GURL("bogus"), &mime_type
, &charset
, &data
, headers
.get()));
59 TEST(BuildResponseTest
, InvalidMimeType
) {
60 std::string mime_type
;
63 scoped_refptr
<net::HttpResponseHeaders
> headers(
64 new net::HttpResponseHeaders(std::string()));
66 // MIME type contains delimiters. Must be accepted but Content-Type header
67 // should be generated as if the mediatype was text/plain.
70 URLRequestDataJob::BuildResponse(
71 GURL("data:f(o/b)r,test"),
72 &mime_type
, &charset
, &data
, headers
.get()));
75 EXPECT_TRUE(headers
->GetNormalizedHeader("Content-Type", &value
));
76 EXPECT_EQ(value
, "text/plain;charset=US-ASCII");
79 TEST(BuildResponseTest
, InvalidCharset
) {
80 std::string mime_type
;
83 scoped_refptr
<net::HttpResponseHeaders
> headers(
84 new net::HttpResponseHeaders(std::string()));
86 // MIME type contains delimiters. Must be rejected.
89 URLRequestDataJob::BuildResponse(
90 GURL("data:text/html;charset=(),test"),
91 &mime_type
, &charset
, &data
, headers
.get()));