| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright 2017 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 "components/payments/payment_manifest_downloader.h" | 
|  | 6 | 
|  | 7 #include "base/macros.h" | 
|  | 8 #include "base/threading/thread_task_runner_handle.h" | 
|  | 9 #include "content/public/test/test_browser_thread_bundle.h" | 
|  | 10 #include "net/http/http_response_headers.h" | 
|  | 11 #include "net/url_request/test_url_fetcher_factory.h" | 
|  | 12 #include "net/url_request/url_fetcher.h" | 
|  | 13 #include "net/url_request/url_request_test_util.h" | 
|  | 14 #include "testing/gmock/include/gmock/gmock.h" | 
|  | 15 #include "testing/gtest/include/gtest/gtest.h" | 
|  | 16 #include "url/gurl.h" | 
|  | 17 | 
|  | 18 namespace payments { | 
|  | 19 namespace { | 
|  | 20 | 
|  | 21 class PaymentManifestDownloaderTest | 
|  | 22     : public testing::Test, | 
|  | 23       public PaymentManifestDownloader::Delegate { | 
|  | 24  public: | 
|  | 25   PaymentManifestDownloaderTest() | 
|  | 26       : context_(new net::TestURLRequestContextGetter( | 
|  | 27             base::ThreadTaskRunnerHandle::Get())), | 
|  | 28         downloader_(context_, GURL("https://bobpay.com"), this) { | 
|  | 29     downloader_.Download(); | 
|  | 30   } | 
|  | 31 | 
|  | 32   ~PaymentManifestDownloaderTest() override {} | 
|  | 33 | 
|  | 34   // PaymentManifestDownloader::Delegate | 
|  | 35   MOCK_METHOD1(OnManifestDownloadSuccess, void(const std::string& content)); | 
|  | 36   MOCK_METHOD0(OnManifestDownloadFailure, void()); | 
|  | 37 | 
|  | 38   net::TestURLFetcher* fetcher() { return factory_.GetFetcherByID(0); } | 
|  | 39 | 
|  | 40  private: | 
|  | 41   content::TestBrowserThreadBundle thread_bundle_; | 
|  | 42   net::TestURLFetcherFactory factory_; | 
|  | 43   scoped_refptr<net::TestURLRequestContextGetter> context_; | 
|  | 44   PaymentManifestDownloader downloader_; | 
|  | 45 | 
|  | 46   DISALLOW_COPY_AND_ASSIGN(PaymentManifestDownloaderTest); | 
|  | 47 }; | 
|  | 48 | 
|  | 49 TEST_F(PaymentManifestDownloaderTest, HttpHeadResponse404IsFailure) { | 
|  | 50   fetcher()->set_response_code(404); | 
|  | 51 | 
|  | 52   EXPECT_CALL(*this, OnManifestDownloadFailure()); | 
|  | 53 | 
|  | 54   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 55 } | 
|  | 56 | 
|  | 57 TEST_F(PaymentManifestDownloaderTest, NoHttpHeadersIsFailure) { | 
|  | 58   fetcher()->set_response_code(200); | 
|  | 59 | 
|  | 60   EXPECT_CALL(*this, OnManifestDownloadFailure()); | 
|  | 61 | 
|  | 62   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 63 } | 
|  | 64 | 
|  | 65 TEST_F(PaymentManifestDownloaderTest, EmptyHttpHeaderIsFailure) { | 
|  | 66   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 67       new net::HttpResponseHeaders(std::string())); | 
|  | 68   fetcher()->set_response_headers(headers); | 
|  | 69   fetcher()->set_response_code(200); | 
|  | 70 | 
|  | 71   EXPECT_CALL(*this, OnManifestDownloadFailure()); | 
|  | 72 | 
|  | 73   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 74 } | 
|  | 75 | 
|  | 76 TEST_F(PaymentManifestDownloaderTest, EmptyHttpLinkHeaderIsFailure) { | 
|  | 77   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 78       new net::HttpResponseHeaders(std::string())); | 
|  | 79   headers->AddHeader("Link:"); | 
|  | 80   fetcher()->set_response_headers(headers); | 
|  | 81   fetcher()->set_response_code(200); | 
|  | 82 | 
|  | 83   EXPECT_CALL(*this, OnManifestDownloadFailure()); | 
|  | 84 | 
|  | 85   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 86 } | 
|  | 87 | 
|  | 88 TEST_F(PaymentManifestDownloaderTest, NoRelInHttpLinkHeaderIsFailure) { | 
|  | 89   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 90       new net::HttpResponseHeaders(std::string())); | 
|  | 91   headers->AddHeader("Link: <manifest.json>"); | 
|  | 92   fetcher()->set_response_headers(headers); | 
|  | 93   fetcher()->set_response_code(200); | 
|  | 94 | 
|  | 95   EXPECT_CALL(*this, OnManifestDownloadFailure()); | 
|  | 96 | 
|  | 97   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 98 } | 
|  | 99 | 
|  | 100 TEST_F(PaymentManifestDownloaderTest, NoUrlInHttpLinkHeaderIsFailure) { | 
|  | 101   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 102       new net::HttpResponseHeaders(std::string())); | 
|  | 103   headers->AddHeader("Link: rel=payment-method-manifest"); | 
|  | 104   fetcher()->set_response_headers(headers); | 
|  | 105   fetcher()->set_response_code(200); | 
|  | 106 | 
|  | 107   EXPECT_CALL(*this, OnManifestDownloadFailure()); | 
|  | 108 | 
|  | 109   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 110 } | 
|  | 111 | 
|  | 112 TEST_F(PaymentManifestDownloaderTest, NoManifestRellInHttpLinkHeaderIsFailure) { | 
|  | 113   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 114       new net::HttpResponseHeaders(std::string())); | 
|  | 115   headers->AddHeader("Link: <manifest.json>; rel=web-app-manifest"); | 
|  | 116   fetcher()->set_response_headers(headers); | 
|  | 117   fetcher()->set_response_code(200); | 
|  | 118 | 
|  | 119   EXPECT_CALL(*this, OnManifestDownloadFailure()); | 
|  | 120 | 
|  | 121   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 122 } | 
|  | 123 | 
|  | 124 TEST_F(PaymentManifestDownloaderTest, HttpGetResponse404IsFailure) { | 
|  | 125   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 126       new net::HttpResponseHeaders(std::string())); | 
|  | 127   headers->AddHeader("Link: <manifest.json>; rel=payment-method-manifest"); | 
|  | 128   fetcher()->set_response_headers(headers); | 
|  | 129   fetcher()->set_response_code(200); | 
|  | 130   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 131   fetcher()->set_response_code(404); | 
|  | 132 | 
|  | 133   EXPECT_CALL(*this, OnManifestDownloadFailure()); | 
|  | 134 | 
|  | 135   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 136 } | 
|  | 137 | 
|  | 138 TEST_F(PaymentManifestDownloaderTest, EmptyHttpGetResponseIsFailure) { | 
|  | 139   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 140       new net::HttpResponseHeaders(std::string())); | 
|  | 141   headers->AddHeader("Link: <manifest.json>; rel=payment-method-manifest"); | 
|  | 142   fetcher()->set_response_headers(headers); | 
|  | 143   fetcher()->set_response_code(200); | 
|  | 144   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 145   fetcher()->set_response_code(200); | 
|  | 146 | 
|  | 147   EXPECT_CALL(*this, OnManifestDownloadFailure()); | 
|  | 148 | 
|  | 149   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 150 } | 
|  | 151 | 
|  | 152 TEST_F(PaymentManifestDownloaderTest, NonEmptyHttpGetResponseIsSuccess) { | 
|  | 153   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 154       new net::HttpResponseHeaders(std::string())); | 
|  | 155   headers->AddHeader("Link: <manifest.json>; rel=payment-method-manifest"); | 
|  | 156   fetcher()->set_response_headers(headers); | 
|  | 157   fetcher()->set_response_code(200); | 
|  | 158   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 159   fetcher()->SetResponseString("manifest content"); | 
|  | 160   fetcher()->set_response_code(200); | 
|  | 161 | 
|  | 162   EXPECT_CALL(*this, OnManifestDownloadSuccess("manifest content")); | 
|  | 163 | 
|  | 164   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 165 } | 
|  | 166 | 
|  | 167 TEST_F(PaymentManifestDownloaderTest, RelativeHttpHeaderLinkUrl) { | 
|  | 168   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 169       new net::HttpResponseHeaders(std::string())); | 
|  | 170   headers->AddHeader("Link: <manifest.json>; rel=payment-method-manifest"); | 
|  | 171   fetcher()->set_response_headers(headers); | 
|  | 172   fetcher()->set_response_code(200); | 
|  | 173   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 174 | 
|  | 175   EXPECT_EQ("https://bobpay.com/manifest.json", | 
|  | 176             fetcher()->GetOriginalURL().spec()); | 
|  | 177 } | 
|  | 178 | 
|  | 179 TEST_F(PaymentManifestDownloaderTest, AbsoluteHttpsHeaderLinkUrl) { | 
|  | 180   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 181       new net::HttpResponseHeaders(std::string())); | 
|  | 182   headers->AddHeader( | 
|  | 183       "Link: <https://alicepay.com/manifest.json>; " | 
|  | 184       "rel=payment-method-manifest"); | 
|  | 185   fetcher()->set_response_headers(headers); | 
|  | 186   fetcher()->set_response_code(200); | 
|  | 187   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 188 | 
|  | 189   EXPECT_EQ("https://alicepay.com/manifest.json", | 
|  | 190             fetcher()->GetOriginalURL().spec()); | 
|  | 191 } | 
|  | 192 | 
|  | 193 TEST_F(PaymentManifestDownloaderTest, AbsoluteHttpHeaderLinkUrl) { | 
|  | 194   scoped_refptr<net::HttpResponseHeaders> headers( | 
|  | 195       new net::HttpResponseHeaders(std::string())); | 
|  | 196   headers->AddHeader( | 
|  | 197       "Link: <http://alicepay.com/manifest.json>; " | 
|  | 198       "rel=payment-method-manifest"); | 
|  | 199   fetcher()->set_response_headers(headers); | 
|  | 200   fetcher()->set_response_code(200); | 
|  | 201   fetcher()->delegate()->OnURLFetchComplete(fetcher()); | 
|  | 202 | 
|  | 203   EXPECT_EQ("https://bobpay.com/", fetcher()->GetOriginalURL().spec()); | 
|  | 204 } | 
|  | 205 | 
|  | 206 }  // namespace | 
|  | 207 }  // namespace payments | 
| OLD | NEW | 
|---|