Example #1
0
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");
}
Example #2
0
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));
}