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.
4
5#include <string>
6
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"
13#include "url/gurl.h"
14
15namespace net {
16
17TEST(BuildResponseTest, Simple) {
18  std::string mime_type;
19  std::string charset;
20  std::string data;
21  scoped_refptr<net::HttpResponseHeaders> headers(
22      new net::HttpResponseHeaders(std::string()));
23
24  ASSERT_EQ(
25      net::OK,
26      URLRequestDataJob::BuildResponse(
27          GURL("data:,Hello"), &mime_type, &charset, &data, headers.get()));
28
29  EXPECT_EQ("text/plain", mime_type);
30  EXPECT_EQ("US-ASCII", charset);
31  EXPECT_EQ("Hello", data);
32
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());
37  std::string value;
38  EXPECT_TRUE(headers->GetNormalizedHeader("Content-Type", &value));
39  EXPECT_EQ(value, "text/plain;charset=US-ASCII");
40  value.clear();
41  EXPECT_TRUE(
42      headers->GetNormalizedHeader("Access-Control-Allow-Origin", &value));
43  EXPECT_EQ(value, "*");
44}
45
46TEST(BuildResponseTest, InvalidInput) {
47  std::string mime_type;
48  std::string charset;
49  std::string data;
50  scoped_refptr<net::HttpResponseHeaders> headers(
51      new net::HttpResponseHeaders(std::string()));
52
53  EXPECT_EQ(
54      net::ERR_INVALID_URL,
55      URLRequestDataJob::BuildResponse(
56          GURL("bogus"), &mime_type, &charset, &data, headers.get()));
57}
58
59TEST(BuildResponseTest, InvalidMimeType) {
60  std::string mime_type;
61  std::string charset;
62  std::string data;
63  scoped_refptr<net::HttpResponseHeaders> headers(
64      new net::HttpResponseHeaders(std::string()));
65
66  // MIME type contains delimiters. Must be accepted but Content-Type header
67  // should be generated as if the mediatype was text/plain.
68  EXPECT_EQ(
69      net::OK,
70      URLRequestDataJob::BuildResponse(
71          GURL("data:f(o/b)r,test"),
72          &mime_type, &charset, &data, headers.get()));
73
74  std::string value;
75  EXPECT_TRUE(headers->GetNormalizedHeader("Content-Type", &value));
76  EXPECT_EQ(value, "text/plain;charset=US-ASCII");
77}
78
79TEST(BuildResponseTest, InvalidCharset) {
80  std::string mime_type;
81  std::string charset;
82  std::string data;
83  scoped_refptr<net::HttpResponseHeaders> headers(
84      new net::HttpResponseHeaders(std::string()));
85
86  // MIME type contains delimiters. Must be rejected.
87  EXPECT_EQ(
88      net::ERR_INVALID_URL,
89      URLRequestDataJob::BuildResponse(
90          GURL("data:text/html;charset=(),test"),
91          &mime_type, &charset, &data, headers.get()));
92}
93
94}  // namespace net
95