📄 cgitest.cpp
字号:
/* * =========================================================================== * PRODUCTION $Log: cgitest.cpp,v $ * PRODUCTION Revision 1000.2 2004/06/01 18:39:31 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.20 * PRODUCTION * =========================================================================== *//* $Id: cgitest.cpp,v 1000.2 2004/06/01 18:39:31 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Denis Vakatov, Eugene Vasilchenko, Vsevolod Sandomirsky * * File Description: * TEST for: NCBI C++ core CGI API * Note: this is a CGI part of former "coretest.cpp" test * */#include <ncbi_pch.hpp>#include <corelib/ncbiapp.hpp>#include <corelib/ncbienv.hpp>#include <corelib/ncbireg.hpp>#include <cgi/ncbires.hpp>#include <cgi/ncbicgir.hpp>#include <algorithm>#include <time.h>#include <stdio.h>#include <stdlib.h>#include <test/test_assert.h> /* This header must go last */// This is to use the ANSI C++ standard templates without the "std::" prefix// and to use NCBI C++ entities without the "ncbi::" prefixUSING_NCBI_SCOPE;/////////////////////////////////// CGI//static void TestCgi_Cookies(void){ CCgiCookies cookies("coo1=kie1BAD1;coo2=kie2_ValidPath; "); cookies.Add(" coo1=kie1BAD2;CooT=KieT_ExpTime "); string str = "eee; Coo11=Kie11_OK; Coo2=Kie2BAD; uuu; coo1=Kie1_OK; Coo6=kie6; iii"; cookies.Add(str); cookies.Add("RemoveThisCookie", "BAD"); cookies.Add(str); assert( !cookies.Find("Coo2", "qq.rr.oo", NcbiEmptyString) ); assert(cookies.Find("Coo2") == cookies.Find("Coo2", "", "")); cookies.Find("Coo2")->SetValue("Kie2_OK"); CCgiCookie c0("Coo5", "Kie5BAD"); CCgiCookie c1("Coo5", "Kie", "aaa.bbb.ccc", "/"); CCgiCookie c2(c1); c2.SetValue("Kie5_Dom_Sec"); c2.SetPath(""); c2.SetSecure(true); cookies.Add(c1); cookies.Add(c2); CCgiCookie* c3 = cookies.Find("coo2", NcbiEmptyString, ""); c3->SetPath("coo2_ValidPath"); assert( !cookies.Remove(cookies.Find("NoSuchCookie")) ); assert( cookies.Remove(cookies.Find("RemoveThisCookie")) ); assert( !cookies.Remove(cookies.Find("RemoveThisCookie")) ); assert( !cookies.Find("RemoveThisCookie") ); assert( cookies.Find("CoO5") ); assert( cookies.Find("cOo5") == cookies.Find("Coo5", "aaa.bBb.ccC", "") ); assert( cookies.Find("Coo5")->GetDomain() == "aaa.bbb.ccc" ); assert( cookies.Find("coo2")->GetDomain().empty() ); assert( cookies.Find("cOO5")->GetSecure() ); assert( !cookies.Find("cOo2")->GetSecure() ); time_t timer = time(0); tm *date = gmtime(&timer); CCgiCookie *ct = cookies.Find("CooT"); ct->SetExpDate(*date); cookies.Add("AAA", "11", "qq.yy.dd"); cookies.Add("AAA", "12", "QQ.yy.Dd"); assert( cookies.Find("AAA", "qq.yy.dd", NcbiEmptyString)->GetValue() == "12" ); cookies.Add("aaa", "1", "QQ.yy.Dd"); assert( cookies.Find("AAA", "qq.yy.dd", NcbiEmptyString)->GetValue() == "1" ); cookies.Add("aaa", "19"); cookies.Add("aaa", "21", "QQ.yy.Dd", "path"); assert( cookies.Find("AAA", "qq.yy.dd", "path")->GetValue() == "21"); cookies.Add("aaa", "22", "QQ.yy.Dd", "Path"); assert( cookies.Find("AAA", "qq.yy.dd", "path")->GetValue() == "21" ); assert( cookies.Find("AAA")->GetValue() == "19" ); cookies.Add("AAA", "2", "QQ.yy.Dd", "path"); assert( cookies.Find("AAA", "qq.yy.dd", "path")->GetValue() == "2" ); cookies.Add("AAA", "3"); cookies.Add("BBA", "BBA1"); cookies.Add("BBA", "BBA2", "", "path"); cookies.Add("BBB", "B1", "oo.pp.yy"); cookies.Add("BBB", "B2"); cookies.Add("BBB", "B3", "bb.pp.yy", "path3"); cookies.Add("BBB", "B3", "cc.pp.yy", "path3"); CCgiCookies::TRange range; assert( cookies.Find("BBA", &range)->GetValue() == "BBA1" ); assert( cookies.Remove(range) ); cookies.Add("BBB", "B3", "dd.pp.yy", "path3"); cookies.Add("BBB", "B3", "aa.pp.yy", "path3"); cookies.Add("BBB", "B3", "", "path3"); cookies.Add("BBC", "BBC1", "", "path"); cookies.Add("BBC", "BBC2", "44.88.99", "path"); cookies.Add("BBD", "BBD1", "", "path"); cookies.Add("BBD", "BBD20", "44.88.99", "path"); cookies.Add("BBD", "BBD2", "44.88.99", "path"); cookies.Add("BBD", "BBD3", "77.99.00", "path"); assert( cookies.Remove( cookies.Find("BBB", "dd.pp.yy", "path3") ) ); cookies.Add("DDD", "DDD1", "aa.bb.cc", "p1/p2"); cookies.Add("DDD", "DDD2", "aa.bb.cc"); cookies.Add("DDD", "DDD3", "aa.bb.cc", "p3/p4"); cookies.Add("DDD", "DDD4", "aa.bb.cc", "p1"); cookies.Add("DDD", "DDD5", "aa.bb.cc", "p1/p2/p3"); cookies.Add("DDD", "DDD6", "aa.bb.cc", "p1/p4"); assert( cookies.Find("DDD", &range)->GetValue() == "DDD2" ); assert( cookies.Find("BBD", "44.88.99", "path")->GetValue() == "BBD2" ); assert( cookies.Remove(cookies.Find("BBD", "77.99.00", "path")) ); assert( cookies.Remove("BBD") == 2 ); NcbiCerr << "\n\nCookies:\n\n" << cookies << NcbiEndl;}static void PrintEntries(TCgiEntries& entries){ for (TCgiEntries::iterator iter = entries.begin(); iter != entries.end(); ++iter) { assert( !NStr::StartsWith(iter->first, "amp;", NStr::eNocase) ); NcbiCout << " (\"" << iter->first << "\", \"" << iter->second << "\")" << NcbiEndl; }}static bool TestEntries(TCgiEntries& entries, const string& str){ NcbiCout << "\n Entries: `" << str << "'\n"; SIZE_TYPE err_pos = CCgiRequest::ParseEntries(str, entries); PrintEntries(entries); if ( err_pos ) { NcbiCout << "-- Error at position #" << err_pos << NcbiEndl; return false; } return true;}static void PrintIndexes(TCgiIndexes& indexes){ for (TCgiIndexes::iterator iter = indexes.begin(); iter != indexes.end(); ++iter) { NcbiCout << " \"" << *iter << "\" "; } NcbiCout << NcbiEndl;}static bool TestIndexes(TCgiIndexes& indexes, const string& str){ NcbiCout << "\n Indexes: `" << str << "'\n"; SIZE_TYPE err_pos = CCgiRequest::ParseIndexes(str, indexes); PrintIndexes(indexes); if ( err_pos ) { NcbiCout << "-- Error at position #" << err_pos << NcbiEndl; return false; } return true;}static void TestCgi_Request_Static(void){ // Test CCgiRequest::ParseEntries() TCgiEntries entries; assert( TestEntries(entries, "aa=bb&cc=dd") ); assert( TestEntries(entries, "e%20e=f%26f&g%2Ag=h+h%2e") ); entries.clear(); assert( !TestEntries(entries, " xx=yy") ); assert( TestEntries(entries, "xx=&yy=zz") ); assert( TestEntries(entries, "rr=") ); assert( !TestEntries(entries, "xx&") ); entries.clear(); // Give a temporary slack to some buggy clients -- allow leading ampersand // assert( !TestEntries(entries, "&zz=qq") ); assert( TestEntries(entries, "&zz=qq") ); // assert( !TestEntries(entries, "tt=qq=pp") ); assert( !TestEntries(entries, "=ggg&ppp=PPP") ); assert( TestEntries(entries, "a=d&eee") ); assert( TestEntries(entries, "xxx&eee") ); assert( TestEntries(entries, "xxx+eee") ); assert( TestEntries(entries, "UUU") ); assert( !TestEntries(entries, "a=d&&eee") ); assert( TestEntries(entries, "a%21%2f%25aa=%2Fd%2c&eee=%3f") ); // some older browsers fail to parse & in HREFs; ensure that // we handle it properly. assert( TestEntries(entries, "a=b&c=d&e=f") ); // Test CCgiRequest::ParseIndexes() TCgiIndexes indexes; assert( TestIndexes(indexes, "a+bb+ccc+d") ); assert( TestIndexes(indexes, "e%20e+f%26f+g%2Ag+hh%2e") ); indexes.clear(); assert( !TestIndexes(indexes, " jhg") ); assert( !TestIndexes(indexes, "e%h%2e+3") ); assert( TestIndexes(indexes, "aa+%20+bb") ); assert( !TestIndexes(indexes, "aa++bb") ); indexes.clear(); assert( !TestIndexes(indexes, "+1") ); assert( !TestIndexes(indexes, "aa+") ); assert( !TestIndexes(indexes, "aa+bb ") ); assert( !TestIndexes(indexes, "c++b") ); assert( !TestIndexes(indexes, "ff++ ") ); assert( !TestIndexes(indexes, "++") );}static void TestCgi_Request_Full(CNcbiIstream* istr, const CNcbiArguments* args = 0, CCgiRequest::TFlags flags = 0){ CCgiRequest CCR(args, 0, istr, flags); NcbiCout << "\n\nCCgiRequest::\n"; try { NcbiCout << "GetContentLength(): " << CCR.GetContentLength() << NcbiEndl; } STD_CATCH ("TestCgi_Request_Full"); NcbiCout << "GetRandomProperty(\"USER_AGENT\"): " << CCR.GetRandomProperty("USER_AGENT") << NcbiEndl; NcbiCout << "GetRandomProperty(\"MY_RANDOM_PROP\"): " << CCR.GetRandomProperty("MY_RANDOM_PROP") << NcbiEndl; NcbiCout << "GetRandomProperty(\"HTTP_MY_RANDOM_PROP\"): " << CCR.GetRandomProperty("HTTP_MY_RANDOM_PROP") << NcbiEndl; NcbiCout << "GetRandomProperty(\"HTTP_MY_RANDOM_PROP\", false): " << CCR.GetRandomProperty("HTTP_MY_RANDOM_PROP", false) << NcbiEndl; NcbiCout << "\nCCgiRequest:: All properties:\n"; for (size_t prop = 0; prop < (size_t)eCgi_NProperties; prop++) { NcbiCout << NcbiSetw(24) << CCgiRequest::GetPropertyName((ECgiProp)prop) << " = \"" << CCR.GetProperty((ECgiProp)prop) << "\"\n"; } CCgiCookies cookies; {{ // Just an example of copying the cookies from a request data // Of course, we could use the original request's cookie set // ("x_cookies") if we performed only "const" operations on it const CCgiCookies& x_cookies = CCR.GetCookies(); cookies.Add(x_cookies); }} NcbiCout << "\nCCgiRequest:: All cookies:\n"; if ( cookies.Empty() ) NcbiCout << "No cookies specified" << NcbiEndl; else NcbiCout << cookies << NcbiEndl; TCgiEntries entries = CCR.GetEntries(); NcbiCout << "\nCCgiRequest:: All entries:\n"; if ( entries.empty() ) { NcbiCout << "No entries specified" << NcbiEndl; } else { PrintEntries(entries); if ( !CCR.GetEntry("get_query1").empty() ) { NcbiCout << "GetEntry() check." << NcbiEndl; assert(CCR.GetEntry("get_query1") == "gq1"); bool is_found = false; assert(CCR.GetEntry("get_query1", &is_found) == "gq1"); assert(is_found); assert(CCR.GetEntry("get_query2", 0).empty()); is_found = false; assert(CCR.GetEntry("get_query2", &is_found).empty()); assert(is_found); assert(CCR.GetEntry("qwe1rtyuioop", &is_found).empty()); assert(!is_found); } } TCgiIndexes indexes = CCR.GetIndexes(); NcbiCout << "\nCCgiRequest:: ISINDEX values:\n"; if ( indexes.empty() ) { NcbiCout << "No ISINDEX values specified" << NcbiEndl; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -