Пример #1
0
 def test_request_querystring(self):
     s = HttpSession(http_method='GET', http_url='http://localhost:9000/endpoint?q=foo+bar&q=bar&z=qux')
     querystring_params = s.querystring_params()
     
     self.failUnless(('q', 'foo bar') in querystring_params)
     self.failUnless(('q', 'bar') in querystring_params)
     self.failUnless(('z', 'qux') in querystring_params)
Пример #2
0
 def test_raw_request_with_alternate_url(self):
 
     s = HttpSession(http_method='GET', http_url='http://google.com/')
     raw_request = s.get_raw_request('http://www.google.com/')
     expected_request = 'GET / HTTP/1.1\r\n' + \
         'Host: www.google.com\r\nAccept: */*\r\n\r\n'
     
     self.failUnlessEqual(raw_request, expected_request)
Пример #3
0
 def test_needs_to_send_request(self):
 
     s = HttpSession(http_method='GET', http_url='http://api.gowalla.com/')
     
     self.failUnless(s.needs_to_send_request())
     
     s.time_completed = datetime.datetime.now()
     
     self.failIf(s.needs_to_send_request())
Пример #4
0
 def test_multiple_header_values(self):
     headers = '\r\n'.join([
         'Cache-Control: a',
         'Cache-Control: b',
     ])
     s = HttpSession(http_method='GET', http_url='http://localhost:9000/endpoint', http_headers=headers)
     
     request = s.get_raw_request()
     
     self.failUnless('Cache-Control: a' in request)
     self.failUnless('Cache-Control: b' in request)
Пример #5
0
 def test_raw_request_with_extra_headers(self):
     headers = '\r\n'.join([
         'User-Agent: Spyglass/0.1',
         'Referer: http://localhost:9000/login.jsp'
     ])
     s = HttpSession(http_method='GET', http_url='http://localhost:9000/endpoint', http_headers=headers)
          
     status, mime_headers, mime_body = self.parse_request(s.get_raw_request())
     
     self.failUnlessEqual(mime_headers['Host'], 'localhost')
     self.failUnlessEqual(mime_headers['User-Agent'], 'Spyglass/0.1')
     self.failUnlessEqual(mime_headers['Referer'], 'http://localhost:9000/login.jsp')
Пример #6
0
 def test_raw_request_with_host_headers(self):
     headers = '\r\n'.join([
         'User-Agent: Spyglass/0.1',
         'Referer: http://localhost:9000/login.jsp',
         'Host: google.com',
     ])
     s = HttpSession(http_method='GET', http_url='http://localhost:9000/endpoint', http_headers=headers)
     
     # this is a bit of a hack, but using the email parsing library
     # is way easier and more accurate than comparing strings
     status, mime_headers, mime_body = self.parse_request(s.get_raw_request())
     
     self.assertTrue('Host' in mime_headers)
     self.assertTrue(mime_headers['Host'] == 'google.com')
Пример #7
0
def create_session(request):
    
    if request.GET.get('advanced', 'no') == 'yes':
        advanced = True
    else:
        advanced = False
    
    if request.method == "POST":
    
        f, header_formset = session_and_headers_form(request.POST)
        
        if f.is_valid():
        
            # did the user accidentally use the hostname 'localhost'?
            real_hostname = socket.gethostbyaddr(request.META['REMOTE_ADDR'])[0]
            real_url, corrected = replace_localhost_hostname(f.cleaned_data['url'], real_hostname)
        
            header_text = ''
            if header_formset.is_valid():
                header_text = '\r\n'.join([x.formatted_header() for x in header_formset.forms if x.formatted_header()])
                
            s = HttpSession(http_method=f.cleaned_data['method'],
                http_url=real_url,
                http_headers=header_text,
                follow_redirects=f.cleaned_data['follow_redirects'],
                http_body=f.cleaned_data['body'],
                autocorrected_localhost=corrected)
            s.save()
            if s.pk:
                # save was successful
                request.session.setdefault('spyglass_session_ids', set()).add(s.pk)
                request.session.modified = True
                
                return redirect(s.get_absolute_url())
            else:
                # something went wrong...
                print "[ERROR] Unable to save HttpSession!"
        else:
            print " [WARN] submitted form was invalid: " + str(f.errors)
    else:
        f, header_formset = session_and_headers_form()
    
    context = {
        'form': f,
        'http_header_form': header_formset,
        'use_advanced_form': advanced,
    }
    
    return render_to_response('spyglass/create_session.html', context, context_instance=RequestContext(request))
Пример #8
0
 def test_raw_request_with_body(self):
 
     body = '''
         <fake-xml>
             <key>com.bleedingwolf.PrincipalClass</key>
             <value>SomeClassName</value>
         </fake-xml>
     '''
     content_length = len(body)
     
     s = HttpSession(http_method='POST', http_url='http://localhost:9000/endpoint', http_body=body)
     
     expected_request = '''POST /endpoint HTTP/1.1\r\nHost: localhost\r\n''' + \
         '''Accept: */*\r\nContent-Length: %d\r\n\r\n%s''' % (content_length, body)
             
     self.failUnlessEqual(s.get_raw_request(), expected_request)
