Esempio n. 1
0
static void test_request()
{
    static const struct {
            int         d_line;
            std::string d_input;
            std::string d_expected_output;
        } DATA[] = {
    // Check that inputs are normalized
        // LINE  // INPUT                                       // EXP
        {L_,     "GET http://google.com/ HTTP/1.1\r\n\r\n"    , "GET / HTTP/1.1\r\n"
                                                                "Host: google.com\r\n\r\n"},
        {L_,     "GET http://www.google.com/ HTTP/1.1\r\n\r\n", "GET / HTTP/1.1\r\n"
                                                                "Host: google.com\r\n\r\n"},
        {L_,     "GET www.google.com/ HTTP/1.1\r\n\r\n"       , "GET / HTTP/1.1\r\n"
                                                                "Host: google.com\r\n\r\n"},
        {L_,     "GET google.com/ HTTP/1.1\r\n\r\n"           , "GET / HTTP/1.1\r\n"
                                                                "Host: google.com\r\n\r\n"},
    // Check a HEAD Request
        // LINE  // INPUT                                       // EXP
        {L_,     "HEAD http://google.com/ HTTP/1.1\r\n\r\n"   , "HEAD / HTTP/1.1\r\n"
                                                                "Host: google.com\r\n\r\n"},
                                                               
    // Check a request with a PATH
         {L_,    "GET http://cs.ucla.edu" 
                 "/classes/fall13/cs111/news.html " 
                 "HTTP/1.1\r\n\r\n"                           ,    
                                                                "GET /classes/fall13/cs111/news.html HTTP/1.1\r\n"
                                                                "Host: cs.ucla.edu\r\n\r\n"},
    // Check a request with more headers
        {L_,     "GET http://cs.ucla.edu" 
                 "/classes/fall13/cs111/news.html " 
                 "HTTP/1.1\r\n"
                 "Accept-Encoding: identity\r\n"
                 "If-Modified-Since: Sat, 15 Feb 2014 17:00:59 GMT\r\n\r\n",
                                                                
                                                                "GET /classes/fall13/cs111/news.html HTTP/1.1\r\n"
                                                                "Host: cs.ucla.edu\r\n"
                                                                "Accept-Encoding: identity\r\n"
                                                                "If-Modified-Since: Sat, 15 Feb 2014 17:00:59 GMT\r\n\r\n"},
    };
        
    const size_t NUM_DATA = sizeof (DATA) / sizeof (*DATA);
    
    for (size_t ti = 0; ti < NUM_DATA; ++ti) {
        const int     LINE       = DATA[ti].d_line;
        const char   *INPUT      = DATA[ti].d_input.c_str();
        const size_t  LEN_INPUT  = DATA[ti].d_input.length();
        const char   *EXP_OUTPUT = DATA[ti].d_expected_output.c_str();
        
        char         *output     = new char[LEN_INPUT];
        
        if (veryVeryVerbose) { T_ P(LINE) };
        HttpRequest req;
        req.ParseRequest(INPUT, LEN_INPUT);
        req.FormatRequest(output);
        ASSERT(0 == strcmp(output, EXP_OUTPUT))
    }
}
Esempio n. 2
0
static void test_client(int testCase)
{
	bool loop = testCase == -1 ? true : false;
	int i = testCase == -1 ? 4 : testCase;
	do 
	{
	  switch(i)
	  {
		case 4: {	//test case for sending a good request
			if (verbose) std::cout << "CLIENT TEST 1" << std::endl;
			HttpRequest req;
			std::string request = "GET /index.html HTTP/1.1\r\nHost: cs.ucla.edu\r\n\r\n";
			HttpClient h("cs.ucla.edu", 80);
			h.createConnection();
			req.ParseRequest(request.c_str(), request.length());
			int status = h.sendRequest(req);
			if (veryVerbose) std::cout << "Send Status: " << status << std::endl;
			std::string b = h.getResponse().GetBody();
			if (veryVerbose) std::cout << "Body: " << b << std::endl;
		 } break;
		 case 5: {	//test case for trying to create a bad connection
			if (verbose) std::cout << "CLIENT TEST 2" << std::endl;
			HttpClient h("blah", 80);
			int status = h.createConnection();
			if (veryVerbose) std::cout << "Connection Status: " <<  status << std::endl;
			status = atoi(h.getResponse().GetStatusCode().c_str());
			if (veryVerbose) std::cout << "Response Status Code: " << status  << std::endl;
		 } break;
		 case 6: {	//test case for trying to send a request without initializing connection
			if (verbose) std::cout << "CLIENT TEST 3" << std::endl;
			HttpRequest req;
			HttpClient h("cs.ucla.edu", 80);
			std::string request = "GET /index.html HTTP/1.1\r\nHost: cs.ucla.edu\r\n\r\n";
			req.ParseRequest(request.c_str(), request.length());
			int status = h.sendRequest(req);
			if (veryVerbose) std::cout << "Send Status: " << status << std::endl;
			status = atoi(h.getResponse().GetStatusCode().c_str());
			if (veryVerbose) std::cout << "Response Status Code: " << status << std::endl;
		 } break;
		 case 7: {	//test case for testing socket timeout but with good response
			if (verbose) std::cout << "CLIENT TEST 4" << std::endl;
			HttpRequest req;
			std::string request = "GET /index.html HTTP/1.1\r\nHost: www.google.com\r\n\r\n";
			HttpClient h("www.google.com", 80);
			h.createConnection();
			req.ParseRequest(request.c_str(), request.length());
			int status = h.sendRequest(req);
			if (veryVerbose) std::cout << "Send Status: " << status << std::endl;
			status = atoi(h.getResponse().GetStatusCode().c_str());
			if (veryVerbose) std::cout << "Response Status Code: " << status << std::endl;
			std::string b = h.getResponse().GetBody();
			if (veryVerbose) std::cout << "Body: " << b << std::endl;
		 } break;
		 case 8: {	//test case for testing socket timeout with bad response
			if (verbose) std::cout << "CLIENT TEST 5" << std::endl;
			HttpRequest req;
			std::string request = "GET /index.html HTTP/1.1\r\nHost: thepiratebay.se\r\n\r\n";
			HttpClient h("thepiratebay.se", 80);
			h.createConnection();
			req.ParseRequest(request.c_str(), request.length());
			int status = h.sendRequest(req);
			if (veryVerbose)std::cout << "Send Status: " << status << std::endl;
			status = atoi(h.getResponse().GetStatusCode().c_str());
			if (veryVerbose) std::cout << "Response Status Code: " << status << std::endl;
			std::string b = h.getResponse().GetBody();
			if (veryVerbose) std::cout << "Body: " << b << std::endl;
		 } break;
		 default: {
			return;
		 } break;
	   }
	 }
	 while(loop && ++i < 9);
}