Exemplo n.º 1
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));
}
Exemplo n.º 2
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");
}
Exemplo n.º 3
0
JsObject::Ptr parse(String::CPtr urlStr) {
    if (!urlStr) return JsObject::null();

    http_parser_url info;
    std::string str = urlStr->toStdString();
    const char* cstr = str.c_str();
    int r = http_parser_parse_url(
        cstr,
        str.length(),
        0,
        &info);
    if (r) return JsObject::null();

    String::CPtr scheme   = getField(cstr, UF_SCHEMA,   &info);
    String::CPtr host     = getField(cstr, UF_HOST,     &info);
    String::CPtr port     = getField(cstr, UF_PORT,     &info);
    String::CPtr path     = getField(cstr, UF_PATH,     &info);
    String::CPtr query    = getField(cstr, UF_QUERY,    &info);
    String::CPtr fragment = getField(cstr, UF_FRAGMENT, &info);
    String::CPtr userinfo = getField(cstr, UF_USERINFO, &info);

    JsObject::Ptr urlObj = JsObject::create();

    // TODO(plenluno): protocol and host are lowercased
    urlObj->put(HREF, urlStr);

    if (scheme) {
        scheme = scheme->toLowerCase();
        StringBuilder::Ptr sb = StringBuilder::create();
        sb->appendStr(scheme);
        sb->appendChar(':');
        urlObj->put(PROTOCOL, sb->toString());
    }

    if (host) {
        host = host->toLowerCase();
        urlObj->put(HOSTNAME, host);

        if (port) {
            StringBuilder::Ptr sb = StringBuilder::create();
            sb->appendStr(host);
            sb->appendChar(':');
            sb->appendStr(port);
            urlObj->put(HOST, sb->toString());
        } else {
            urlObj->put(HOST, host);
        }
    }

    if (port) {
        urlObj->put(PORT, port);
    }

    if (path) {
        urlObj->put(PATHNAME, path);

        if (query) {
            StringBuilder::Ptr sb = StringBuilder::create();
            sb->appendStr(path);
            sb->appendChar('?');
            sb->appendStr(query);
            urlObj->put(PATH, sb->toString());
        } else {
            urlObj->put(PATH, path);
        }
    }

    if (query) {
        urlObj->put(QUERY, query);

        StringBuilder::Ptr sb = StringBuilder::create();
        sb->appendChar('?');
        sb->appendStr(query);
        urlObj->put(SEARCH, sb->toString());
    }

    if (fragment) {
        StringBuilder::Ptr sb = StringBuilder::create();
        sb->appendChar('#');
        sb->appendStr(fragment);
        urlObj->put(HASH, sb->toString());
    }

    if (userinfo) {
        urlObj->put(AUTH, userinfo);
    }

    return urlObj;
}
Exemplo n.º 4
0
TEST(GTestQueryString, TestStringify) {
    JsObject::Ptr obj = JsObject::create();
    String::CPtr query = querystring::stringify(obj);
    ASSERT_TRUE(query->equals(String::create()));

    obj = JsObject::create();
    obj->put(String::create("a"), String::create("b"));
    query = querystring::stringify(obj);
    ASSERT_TRUE(query->equals(String::create("a=b")));

    obj = JsObject::create();
    obj->put(String::create("a"), String::create("b"));
    obj->put(String::create("x"), 1);
    query = querystring::stringify(obj);
    ASSERT_TRUE(query->equals(String::create("a=b&x=1")));

    obj = JsObject::create();
    obj->put(String::create("a"), JsObject::create());
    query = querystring::stringify(obj);
    ASSERT_TRUE(query->equals(String::create("a=")));

    obj = JsObject::create();
    obj->put(String::create("a"), JsArray::create());
    query = querystring::stringify(obj);
    ASSERT_TRUE(query->equals(String::create()));

    obj = JsObject::create();
    obj->put(String::create("a"), Object::null());
    query = querystring::stringify(obj);
    ASSERT_TRUE(query->equals(String::create("a=")));

    obj = JsObject::create();
    obj->put(String::create("a"), UNDEFINED);
    query = querystring::stringify(obj);
    ASSERT_TRUE(query->equals(String::create("a=")));

    obj = JsObject::create();
    obj->put(String::create(), String::create());
    query = querystring::stringify(obj);
    ASSERT_TRUE(query->equals(String::create("=")));

    obj = JsObject::create();
    obj->put(Object::null(), Object::null());
    query = querystring::stringify(obj);
    ASSERT_TRUE(query->equals(String::create("null=")));

    obj = JsObject::create();
    JsArray::Ptr ary = JsArray::create();
    ary->add(String::create("xyz"));
    ary->add(123);
    obj->put(UNDEFINED, ary);
    query = querystring::stringify(obj);
    ASSERT_TRUE(query->equals(
        String::create("undefined=xyz&undefined=123")));

    obj = JsObject::create();
    obj->put(String::create(" "), String::create(" "));
    query = querystring::stringify(obj);
    ASSERT_TRUE(query->equals(String::create("%20=%20")));
}
Exemplo n.º 5
0
JsObject::Ptr parse(String::CPtr urlStr) {
    static const String::CPtr colon = String::create(":");
    static const String::CPtr slash = String::create("/");
    static const String::CPtr sharp = String::create("#");
    static const String::CPtr question = String::create("?");

    if (!urlStr) {
        LIBJ_NULL_PTR(JsObject, nullp);
        return nullp;
    }

    struct parsed_url* url = parse_url(urlStr->toStdString().c_str());
    JsObject::Ptr obj = JsObject::create();

    obj->put(HREF, urlStr);
    if (url->scheme) {
        obj->put(PROTOCOL, String::create(url->scheme)->toLowerCase());
    }
    LIBJ_NULL_CPTR(String, port);
    if (url->port) {
        port = String::create(url->port);
        obj->put(PORT, port);
    }
    if (url->host) {
        String::CPtr hostname = String::create(url->host)->toLowerCase();
        obj->put(HOSTNAME, hostname);
        if (port) {
            obj->put(HOST, hostname->concat(colon)->concat(port));
        } else {
            obj->put(HOST, hostname);
        }
    }
    LIBJ_NULL_CPTR(String, query);
    if (url->query) {
        query = String::create(url->query);
        obj->put(QUERY, query);
    }
    if (url->path) {
        String::CPtr pathname = slash->concat(String::create(url->path));
        obj->put(PATHNAME, pathname);
        if (query) {
            obj->put(PATH, pathname->concat(question)->concat(query));
        } else {
            obj->put(PATH, pathname);
        }
    }
    if (url->username && url->password) {
        String::CPtr auth = String::create(url->username);
        auth = auth->concat(colon);
        auth = auth->concat(String::create(url->password));
        obj->put(AUTH, auth);
    }
    if (url->fragment) {
        String::CPtr hash = sharp->concat(String::create(url->fragment));
        obj->put(HASH, hash);
    }

    parsed_url_free(url);
    return obj;
}