コード例 #1
0
ファイル: tests.py プロジェクト: pinelounge/42cc
    def test_request_view(self):
        # Fill DB with random requests
        for req in range(10):
            request = HttpRequest()
            request.method = random.sample([u'POST', u'GET'], 1)[0]
            request.path = random.sample([u'/requests/',
                                          u'/edit/',
                                          u'/admin/'], 1)[0]
            request.user = AnonymousUser()
            request.POST = QueryDict(u'k_post=v_post')
            request.COOKIES = QueryDict(u'k_coo=v_coo')
            request.META['HTTP_ACCEPT_LANGUAGE'] = u'en'
            request.META['HTTP_HOST'] = get_random_ip()
            request.META['HTTP_REFERER'] = u''
            request.META['HTTP_USER_AGENT'] = u''
            request.META['QUERY_STRING'] = u'&t=1'
            request.META['REMOTE_ADDR'] = get_random_ip()
            request.META['REMOTE_USER'] = u''
            request.META['REQUEST_METHOD'] = request.method
            request.META['SERVER_NAME'] = u'localhost'
            request.META['SERVER_PORT'] = u'80'

            response = HttpResponse()
            response.status_code = random.sample(
                [200, 302, 404, 500], 1)[0]
            middleware = RequestMiddleware()
            resp = middleware.process_response(request, response)
            self.assertEquals(response, resp)
        self.check_request_view()
        self.add_priorities()
        self.check_request_view()
コード例 #2
0
ファイル: tests.py プロジェクト: pinelounge/42cc
    def test_request_middleware(self):

        # Middleware related test
        request = HttpRequest()
        request.method = random.sample([u'POST', u'GET'], 1)[0]
        request.path = random.sample([u'/requests/',
                                      u'/edit/',
                                      u'/admin/'], 1)[0]
        request.user = AnonymousUser()
        request.POST = QueryDict(u'k_post=v_post')
        request.COOKIES = QueryDict(u'k_coo=v_coo')
        request.META['HTTP_ACCEPT_LANGUAGE'] = u'en'
        request.META['HTTP_HOST'] = get_random_ip()
        request.META['HTTP_REFERER'] = u''
        request.META['HTTP_USER_AGENT'] = u''
        request.META['QUERY_STRING'] = u'&t=1'
        request.META['REMOTE_ADDR'] = get_random_ip()
        request.META['REMOTE_USER'] = u''
        request.META['REQUEST_METHOD'] = request.method
        request.META['SERVER_NAME'] = u'localhost'
        request.META['SERVER_PORT'] = u'80'

        response = HttpResponse()
        response.status_code = random.sample(
            [200, 302, 404, 500], 1)[0]
        middleware = RequestMiddleware()
        resp = middleware.process_response(request, response)
        self.assertEquals(response, resp)

        r = Request.objects.all().order_by('-time')[:1][0]
        self.assertEquals(r.status_code, response.status_code)
        self.assertEquals(r.host, request.get_host())
        self.assertEquals(r.path, request.path)
        self.assertEquals(r.user, None)
        self.assertEquals(r.method, request.method)
        self.assertEquals(r.user_agent,
                          request.META.get('HTTP_USER_AGENT', ''))
        self.assertEquals(r.language,
                          request.META.get('HTTP_ACCEPT_LANGUAGE', ''))
        self.assertEquals(r.timezone, request.META.get('TZ', ''))
        self.assertEquals(r.remote_address,
                          request.META.get('REMOTE_ADDR', ''))
        self.assertEquals(r.referer,
                          request.META.get('HTTP_REFERER', ''))
        self.assertEquals(r.is_secure, request.is_secure())
        self.assertEquals(r.is_ajax, request.is_ajax())
        self.assertEquals(r.GET, json.dumps(request.GET))
        self.assertEquals(r.POST, json.dumps(request.POST))
        self.assertEquals(r.cookies, json.dumps(request.COOKIES))