Ejemplo 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))
    }
}