TEST_F(HttpClientTest, GetWithBadURI) { HttpClient client; HttpResponse response; HttpClient::ErrorCode error; std::string bad_uri = "http://-www.qq.com"; EXPECT_FALSE(client.Get(bad_uri, &response, &error)); EXPECT_EQ(HttpClient::ERROR_INVALID_URI_ADDRESS, error); }
TEST_F(HttpClientTest, GetWithBadProxy) { HttpClient client; HttpResponse response; HttpClient::ErrorCode error; client.SetProxy("http://-proxy.tencent.com:8080"); EXPECT_FALSE(client.Get(m_server_address + "/hello.txt", &response, &error)); EXPECT_EQ(HttpClient::ERROR_INVALID_PROXY_ADDRESS, error); }
void SingleHiScoreTable::HiScoreUrlThread::Work() { //std::cout << "In thread: URL for hi scores: " << m_url << "\n"; HttpClient h; HttpResult res; h.Get(m_url, HttpClient::GET, &res); #ifdef _DEBUG std::cout << "Url thread has now finished.\n"; #endif }
TEST_F(HttpClientTest, GetWithoutProxy) { HttpClient client; HttpResponse response; HttpClient::Options options; options.AddHeader("Content-Type", "text/plain"); client.SetUserAgent("TestDownloader"); HttpClient::ErrorCode error; EXPECT_TRUE(client.Get(m_server_address + "/robots.txt", &response, &error)); EXPECT_EQ(HttpClient::SUCCESS, error); EXPECT_EQ(HttpResponse::Status_OK, response.Status()); EXPECT_FALSE(response.Body().empty()); EXPECT_FALSE(client.Get(m_server_address + "/world", &response, &error)); EXPECT_EQ(HttpClient::ERROR_HTTP_STATUS_CODE, error); EXPECT_EQ(HttpResponse::Status_NotFound, response.Status()); EXPECT_FALSE(client.Get("http://127.0.0.1:8/world", &response, &error)); EXPECT_EQ(HttpClient::ERROR_FAIL_TO_CONNECT_SERVER, error); EXPECT_STREQ("Failed to connect to server", client.GetErrorMessage(error)); // FIXME(chen3feng) : On some env, below cases will fail. // EXPECT_FALSE(client.Get("http://non-exist-domaon.test/world", &response, &error)); // EXPECT_EQ(HttpClient::ERROR_FAIL_TO_RESOLVE_ADDRESS, error); // EXPECT_STREQ("Failed to resolve address", client.GetErrorMessage(error)); // not supported scheme EXPECT_FALSE(client.Get("ftp://127.0.0.1/hello.txt", &response, &error)); // Receive with small response length HttpClient client2; options.SetMaxResponseLength(3); EXPECT_FALSE(client2.Get(m_server_address + "/hello.txt", options, &response, &error)); // room is not enough to store a complete http response header EXPECT_NE(error, HttpClient::SUCCESS); }