summaryrefslogtreecommitdiffstats
path: root/net/url_request/url_request_data_job_unittest.cc
blob: 49a7ecd92c3cbe80ed076497b0359324e98a3e84 (plain)
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
// Copyright 2014 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 <string>

#include "base/memory/ref_counted.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_version.h"
#include "net/url_request/url_request_data_job.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace net {

TEST(BuildResponseTest, Simple) {
  std::string mime_type;
  std::string charset;
  std::string data;
  scoped_refptr<net::HttpResponseHeaders> headers(
      new net::HttpResponseHeaders(std::string()));

  ASSERT_EQ(
      net::OK,
      URLRequestDataJob::BuildResponse(
          GURL("data:,Hello"), &mime_type, &charset, &data, headers.get()));

  EXPECT_EQ("text/plain", mime_type);
  EXPECT_EQ("US-ASCII", charset);
  EXPECT_EQ("Hello", data);

  const net::HttpVersion& version = headers->GetParsedHttpVersion();
  EXPECT_EQ(1, version.major_value());
  EXPECT_EQ(1, version.minor_value());
  EXPECT_EQ("OK", headers->GetStatusText());
  std::string value;
  EXPECT_TRUE(headers->GetNormalizedHeader("Content-Type", &value));
  EXPECT_EQ(value, "text/plain;charset=US-ASCII");
  value.clear();
  EXPECT_TRUE(
      headers->GetNormalizedHeader("Access-Control-Allow-Origin", &value));
  EXPECT_EQ(value, "*");
}

TEST(BuildResponseTest, InvalidInput) {
  std::string mime_type;
  std::string charset;
  std::string data;
  scoped_refptr<net::HttpResponseHeaders> headers(
      new net::HttpResponseHeaders(std::string()));

  EXPECT_EQ(
      net::ERR_INVALID_URL,
      URLRequestDataJob::BuildResponse(
          GURL("bogus"), &mime_type, &charset, &data, headers.get()));
}

TEST(BuildResponseTest, InvalidMimeType) {
  std::string mime_type;
  std::string charset;
  std::string data;
  scoped_refptr<net::HttpResponseHeaders> headers(
      new net::HttpResponseHeaders(std::string()));

  // MIME type contains delimiters. Must be accepted but Content-Type header
  // should be generated as if the mediatype was text/plain.
  EXPECT_EQ(
      net::OK,
      URLRequestDataJob::BuildResponse(
          GURL("data:f(o/b)r,test"),
          &mime_type, &charset, &data, headers.get()));

  std::string value;
  EXPECT_TRUE(headers->GetNormalizedHeader("Content-Type", &value));
  EXPECT_EQ(value, "text/plain;charset=US-ASCII");
}

TEST(BuildResponseTest, InvalidCharset) {
  std::string mime_type;
  std::string charset;
  std::string data;
  scoped_refptr<net::HttpResponseHeaders> headers(
      new net::HttpResponseHeaders(std::string()));

  // MIME type contains delimiters. Must be rejected.
  EXPECT_EQ(
      net::ERR_INVALID_URL,
      URLRequestDataJob::BuildResponse(
          GURL("data:text/html;charset=(),test"),
          &mime_type, &charset, &data, headers.get()));
}

}  // namespace net