# The Grinder 3.4
# HTTP script recorded by TCPProxy at Apr 12, 2011 11:19:28 AM

from net.grinder.script import Test as StandardTest

import script_offset

def Test(number, description):
    # Adjust to the appropriate offset.
    return StandardTest(number + script_offset.get_offset(), description) #@UndefinedVariable

from net.grinder.script.Grinder import grinder
from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest
from HTTPClient import NVPair
connectionDefaults = HTTPPluginControl.getConnectionDefaults()
connectionDefaults.setFollowRedirects(0)
httpUtilities = HTTPPluginControl.getHTTPUtilities()

import thread_data as td #@UnusedImport
import text_processor as tp #@UnusedImport
import pool_data as pd #@UnusedImport

# To use a proxy server, uncomment the next line and set the host and port.
# connectionDefaults.setProxyServer("localhost", 8001)

# These definitions at the top level of the file are evaluated once,
# when the worker process is started.

connectionDefaults.defaultHeaders = \
    [ NVPair('Accept-Language', 'en-us,en;q=0.5'),
        NVPair('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
        NVPair('Accept-Encoding', 'gzip, deflate'),
        NVPair('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64; rv:2.0) Gecko/20100101 Firefox/4.0'), ]

headers0= \
    [ NVPair('Accept', 'image/png,image/*;q=0.8,*/*;q=0.5'),
        NVPair('Referer', 'http://10.1.3.81:8080/sustain/gzip_N1493083242/assets/dashboard/dashboard.css'), ]

headers1= \
    [ NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
        NVPair('Referer', 'http://10.1.3.81:8080/sustain/cms/dashboard'), ]

headers2= \
    [ NVPair('Accept', '*/*'),
        NVPair('Referer', 'http://10.1.3.81:8080/sustain/cms/dashboard'), ]

headers3= \
    [ NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'), ]

headers4= \
    [ NVPair('Accept', 'text/javascript, text/html, application/xml, text/xml, */*'),
        NVPair('Referer', 'http://10.1.3.81:8080/sustain/cms/dashboard'),
        NVPair('Cache-Control', 'no-cache'), ]

headers5= \
    [ NVPair('Accept', 'image/png,image/*;q=0.8,*/*;q=0.5'),
        NVPair('Referer', 'http://10.1.3.81:8080/sustain/gzip_N956284587/assets/css/base.css'), ]

url0 = grinder.getProperties().getProperty('ecourt.url', 'http://10.1.3.81:8080')

# Create an HTTPRequest for each request, then replace the
# reference to the HTTPRequest with an instrumented version.
# You can access the unadorned instance using request101.__target__.
request101 = HTTPRequest(url=url0, headers=headers0)
request101 = Test(101, 'GET icon-dashboard-delete-tab-hover.png').wrap(request101)

# click on the ecourt logo
request201 = HTTPRequest(url=url0, headers=headers1)
request201 = Test(201, 'GET dashboard').wrap(request201)

request301 = HTTPRequest(url=url0, headers=headers2)
request301 = Test(301, 'GET leftmenu-DEFAULT-t1302607106557_en_US.javascript').wrap(request301)

request302 = HTTPRequest(url=url0, headers=headers3)
request302 = Test(302, 'GET eCourt.ico').wrap(request302)

request401 = HTTPRequest(url=url0, headers=headers4)
request401 = Test(401, 'POST summary').wrap(request401)

request501 = HTTPRequest(url=url0, headers=headers4)
request501 = Test(501, 'POST content').wrap(request501)

request502 = HTTPRequest(url=url0, headers=headers5)
request502 = Test(502, 'GET ygtvlinklabel-hover-bg.png').wrap(request502)


class TestRunner:
    """A TestRunner instance is created for each worker thread."""

    # A method for each recorded page.
    def page1(self):
        """GET icon-dashboard-delete-tab-hover.png (request 101)."""
        result = request101.GET('/sustain/cb2236986055/assets/dashboard/images/controls/icon-dashboard-delete-tab-hover.png')
        tp.process_result()

        return result

    def page2(self):
        """GET dashboard (request 201)."""
        result = request201.GET('/sustain/cms/dashboard')
        tp.process_result()
        # 4 different values for token_dispatch found in response, using the first one.
        self.token_dispatch = \
            httpUtilities.valueFromBodyURI('dispatch') # 'onView'
        # 5 different values for token_formCode found in response, using the first one.
        self.token_formCode = \
            httpUtilities.valueFromBodyURI('formCode') # 'S-PARTY'
        # 3 different values for token_format found in response, using the first one.
        self.token_format = \
            httpUtilities.valueFromBodyURI('format') # 'daily'
        self.token_helpKey = \
            httpUtilities.valueFromBodyURI('helpKey') # '.dashboard'
        # 2 different values for token_page found in response, using the first one.
        self.token_page = \
            httpUtilities.valueFromBodyURI('page') # '1'
        # 19 different values for token_id found in response, using the first one.
        self.token_id = \
            httpUtilities.valueFromBodyURI('id') # '3914615'

        return result

    def page3(self):
        """GET leftmenu-DEFAULT-t1302607106557_en_US.javascript (requests 301-302)."""
        result = request301.GET('/sustain/menu-left/leftmenu-DEFAULT-t1302607106557_en_US.javascript', None,
            ( NVPair('If-Modified-Since', 'Tue, 12 Apr 2011 11:18:26 GMT'),
                NVPair('If-None-Match', 'W/\"30174-1302607106000\"'), ))
        tp.process_result()

        grinder.sleep(20)
        request302.GET('/sustain/eCourt.ico', None,
            ( NVPair('If-Modified-Since', 'Tue, 12 Apr 2011 11:01:40 GMT'),
                NVPair('If-None-Match', 'W/\"56745-1302606100000\"'), ))
        tp.process_result()

        return result

    def page4(self):
        """POST summary (request 401)."""
        self.token_compact = \
            'true'
        self.token_assignedTo = \
            'Myself'
        result = request401.POST('/sustain/cms/workspace/summary' +
            '?compact=' +
            self.token_compact +
            '&assignedTo=' +
            self.token_assignedTo,
            '',
            ( NVPair('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'), ))
        tp.process_result()
        self.token_dispatch = \
            httpUtilities.valueFromBodyURI('dispatch') # 'onView'
        # 16 different values for token_wmActivityId found in response, using the first one.
        self.token_wmActivityId = \
            httpUtilities.valueFromBodyURI('wmActivityId') # '204'
        # 16 different values for token_participantId found in response, using the first one.
        self.token_participantId = \
            httpUtilities.valueFromBodyURI('participantId') # '651'
        # 2 different values for token_category found in response, using the first one.
        self.token_category = \
            httpUtilities.valueFromBodyURI('category') # 'OPEN'

        return result

    def page5(self):
        """POST content (requests 501-502)."""
        self.token_dispatch = \
            'onViewResource'
        self.token_resourcecode = \
            'SYSMSG'
        result = request501.POST('/sustain/cms/content' +
            '?dispatch=' +
            self.token_dispatch +
            '&resource.code=' +
            self.token_resourcecode,
            '',
            ( NVPair('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'), ))
        tp.process_result()

        grinder.sleep(20)
        request502.GET('/sustain/cb4176532352/assets/img/ygtvlinklabel-hover-bg.png')
        tp.process_result()

        return result

    def __call__(self):
        """This method is called for every run performed by the worker thread."""
        self.init_data = td.import_data(self.__module__)
        self.page1()      # GET icon-dashboard-delete-tab-hover.png (request 101)

        grinder.sleep(20)
        self.page2()      # GET dashboard (request 201)

        grinder.sleep(20)
        self.page3()      # GET leftmenu-DEFAULT-t1302607106557_en_US.javascript (requests 301-302)

        grinder.sleep(20)
        self.page4()      # POST summary (request 401)
        self.page5()      # POST content (requests 501-502)


def instrumentMethod(test, method_name, c=TestRunner):
    """Instrument a method with the given Test."""
    unadorned = getattr(c, method_name)
    import new
    method = new.instancemethod(test.wrap(unadorned), None, c)
    setattr(c, method_name, method)

# Replace each method with an instrumented version.
# You can call the unadorned method using self.page1.__target__().
instrumentMethod(Test(100, 'Page 1'), 'page1')
instrumentMethod(Test(200, 'Page 2'), 'page2')
instrumentMethod(Test(300, 'Page 3'), 'page3')
instrumentMethod(Test(400, 'Page 4'), 'page4')
instrumentMethod(Test(500, 'Page 5'), 'page5')