Пример #9
0
 def test_header_dicts_method(self):
     headers = '\r\n'.join([
         'User-Agent: Spyglass/0.1',
         'User-Agent: Foobar/2.4',
         'Referer: http://localhost:9000/login.jsp',
         'Host: google.com',
     ])
     s = HttpSession(http_method='GET', http_url='http://localhost:9000/endpoint', http_headers=headers)
     
     expected_headers = [
         dict(name='User-Agent', value='Spyglass/0.1'),
         dict(name='User-Agent', value='Foobar/2.4'),
         dict(name='Referer', value='http://localhost:9000/login.jsp'),
         dict(name='Host', value='google.com')
     ]
     
     self.failUnlessEqual(expected_headers, s.header_list())
Пример #10
0
 def test_headers_method(self):
     headers = '\r\n'.join([
         'User-Agent: Spyglass/0.1',
         'User-Agent: Foobar/2.4',
         'Referer: http://localhost:9000/login.jsp',
         'Host: google.com',
     ])
     s = HttpSession(http_method='GET', http_url='http://localhost:9000/endpoint', http_headers=headers)
     
     expected_headers = [
         ('User-Agent', 'Spyglass/0.1'),
         ('User-Agent', 'Foobar/2.4'),
         ('Referer', 'http://localhost:9000/login.jsp'),
         ('Host', 'google.com')
     ]
     
     self.failUnlessEqual(expected_headers, s.get_request_headers())
Пример #11
0
 def test_raw_request_with_extra_headers(self):
     headers = '\r\n'.join([
         'User-Agent: Spyglass/0.1',
         'Referer: http://localhost:9000/login.jsp'
     ])
     s = HttpSession(http_method='GET', http_url='http://localhost:9000/endpoint', http_headers=headers)
     
     expected_request = '\r\n'.join([
         'GET /endpoint HTTP/1.1',
         'Host: localhost',
         'Accept: */*',
         'User-Agent: Spyglass/0.1',
         'Referer: http://localhost:9000/login.jsp',
         '',
         '',
     ])
             
     self.failUnlessEqual(s.get_raw_request(), expected_request)
Пример #12
0
 def test_raw_request_with_body(self):
 
     import textwrap
 
     body = textwrap.dedent('''\
         <fake-xml>
             <key>com.bleedingwolf.PrincipalClass</key>
             <value>SomeClassName</value>
         </fake-xml>''')
     content_length = len(body)
     
     s = HttpSession(http_method='POST', http_url='http://localhost:9000/endpoint', http_body=body)
     
     expected_request = '''POST /endpoint HTTP/1.1\r\nHost: localhost\r\n''' + \
         '''Accept: */*\r\nContent-Length: %d\r\n\r\n%s''' % (content_length, body)
     
     status, mime_headers, mime_body = self.parse_request(s.get_raw_request())
            
     self.failUnlessEqual(mime_headers['Content-Length'], str(content_length))
     self.failUnlessEqual(mime_body, body)
Пример #13
0
def create_session(request):
    
    if request.GET.get('advanced', 'no') == 'yes':
        advanced = True
    else:
        advanced = False
    
    if request.method == "POST":
    
        f, header_formset = session_and_headers_form(request.POST)
        
        if f.is_valid():
        
            header_text = ''
            if header_formset.is_valid():
                header_text = '\r\n'.join([x.formatted_header() for x in header_formset.forms if x.formatted_header()])
                
            s = HttpSession(http_method=f.cleaned_data['method'],
                http_url=f.cleaned_data['url'],
                http_headers=header_text,
                follow_redirects=f.cleaned_data['follow_redirects'],
                http_body=f.cleaned_data['body'])
            s.save()
            if s.pk:
                # save was successful
                return redirect(s.get_absolute_url())
            else:
                # something went wrong...
                print "[ERROR] Unable to save HttpSession!"
        else:
            print " [WARN] submitted form was invalid: " + str(f.errors)
    else:
        f, header_formset = session_and_headers_form()
    
    context = {
        'form': f,
        'http_header_form': header_formset,
        'use_advanced_form': advanced,
    }
    
    return render_to_response('spyglass/create_session.html', context, context_instance=RequestContext(request))
Пример #14
0
 def test_raw_request_with_querystring(self):
 
     s = HttpSession(http_method='GET', http_url='http://api.flickr.com/services/rest/?method=flickr.photos.getExif')
     expected_request = 'GET /services/rest/?method=flickr.photos.getExif HTTP/1.1\r\n' + \
         'Host: api.flickr.com\r\nAccept: */*\r\n\r\n'
     self.failUnlessEqual(s.get_raw_request(), expected_request)
Пример #15
0
 def test_raw_request_for_index(self):
     
     s = HttpSession(http_method='GET', http_url='http://www.carfax.com')
     expected_request = 'GET / HTTP/1.1\r\nHost: www.carfax.com\r\nAccept: */*\r\n\r\n'
     self.failUnlessEqual(s.get_raw_request(), expected_request)
Пример #16
0
 def test_raw_request_with_path(self):
     
     s = HttpSession(http_method='GET', http_url='http://api.gowalla.com/spots')
     expected_request = 'GET /spots HTTP/1.1\r\nHost: api.gowalla.com\r\nAccept: */*\r\n\r\n'
     self.failUnlessEqual(s.get_raw_request(), expected_request)
Пример #17
0
 def test_absolute_url(self):
 
     s = HttpSession(id=543)
     self.failUnlessEqual(s.get_absolute_url(), '/sessions/543')