OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/google_apis/test_server/http_server.h" |
| 6 |
| 7 #include "base/threading/thread.h" |
| 8 #include "content/public/browser/browser_thread.h" |
| 9 #include "content/public/test/test_browser_thread.h" |
| 10 #include "net/url_request/url_fetcher.h" |
| 11 #include "net/url_request/url_fetcher_delegate.h" |
| 12 #include "net/url_request/url_request_test_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace drive { |
| 16 namespace test_server { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Helper function to receive content of a response returned after invoking |
| 21 // |fetcher|. |
| 22 std::string GetFetcherResponseContent(const net::URLFetcher* fetcher) { |
| 23 std::string result; |
| 24 const bool success = fetcher->GetResponseAsString(&result); |
| 25 EXPECT_TRUE(success); |
| 26 return result; |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 class HttpServerTest : public testing::Test, |
| 32 public net::URLFetcherDelegate { |
| 33 public: |
| 34 HttpServerTest() |
| 35 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 36 io_thread_(content::BrowserThread::IO) { |
| 37 } |
| 38 |
| 39 virtual void SetUp() OVERRIDE { |
| 40 io_thread_.StartIOThread(); |
| 41 |
| 42 request_context_getter_ = new net::TestURLRequestContextGetter( |
| 43 content::BrowserThread::GetMessageLoopProxyForThread( |
| 44 content::BrowserThread::IO)); |
| 45 |
| 46 server_.reset(new HttpServer()); |
| 47 ASSERT_TRUE(server_->InitializeAndWaitUntilReady()); |
| 48 } |
| 49 |
| 50 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { |
| 51 MessageLoop::current()->Quit(); |
| 52 } |
| 53 |
| 54 protected: |
| 55 MessageLoopForUI message_loop_; |
| 56 content::TestBrowserThread ui_thread_; |
| 57 content::TestBrowserThread io_thread_; |
| 58 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 59 scoped_ptr<HttpServer> server_; |
| 60 }; |
| 61 |
| 62 TEST_F(HttpServerTest, TextRequest) { |
| 63 // The simplest text response with an auto generated url. |
| 64 GURL url1 = server_->RegisterTextResponse("test1", |
| 65 "Raspberry chocolate", |
| 66 "text/html", |
| 67 SUCCESS); |
| 68 ASSERT_NE("", url1.spec()); |
| 69 |
| 70 GURL url2 = server_->RegisterTextResponse("test2", |
| 71 "Vanilla chocolate", |
| 72 "text/html", |
| 73 SUCCESS); |
| 74 ASSERT_NE("", url2.spec()); |
| 75 |
| 76 // Response with a specified url and response code. |
| 77 GURL url3 = server_->RegisterTextResponse( |
| 78 "chocolate/bar.html", // URL |
| 79 "No chocolates", // Dummy response text. |
| 80 "text/plain", // Content type. |
| 81 NOT_FOUND); // Response code (404 here). |
| 82 ASSERT_NE("", url3.spec()); |
| 83 |
| 84 // Set up fetchers. |
| 85 scoped_ptr<net::URLFetcher> fetcher1 = scoped_ptr<net::URLFetcher>( |
| 86 net::URLFetcher::Create(url1, |
| 87 net::URLFetcher::GET, |
| 88 this)); |
| 89 fetcher1->SetRequestContext(request_context_getter_.get()); |
| 90 scoped_ptr<net::URLFetcher> fetcher2 = scoped_ptr<net::URLFetcher>( |
| 91 net::URLFetcher::Create(url2, |
| 92 net::URLFetcher::GET, |
| 93 this)); |
| 94 fetcher2->SetRequestContext(request_context_getter_.get()); |
| 95 scoped_ptr<net::URLFetcher> fetcher3 = scoped_ptr<net::URLFetcher>( |
| 96 net::URLFetcher::Create(url3, |
| 97 net::URLFetcher::GET, |
| 98 this)); |
| 99 fetcher3->SetRequestContext(request_context_getter_.get()); |
| 100 |
| 101 // Test. |
| 102 fetcher1->Start(); |
| 103 MessageLoop::current()->Run(); |
| 104 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher1->GetStatus().status()); |
| 105 EXPECT_EQ(200, fetcher1->GetResponseCode()); |
| 106 EXPECT_EQ("Raspberry chocolate", GetFetcherResponseContent(fetcher1.get())); |
| 107 |
| 108 fetcher2->Start(); |
| 109 MessageLoop::current()->Run(); |
| 110 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher1->GetStatus().status()); |
| 111 EXPECT_EQ(200, fetcher2->GetResponseCode()); |
| 112 EXPECT_EQ("Vanilla chocolate", GetFetcherResponseContent(fetcher2.get())); |
| 113 |
| 114 fetcher3->Start(); |
| 115 MessageLoop::current()->Run(); |
| 116 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher1->GetStatus().status()); |
| 117 EXPECT_EQ(404, fetcher3->GetResponseCode()); |
| 118 EXPECT_EQ("No chocolates", GetFetcherResponseContent(fetcher3.get())); |
| 119 } |
| 120 |
| 121 TEST_F(HttpServerTest, DefaultNotFoundResponse) { |
| 122 ASSERT_NE("", server_->GetBaseURL().spec()); |
| 123 |
| 124 scoped_ptr<net::URLFetcher> fetcher = scoped_ptr<net::URLFetcher>( |
| 125 net::URLFetcher::Create(server_->GetBaseURL(), |
| 126 net::URLFetcher::GET, |
| 127 this)); |
| 128 fetcher->SetRequestContext(request_context_getter_.get()); |
| 129 |
| 130 fetcher->Start(); |
| 131 MessageLoop::current()->Run(); |
| 132 EXPECT_EQ(net::URLRequestStatus::SUCCESS, fetcher->GetStatus().status()); |
| 133 EXPECT_EQ(404, fetcher->GetResponseCode()); |
| 134 } |
| 135 |
| 136 // TODO(mtomasz): Write a test for a file response. |
| 137 |
| 138 } // namespace test_server |
| 139 } // namespace drive |
OLD | NEW |