~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: James Henstridge
  • Date: 2006-02-06 07:32:47 UTC
  • mto: (1185.50.83 bzr-jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1562.
  • Revision ID: james.henstridge@canonical.com-20060206073247-001a39bcae4ae3d6
Make bzrlib.transport.http.HttpServer output referer and user agent as in
common log format.

Update my new test_http tests to use TestCaseWithWebserver, and check log
to make sure user-agent is written.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# (C) 2005 Canonical
2
2
 
3
 
import threading
4
 
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
5
 
import urllib2
6
 
 
7
3
import bzrlib
8
4
from bzrlib.tests import TestCase
 
5
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
9
6
from bzrlib.transport.http import HttpTransport, extract_auth
10
7
 
11
8
class FakeManager (object):
58
55
           'http://bzr.ozlabs.org/.bzr/tree-version')
59
56
 
60
57
 
61
 
class RequestHandler(BaseHTTPRequestHandler):
62
 
    def do_GET(self):
63
 
        self.send_response(200)
64
 
        self.send_header('Content-type', 'text/plain;charset=UTF-8')
65
 
        self.end_headers()
66
 
        self.wfile.write('Path: %s\nUser-agent: %s\n' %
67
 
                         (self.path, self.headers.getheader('user-agent', '')))
68
 
        self.close_connection = True
69
 
 
70
 
 
71
 
class TestHttpConnections(TestCase):
 
58
class TestHttpConnections(TestCaseWithWebserver):
72
59
 
73
60
    def setUp(self):
74
 
        """Set up a dummy HTTP server as a thread.
75
 
 
76
 
        The server will serve a single request and then quit.
77
 
        """
78
61
        super(TestHttpConnections, self).setUp()
79
 
        self.httpd = HTTPServer(('127.0.0.1', 0), RequestHandler)
80
 
        host, port = self.httpd.socket.getsockname()
81
 
        self.baseurl = 'http://127.0.0.1:%d/' % port
82
 
        self.quit_server = False
83
 
        self.thread = threading.Thread(target=self._run_http)
84
 
        self.thread.start()
85
 
 
86
 
    def _run_http(self):
87
 
        while not self.quit_server:
88
 
            self.httpd.handle_request()
89
 
        self.httpd.server_close()
90
 
 
91
 
    def tearDown(self):
92
 
        # tell the server to quit, and issue a request to make sure the
93
 
        # mainloop gets run
94
 
        self.quit_server = True
95
 
        try:
96
 
            response = urllib2.urlopen(self.baseurl)
97
 
            response.read()
98
 
        except IOError:
99
 
            # ignore error, in case server has already quit
100
 
            pass
101
 
        self.thread.join()
102
 
        
103
 
        super(TestHttpConnections, self).tearDown()
 
62
        self.build_tree(['xxx', 'foo/', 'foo/bar'], line_endings='binary')
104
63
 
105
64
    def test_http_has(self):
106
 
        t = HttpTransport(self.baseurl)
 
65
        t = HttpTransport(self.server.get_url())
107
66
        self.assertEqual(t.has('foo/bar'), True)
108
67
 
109
68
    def test_http_get(self):
110
 
        t = HttpTransport(self.baseurl)
 
69
        t = HttpTransport(self.server.get_url())
111
70
        fp = t.get('foo/bar')
112
71
        self.assertEqualDiff(
113
72
            fp.read(),
114
 
            'Path: /foo/bar\nUser-agent: bzr/%s\n' % bzrlib.__version__)
 
73
            'contents of foo/bar\n')
 
74
        self.assertEqual(len(self.server.logs), 1)
 
75
        self.assertTrue(self.server.logs[0].endswith(
 
76
            '"GET /foo/bar HTTP/1.1" 200 - "-" "bzr/%s"' % bzrlib.__version__))