OLD | NEW |
| (Empty) |
1 // Copyright 2008-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 | |
17 #include <windows.h> | |
18 #include <winhttp.h> | |
19 #include "omaha/base/app_util.h" | |
20 #include "omaha/base/string.h" | |
21 #include "omaha/net/browser_request.h" | |
22 #include "omaha/testing/unit_test.h" | |
23 | |
24 namespace omaha { | |
25 | |
26 class BrowserRequestTest : public testing::Test { | |
27 protected: | |
28 BrowserRequestTest() : are_browser_objects_available(false) { | |
29 BrowserRequest request; | |
30 are_browser_objects_available = !request.objects_.empty(); | |
31 } | |
32 | |
33 void BrowserGet(const CString& url); | |
34 void BrowserGet204(const CString& url); | |
35 void BrowserDownload(const CString& url); | |
36 | |
37 bool are_browser_objects_available; | |
38 }; | |
39 | |
40 void BrowserRequestTest::BrowserGet(const CString& url) { | |
41 if (!are_browser_objects_available) { | |
42 return; | |
43 } | |
44 | |
45 BrowserRequest browser_request; | |
46 browser_request.set_url(url); | |
47 EXPECT_HRESULT_SUCCEEDED(browser_request.Send()); | |
48 EXPECT_EQ(HTTP_STATUS_OK, browser_request.GetHttpStatusCode()); | |
49 | |
50 CString response(Utf8BufferToWideChar(browser_request.GetResponse())); | |
51 EXPECT_NE(-1, response.Find(_T("User-agent: *"))); | |
52 CString content_type; | |
53 browser_request.QueryHeadersString(WINHTTP_QUERY_CONTENT_TYPE, | |
54 NULL, &content_type); | |
55 EXPECT_STREQ(_T("text/plain"), content_type); | |
56 EXPECT_FALSE(browser_request.GetResponseHeaders().IsEmpty()); | |
57 } | |
58 | |
59 void BrowserRequestTest::BrowserGet204(const CString& url) { | |
60 if (!are_browser_objects_available) { | |
61 return; | |
62 } | |
63 | |
64 BrowserRequest browser_request; | |
65 browser_request.set_url(url); | |
66 EXPECT_HRESULT_SUCCEEDED(browser_request.Send()); | |
67 EXPECT_EQ(HTTP_STATUS_NO_CONTENT, browser_request.GetHttpStatusCode()); | |
68 CString response(Utf8BufferToWideChar(browser_request.GetResponse())); | |
69 EXPECT_EQ(-1, response.Find(_T("User-agent: *"))); | |
70 CString content_type; | |
71 browser_request.QueryHeadersString(WINHTTP_QUERY_CONTENT_TYPE, | |
72 NULL, &content_type); | |
73 EXPECT_STREQ(_T("text/html"), content_type); | |
74 EXPECT_FALSE(browser_request.GetResponseHeaders().IsEmpty()); | |
75 } | |
76 | |
77 void BrowserRequestTest::BrowserDownload(const CString& url) { | |
78 if (!are_browser_objects_available) { | |
79 return; | |
80 } | |
81 | |
82 BrowserRequest browser_request; | |
83 CString temp_dir = app_util::GetTempDir(); | |
84 CString temp_file; | |
85 EXPECT_TRUE(::GetTempFileName(temp_dir, _T("tmp"), 0, | |
86 CStrBuf(temp_file, MAX_PATH))); | |
87 browser_request.set_filename(temp_file); | |
88 browser_request.set_url(url); | |
89 | |
90 // First request. | |
91 ASSERT_HRESULT_SUCCEEDED(browser_request.Send()); | |
92 EXPECT_EQ(HTTP_STATUS_OK, browser_request.GetHttpStatusCode()); | |
93 browser_request.Close(); | |
94 | |
95 // Second request. | |
96 ASSERT_HRESULT_SUCCEEDED(browser_request.Send()); | |
97 EXPECT_EQ(HTTP_STATUS_OK, browser_request.GetHttpStatusCode()); | |
98 | |
99 EXPECT_TRUE(::DeleteFile(temp_file)); | |
100 } | |
101 | |
102 // http get. | |
103 TEST_F(BrowserRequestTest, HttpGet) { | |
104 BrowserGet(_T("http://www.google.com/robots.txt")); | |
105 } | |
106 | |
107 // https get. | |
108 TEST_F(BrowserRequestTest, HttpsGet) { | |
109 BrowserGet(_T("https://www.google.com/robots.txt")); | |
110 } | |
111 | |
112 TEST_F(BrowserRequestTest, Download) { | |
113 BrowserDownload(_T("http://dl.google.com/update2/UpdateData.bin")); | |
114 } | |
115 | |
116 // http get 204. | |
117 TEST_F(BrowserRequestTest, HttpGet204) { | |
118 BrowserGet204(_T("http://tools.google.com/service/check2")); | |
119 } | |
120 | |
121 } // namespace omaha | |
122 | |
OLD | NEW |