#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "testing/gtest/include/gtest/gtest.h"
namespace {
std::string s_test_app_path;
std::string DirName(const std::string& file_path) {
size_t pos = file_path.find_last_of("\\/");
if (std::string::npos == pos)
return std::string();
return file_path.substr(0, pos + 1);
}
std::string RunCommand(std::string command_line) {
std::string result_out = "command_result.tmp";
EXPECT_EQ(0, std::system((command_line + " >" + result_out).c_str()));
std::stringstream result;
result << std::ifstream(result_out).rdbuf();
std::remove(result_out.c_str());
return result.str();
}
TEST(SampleTest, TestConnectionRefused) {
std::string cronet_sample_path = DirName(s_test_app_path) + "cronet_sample";
std::string url = "http://localhost:99999";
std::string sample_out = RunCommand(cronet_sample_path + " " + url);
EXPECT_NE(std::string::npos, sample_out.find("net::ERR_INVALID_URL"));
}
}
int main(int argc, char** argv) {
s_test_app_path = argv[0];
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}