1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import SimpleHTTPServer
7 from telemetry
import decorators
8 from telemetry
.core
import local_server
9 from telemetry
.testing
import tab_test_case
12 class SimpleLocalServerBackendRequestHandler(
13 SimpleHTTPServer
.SimpleHTTPRequestHandler
):
16 msg
= """<!DOCTYPE html>
23 self
.send_response(200)
24 self
.send_header('Content-Type', 'text/html')
25 self
.send_header('Content-Length', len(msg
))
29 def log_request(self
, code
='-', size
='-'):
33 class SimpleLocalServerBackend(BaseHTTPServer
.HTTPServer
,
34 local_server
.LocalServerBackend
):
37 BaseHTTPServer
.HTTPServer
.__init
__(self
, ('127.0.0.1', 0),
38 SimpleLocalServerBackendRequestHandler
)
39 local_server
.LocalServerBackend
.__init
__(self
)
41 def StartAndGetNamedPorts(self
, args
):
42 assert 'hello' in args
43 assert args
['hello'] == 'world'
44 return [local_server
.NamedPort('http', self
.server_address
[1])]
46 def ServeForever(self
):
50 class SimpleLocalServer(local_server
.LocalServer
):
53 super(SimpleLocalServer
, self
).__init
__(SimpleLocalServerBackend
)
55 def GetBackendStartupArgs(self
):
56 return {'hello': 'world'}
60 return self
.forwarder
.url
+ '/'
63 class LocalServerUnittest(tab_test_case
.TabTestCase
):
67 super(LocalServerUnittest
, cls
).setUpClass()
68 cls
._server
= SimpleLocalServer()
69 cls
._platform
.StartLocalServer(cls
._server
)
71 @decorators.Disabled('all') # https://crbug.com/570955
72 def testLocalServer(self
):
73 self
.assertTrue(self
._server
in self
._platform
.local_servers
)
74 self
._tab
.Navigate(self
._server
.url
)
75 self
._tab
.WaitForDocumentReadyStateToBeComplete()
76 body_text
= self
._tab
.EvaluateJavaScript('document.body.textContent')
77 body_text
= body_text
.strip()
78 self
.assertEquals('hello world', body_text
)
80 @decorators.Disabled('all') # https://crbug.com/570955
81 def testStartingAndRestarting(self
):
82 server2
= SimpleLocalServer()
83 self
.assertRaises(Exception,
84 lambda: self
._platform
.StartLocalServer(server2
))
87 self
.assertTrue(self
._server
not in self
._platform
.local_servers
)
89 self
._platform
.StartLocalServer(server2
)