def test_CommandsReflect(self):
        
         kontext = Kontext()
         kontext['REFERER'] = 'From a UNIT TEST'
         
         c = Command("CommandsReflect")
         c.addParameter("panel",u"west")
        
 
         it = Itinerary(kontext)
         it.addInCommand(c)
         it = fwk().processItinerary(it)
         
         retJSON = fwk().CommandsToJSON(it.outCommands,it.kontext)
         print retJSON
 def test_Ping(self):
         
         kontext = Kontext()
         kontext['REFERER'] = 'From a UNIT TEST'
         
         c = Command("Ping")
         c.addParameter("name",u"echo")
         c.addParameter("age",44)
 
         it = Itinerary(kontext)
         it.addInCommand(c)
         it = fwk().processItinerary(it)
         
         retJSON = fwk().CommandsToJSON(it.outCommands,it.kontext)
         print retJSON
 def test_ShowToc(self):
         
         kontext = Kontext()
         kontext['REFERER'] = 'From a UNIT TEST'
         
         c = Command("ShowToc")
         c.addParameter("panel",u"west")
         c.addParameter("what","admin")
         c.addParameter("orientation","vertical")
 
         it = Itinerary(kontext)
         it.addInCommand(c)
         it = fwk().processItinerary(it)
         
         retJSON = fwk().CommandsToJSON(it.outCommands,it.kontext)
         print retJSON
def Request():
    
    start = time.time()
    Header("Content-type: text/html")

    if Env.REQUEST_METHOD != "POST":
        raise InvalidRequestMethod(Env.REQUEST_METHOD + " not supported")
    
    """pass http header - Context"""
    """look at headers"""
    #parse request headers into a dictionary
    headers_in  = dict((N.upper(), V) for N, V in [Item.split(':',1) for Item in Env.ALL_RAW.split('\n') if Item])
    #log(Utils().ConvertDictToString(headers_in))

    referrer = ""
    if( 'REFERER' in headers_in ):
        referrer = Utils().pack(headers_in['REFERER'])

    
    #$to do: check was a FORM application/x-www-form-urlencoded REQUEST
    #post data is base64 encoded
    
    
    post64 = Read()
    postClear = post64.decode('base64','strict')
 
    itineraryDict = json.loads(postClear)
    itinerary = fwk().parseItinerayDict(itineraryDict)
    itinerary.set_KontextVal('script','processor2.py')
    itinerary.set_KontextVal('REFERER64',referrer)
    
    retEnvIT = fwk().processItinerary(itinerary)
    retJSON = fwk().CommandsToJSON( retEnvIT.outCommands,retEnvIT.kontext )

    Write( str(retJSON) )
    
    end = time.time() - start

    #log("JSON SENT <<%s>>]" % (str(retJSON)))
    log("JSON SENT <<%s ... %s chars omitted>>]" % (str(retJSON)[0:300],(len(retJSON) -300) ))
    log(" Processing took %s seconds -------------------------------- \n \n" % (str(end)))

 


    
    
 def test_CallingPing(self):
     name = "bunny"
     c = Command("Ping")
     c.addParameter("name",name)
    
     kontext = Kontext()
     kontext.setKeyValue("user", "grant")
     itinerary = Itinerary(kontext)
     itinerary.addInCommand(c)
     
     re = fwk().processItinerary(itinerary)
     
     self.assertEquals(re.outCommands[0].name,"ReturnCommand")
     self.assertEquals(re.outCommands[0].params[0].name,"echo")
     self.assertEquals(re.outCommands[0].params[0].val,name)