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