OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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 "chrome/browser/search/suggestion_source.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/ref_counted_memory.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/message_loop.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "content/public/test/test_browser_thread.h" | |
13 #include "googleurl/src/gurl.h" | |
14 #include "grit/browser_resources.h" | |
15 #include "net/url_request/url_request.h" | |
16 #include "net/url_request/url_request_context.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 class SuggestionSourceTest : public testing::Test { | |
20 public: | |
21 SuggestionSourceTest() | |
22 : message_loop_(MessageLoop::TYPE_IO), | |
23 io_thread_(content::BrowserThread::IO, &message_loop_), | |
24 response_(NULL) { | |
25 } | |
26 | |
27 SuggestionSource* source() { return source_.get(); } | |
28 std::string response_string() { | |
29 if (response_) { | |
30 return std::string(reinterpret_cast<const char*>(response_->front()), | |
31 response_->size()); | |
32 } | |
33 return ""; | |
34 } | |
35 net::URLRequest* MockRequest(const std::string& url) { | |
36 return empty_context_.CreateRequest(GURL(url), NULL); | |
37 } | |
38 void StartDataRequest(const std::string& path_and_query) { | |
39 source()->StartDataRequest(path_and_query, false, callback_); | |
40 } | |
41 | |
42 private: | |
43 virtual void SetUp() { | |
44 source_.reset(new SuggestionSource()); | |
45 callback_ = base::Bind(&SuggestionSourceTest::SaveResponse, | |
46 base::Unretained(this)); | |
47 response_ = NULL; | |
48 } | |
49 | |
50 virtual void TearDown() { | |
51 source_.reset(); | |
52 } | |
53 | |
54 void SaveResponse(base::RefCountedMemory* data) { | |
55 response_ = data; | |
56 } | |
57 | |
58 MessageLoop message_loop_; | |
59 content::TestBrowserThread io_thread_; | |
60 | |
61 scoped_ptr<SuggestionSource> source_; | |
62 content::URLDataSource::GotDataCallback callback_; | |
63 net::URLRequestContext empty_context_; | |
64 scoped_refptr<base::RefCountedMemory> response_; | |
65 }; | |
66 | |
67 TEST_F(SuggestionSourceTest, ShouldServiceRequest) { | |
68 scoped_ptr<net::URLRequest> request; | |
69 request.reset(MockRequest("http://suggestion/loader.js")); | |
70 EXPECT_FALSE(source()->ShouldServiceRequest(request.get())); | |
71 request.reset(MockRequest("chrome-search://bogus/loader.js")); | |
72 EXPECT_FALSE(source()->ShouldServiceRequest(request.get())); | |
73 request.reset(MockRequest("chrome-search://suggestion/bogus.js")); | |
74 EXPECT_FALSE(source()->ShouldServiceRequest(request.get())); | |
75 request.reset(MockRequest("chrome-search://suggestion/loader.js")); | |
76 EXPECT_TRUE(source()->ShouldServiceRequest(request.get())); | |
77 request.reset(MockRequest("chrome-search://suggestion/loader.js?origin=1")); | |
78 EXPECT_TRUE(source()->ShouldServiceRequest(request.get())); | |
79 } | |
80 | |
81 TEST_F(SuggestionSourceTest, GetMimeType) { | |
82 // URLDataManagerBackend does not include / in path_and_query. | |
83 EXPECT_EQ("text/html", source()->GetMimeType("loader.html")); | |
84 EXPECT_EQ("application/javascript", | |
85 source()->GetMimeType("loader.js?origin=bar")); | |
86 EXPECT_EQ("", source()->GetMimeType("bogus.html")); | |
87 } | |
88 | |
89 TEST_F(SuggestionSourceTest, StartDataRequest) { | |
90 StartDataRequest("bogus.html"); | |
91 EXPECT_TRUE(response_string().empty()); | |
92 StartDataRequest("loader.html?origin=chunkybacon"); | |
Dan Beam
2013/04/08 16:46:58
chunky bacon!?
Jered
2013/04/08 21:43:16
:-)
http://akuaku.org/images/chunky%20bacon.png
| |
93 EXPECT_FALSE(response_string().empty()); | |
94 EXPECT_EQ(std::string::npos, response_string().find("chunkybacon")); | |
95 StartDataRequest("loader.js"); | |
96 EXPECT_TRUE(response_string().empty()); | |
97 StartDataRequest("loader.js?origin=chunkybacon"); | |
98 EXPECT_FALSE(response_string().empty()); | |
99 EXPECT_NE(std::string::npos, response_string().find("\"chunkybacon\"")); | |
100 StartDataRequest("loader.js?origin=bacon%22;alert('evil');x=%22"); | |
101 EXPECT_FALSE(response_string().empty()); | |
102 EXPECT_NE(std::string::npos, | |
103 response_string().find("bacon\\\";alert('evil');x=\\\"")); | |
104 } | |
OLD | NEW |