
var assert = require('../../lib/common/Assertions.js');

describe("RequestProcessor", function () {

    it("require", function () {
        expect(function() {
          var RequestProcessor = require('../../lib/net/RequestProcessor.js').RequestProcessor;
          expect(RequestProcessor).toBeTruthy();
          assert.isFunction(RequestProcessor);
        }).not.toThrow();
    });
    
    it("creation", function () {
        expect(function () {
            var RequestProcessor = require('../../lib/net/RequestProcessor.js').RequestProcessor;
            var rp = new RequestProcessor();
        }).not.toThrow();
    });
    
    it("attachKeepAliveStats", function () {
        expect(function () {
            var RequestProcessor = require('../../lib/net/RequestProcessor.js').RequestProcessor;
            var Request = require('../../lib/net/Request.js').Request;
            var Response = require('../../lib/net/Response.js').Response;
            
            var EventEmitter = require("events").EventEmitter;
            
            var req = new Request('http://www.onet.pl');
            var res = new Response();
            res.setStatusCode(200);
            res._httpServerResponse = {};
            res._httpServerResponse.socket = new EventEmitter();
            
            // adding (emulate) default node http listener
            expect(res._httpServerResponse.socket.listeners('close').length).toBe(0);
            res._httpServerResponse.socket.on('close', function(){});            
            expect(res._httpServerResponse.socket.listeners('close').length).toBe(1);
            
            var rp = new RequestProcessor({});
            rp._attachKeepAliveStats(req, res);
            expect(res._httpServerResponse.socket.listeners('close').length).toBe(2);
            rp._attachKeepAliveStats(req, res);
            expect(res._httpServerResponse.socket.listeners('close').length).toBe(2);
            
            var consoleInfo = null;
            
            spyOn(console, 'info').andCallFake(function (msg, tt, code, url, first_req_ts, req_count,  socket_ttl, req_ttl) {
                consoleInfo = true;
                expect(tt).toBe(0);
                expect(code).toBe(200);
                expect(url).toBe('http://www.onet.pl/');
                expect(first_req_ts).toBeGreaterThan(+ new Date(0));
                expect(req_count).toBe(2);
                expect(socket_ttl).toBeGreaterThan(-1);
                expect(req_ttl).toBeGreaterThan(-1);
            });
            
            res._httpServerResponse.socket.emit('close');
            
            waitsFor(function() {
                return consoleInfo;
            }, "console.info with stats never emitted", 1000);
            
            
        }).not.toThrow();
    });
    
    it("requestLine should have query string information", function () {
        var RequestProcessor = require('../../lib/net/RequestProcessor.js').RequestProcessor;
        var Request = require('../../lib/net/Request.js').Request;
        var Response = require('../../lib/net/Response.js').Response;
        var req = null;
        var res = new Response();
        res._httpServerResponse = {
            socket: null
        }
        var rp = new RequestProcessor({});
        
        var host = "http://www.onet.pl"
        
        req = new Request(host);
        rp.process(req, res);
        expect(rp._requestLine).toBe('"GET / 1.0"')
        
        req = new Request(host + '/');
        rp.process(req, res);
        expect(rp._requestLine).toBe('"GET / 1.0"')
        
        req = new Request(host + '/path');
        rp.process(req, res);
        expect(rp._requestLine).toBe('"GET /path 1.0"')
        
        req = new Request(host + '/path/');
        rp.process(req, res);
        expect(rp._requestLine).toBe('"GET /path/ 1.0"')
        
        req = new Request(host + '/path/index.html');
        rp.process(req, res);
        expect(rp._requestLine).toBe('"GET /path/index.html 1.0"')
        
        req = new Request(host + '/path//index.html');
        rp.process(req, res);
        expect(rp._requestLine).toBe('"GET /path//index.html 1.0"')
        
        req = new Request(host + '/path//index.html//');
        rp.process(req, res);
        expect(rp._requestLine).toBe('"GET /path//index.html// 1.0"')
        
        req = new Request(host + '/path/index.html?v=1');
        rp.process(req, res);
        expect(rp._requestLine).toBe('"GET /path/index.html?v=1 1.0"')
        
        req = new Request(host + '/path/index.html?v=1&t=2');
        rp.process(req, res);
        expect(rp._requestLine).toBe('"GET /path/index.html?v=1&t=2 1.0"')
        
        req = new Request(host + '/path/index.html?');
        rp.process(req, res);
        expect(rp._requestLine).toBe('"GET /path/index.html 1.0"')
    });
    
    
});