Пример #1
0
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);
}