TEST(GTestDns, TestHttpGet) { GTestOnEnd::clear(); GTestHttpClientOnResponse::clear(); String::CPtr url = String::create("http://nodejs.org"); JsObject::Ptr headers = JsObject::create(); #ifdef USE_PROXY String::CPtr host = String::create("nodejs.org"); String::CPtr proxyHost = String::create("proxy"); String::CPtr proxyPort = String::create("8080"); JsObject::Ptr options = JsObject::create(); options->put(http::OPTION_HEADERS, headers); options->put(http::OPTION_PATH, url); options->put(http::OPTION_HOST, proxyHost); options->put(http::OPTION_PORT, proxyPort); headers->put(http::HEADER_HOST, host); #else JsObject::Ptr options = url::parse(url); #endif headers->put(http::HEADER_CONNECTION, String::create("close")); GTestHttpClientOnResponse::Ptr onResponse(new GTestHttpClientOnResponse()); http::get(options, onResponse); node::run(); JsArray::CPtr msgs = GTestOnEnd::messages(); JsArray::CPtr codes = GTestHttpClientOnResponse::statusCodes(); ASSERT_EQ(1, msgs->length()); ASSERT_EQ(1, codes->length()); ASSERT_TRUE(msgs->getCPtr<String>(0)); ASSERT_TRUE(codes->get(0).equals(200)); // console::log(msgs->get(0)); }
String::CPtr resolve(JsArray::CPtr paths) { const Size kMax = 8192; char dir[kMax]; char* r = getcwd(dir, kMax); if (!r) return String::null(); if (!paths) return String::create(dir); StringBuilder::Ptr resolved = StringBuilder::create(); resolved->appendStr(dir); Size len = paths->length(); for (Size i = 0; i < len; i++) { String::CPtr path = paths->getCPtr<String>(i); if (path) { if (isSep(path->charAt(0))) { resolved = StringBuilder::create(); #ifdef LIBJ_PF_WINDOWS // append the drive letter resolved->appendChar(dir[0]); resolved->appendChar(dir[1]); } else if (isAlpha(path->charAt(0)) && path->charAt(1) == ':') { resolved = StringBuilder::create(); #endif } else if (!path->isEmpty()) { resolved->appendChar(SEP); } resolved->appendStr(path); } } return trimSeps(normalize(resolved->toString())); }
TEST(GTestHttpEcho, TestConnectionClose) { GTestOnEnd::clear(); GTestOnClose::clear(); GTestHttpServerOnEnd::clear(); GTestHttpClientOnResponse::clear(); String::CPtr msg = String::create("xyz"); http::Server::Ptr srv = http::Server::create(); GTestHttpServerOnRequest::Ptr onRequest( new GTestHttpServerOnRequest(srv, NUM_REQS)); srv->on(http::Server::EVENT_REQUEST, onRequest); srv->listen(10000); String::CPtr url = String::create("http://127.0.0.1:10000/abc"); JsObject::Ptr options = url::parse(url); JsObject::Ptr headers = JsObject::create(); headers->put( http::HEADER_CONNECTION, String::create("close")); headers->put( http::HEADER_CONTENT_LENGTH, String::valueOf(Buffer::byteLength(msg))); options->put(http::OPTION_HEADERS, headers); GTestHttpClientOnResponse::Ptr onResponse(new GTestHttpClientOnResponse()); for (Size i = 0; i < NUM_REQS; i++) { http::ClientRequest::Ptr req = http::request(options, onResponse); req->write(msg); req->end(); } node::run(); ASSERT_EQ(0, GTestOnClose::count()); JsArray::CPtr messages = GTestOnEnd::messages(); JsArray::CPtr statusCodes = GTestHttpClientOnResponse::statusCodes(); Size numMsgs = messages->length(); Size numCodes = statusCodes->length(); ASSERT_EQ(NUM_REQS, numMsgs); ASSERT_EQ(NUM_REQS, numCodes); for (Size i = 0; i < numMsgs; i++) { ASSERT_TRUE(messages->get(i).equals(msg)); ASSERT_TRUE(statusCodes->get(i).equals(200)); console::printf(console::INFO, "."); } console::printf(console::INFO, "\n"); }
String::CPtr join(JsArray::CPtr paths) { LIBJ_STATIC_SYMBOL_DEF(symCurrent, "."); if (!paths) return symCurrent; StringBuilder::Ptr joined = StringBuilder::create(); Size len = paths->length(); Boolean first = true; for (Size i = 0; i < len; i++) { String::CPtr path = paths->getCPtr<String>(i); if (path) { if (first) { first = false; } else { joined->appendChar(SEP); } joined->appendStr(path); } } return normalize(joined->toString()); }