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
<HttpResponseHeaders
> headers(
22 new HttpResponseHeaders(std::string()));
25 URLRequestDataJob::BuildResponse(GURL("data:,Hello"), &mime_type
,
26 &charset
, &data
, headers
.get()));
28 EXPECT_EQ("text/plain", mime_type
);
29 EXPECT_EQ("US-ASCII", charset
);
30 EXPECT_EQ("Hello", data
);
32 const HttpVersion
& version
= headers
->GetParsedHttpVersion();
33 EXPECT_EQ(1, version
.major_value());
34 EXPECT_EQ(1, version
.minor_value());
35 EXPECT_EQ("OK", headers
->GetStatusText());
37 EXPECT_TRUE(headers
->GetNormalizedHeader("Content-Type", &value
));
38 EXPECT_EQ(value
, "text/plain;charset=US-ASCII");
41 headers
->GetNormalizedHeader("Access-Control-Allow-Origin", &value
));
42 EXPECT_EQ(value
, "*");
45 TEST(BuildResponseTest
, InvalidInput
) {
46 std::string mime_type
;
49 scoped_refptr
<HttpResponseHeaders
> headers(
50 new HttpResponseHeaders(std::string()));
52 EXPECT_EQ(ERR_INVALID_URL
,
53 URLRequestDataJob::BuildResponse(GURL("bogus"), &mime_type
,
54 &charset
, &data
, headers
.get()));
57 TEST(BuildResponseTest
, InvalidMimeType
) {
58 std::string mime_type
;
61 scoped_refptr
<HttpResponseHeaders
> headers(
62 new HttpResponseHeaders(std::string()));
64 // MIME type contains delimiters. Must be accepted but Content-Type header
65 // should be generated as if the mediatype was text/plain.
66 EXPECT_EQ(OK
, URLRequestDataJob::BuildResponse(GURL("data:f(o/b)r,test"),
67 &mime_type
, &charset
, &data
,
71 EXPECT_TRUE(headers
->GetNormalizedHeader("Content-Type", &value
));
72 EXPECT_EQ(value
, "text/plain;charset=US-ASCII");
75 TEST(BuildResponseTest
, InvalidCharset
) {
76 std::string mime_type
;
79 scoped_refptr
<HttpResponseHeaders
> headers(
80 new HttpResponseHeaders(std::string()));
82 // MIME type contains delimiters. Must be rejected.
83 EXPECT_EQ(ERR_INVALID_URL
, URLRequestDataJob::BuildResponse(
84 GURL("data:text/html;charset=(),test"),
85 &mime_type
, &charset
, &data
, headers
.get()));