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 "content/browser/transition_request_manager.h"
6#include "net/http/http_response_headers.h"
7#include "net/http/http_util.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace content {
11
12class TransitionRequestManagerTest : public testing::Test {
13 public:
14  virtual ~TransitionRequestManagerTest() {}
15};
16
17TEST_F(TransitionRequestManagerTest,
18       ParseTransitionStylesheetsFromNullHeaders) {
19  const GURL url("http://www.test.com/");
20  std::vector<GURL> entering_stylesheets;
21  scoped_refptr<net::HttpResponseHeaders> headers;
22
23  TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
24      headers, entering_stylesheets, url);
25  ASSERT_TRUE(entering_stylesheets.empty());
26}
27
28TEST_F(TransitionRequestManagerTest,
29       ParseTransitionStylesheetsFromEmptyHeaders) {
30  const GURL url("http://www.test.com/");
31  std::vector<GURL> entering_stylesheets;
32
33  char headers_string[] = "";
34  scoped_refptr<net::HttpResponseHeaders> headers(
35      new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders(
36          headers_string, sizeof(headers_string))));
37
38  TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
39      headers, entering_stylesheets, url);
40  ASSERT_TRUE(entering_stylesheets.empty());
41}
42
43TEST_F(TransitionRequestManagerTest,
44       ParseTransitionStylesheetsFromHeadersForInvalidURL) {
45  const GURL url;
46  std::vector<GURL> entering_stylesheets;
47
48  char headers_string[] =
49      "HTTP/1.0 200 OK\r\n"
50      "link: <transition.css>;rel=transition-entering-stylesheet;scope=*\r\n"
51      "\r\n";
52  scoped_refptr<net::HttpResponseHeaders> headers(
53      new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders(
54          headers_string, sizeof(headers_string))));
55
56  TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
57      headers, entering_stylesheets, url);
58  ASSERT_TRUE(entering_stylesheets.empty());
59}
60
61TEST_F(TransitionRequestManagerTest, ParseTransitionStylesheetsFromHeaders) {
62  const GURL url("http://www.test.com/");
63  std::vector<GURL> entering_stylesheets;
64
65  char headers_string[] =
66      "HTTP/1.0 200 OK\r\n"
67      "link: <transition.css>;rel=transition-entering-stylesheet;scope=*\r\n"
68      "\r\n";
69  scoped_refptr<net::HttpResponseHeaders> headers(
70      new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders(
71          headers_string, sizeof(headers_string))));
72
73  TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
74      headers, entering_stylesheets, url);
75  ASSERT_TRUE(entering_stylesheets.size() == 1);
76  ASSERT_STREQ((url.spec() + "transition.css").c_str(),
77               entering_stylesheets[0].spec().c_str());
78}
79
80TEST_F(TransitionRequestManagerTest,
81       ParseMultipleTransitionStylesheetsFromHeaders) {
82  const GURL url("http://www.test.com/");
83  std::vector<GURL> entering_stylesheets;
84
85  char headers_string[] =
86      "HTTP/1.0 200 OK\r\n"
87      "link: <transition0.css>;rel=transition-entering-stylesheet;scope=*\r\n"
88      "link: <transition1.css>;rel=transition-entering-stylesheet;scope=*\r\n"
89      "link: <transition2.css>;rel=transition-entering-stylesheet;scope=*\r\n"
90      "\r\n";
91  scoped_refptr<net::HttpResponseHeaders> headers(
92      new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders(
93          headers_string, sizeof(headers_string))));
94
95  TransitionRequestManager::ParseTransitionStylesheetsFromHeaders(
96      headers, entering_stylesheets, url);
97  ASSERT_TRUE(entering_stylesheets.size() == 3);
98  ASSERT_STREQ((url.spec() + "transition0.css").c_str(),
99               entering_stylesheets[0].spec().c_str());
100  ASSERT_STREQ((url.spec() + "transition1.css").c_str(),
101               entering_stylesheets[1].spec().c_str());
102  ASSERT_STREQ((url.spec() + "transition2.css").c_str(),
103               entering_stylesheets[2].spec().c_str());
104}
105
106}  // namespace content
107