1926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# Copyright (C) 2012 Google Inc. All rights reserved.
2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#
3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# Redistribution and use in source and binary forms, with or without
4926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# modification, are permitted provided that the following conditions are
5926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# met:
6926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#
7926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#    * Redistributions of source code must retain the above copyright
8926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# notice, this list of conditions and the following disclaimer.
9926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#    * Redistributions in binary form must reproduce the above
10926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# copyright notice, this list of conditions and the following disclaimer
11926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# in the documentation and/or other materials provided with the
12926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# distribution.
13926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#    * Neither the name of Google Inc. nor the names of its
14926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# contributors may be used to endorse or promote products derived from
15926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# this software without specific prior written permission.
16926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#
17926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
29e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)import unittest
30926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
31926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)from webkitpy.tool.servers.reflectionhandler import ReflectionHandler
32926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
33926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
34926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class TestReflectionHandler(ReflectionHandler):
35926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    STATIC_FILE_DIRECTORY = "/"
36926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
37926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def __init__(self):
38926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.static_files_served = set()
39926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.errors_sent = set()
40926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.functions_run = set()
41926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
42926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def _serve_static_file(self, name):
43926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.static_files_served.add(name)
44926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
45926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def send_error(self, code, description):
46926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.errors_sent.add(code)
47926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
48926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def function_one(self):
49926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.functions_run.add("function_one")
50926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
51926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def some_html(self):
52926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.functions_run.add("some_html")
53926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
54926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
5553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class WriteConvertingLogger(object):
5653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    def __init__(self):
5753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        self.data = ''
5853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
5953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    def write(self, data):
6053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        # If data is still in ASCII, this will throw an exception.
6153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        self.data = str(data)
6253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class TestReflectionHandlerServeXML(ReflectionHandler):
6553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    def __init__(self):
6653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        self.requestline = False
6753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        self.client_address = '127.0.0.1'
6853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        self.request_version = '1'
6953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        self.wfile = WriteConvertingLogger()
7053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
7153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    def serve_xml(self, data):
7253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        self._serve_xml(data)
7353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
745267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    def log_message(self, _format, *_args):
755267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        pass
765267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
7753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
78926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class ReflectionHandlerTest(unittest.TestCase):
79926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def assert_handler_response(self, requests, expected_static_files, expected_errors, expected_functions):
80926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        handler = TestReflectionHandler()
81926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        for request in requests:
82926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            handler.path = request
83926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            handler._handle_request()
84926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.assertEqual(handler.static_files_served, expected_static_files)
85926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.assertEqual(handler.errors_sent, expected_errors)
86926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.assertEqual(handler.functions_run, expected_functions)
87926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
88926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    def test_static_content_or_function_switch(self):
89926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.assert_handler_response(["/test.js"], set(["test.js"]), set(), set())
90926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.assert_handler_response(["/test.js", "/test.css", "/test.html"], set(["test.js", "test.html", "test.css"]), set(), set())
91926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.assert_handler_response(["/test.js", "/test.exe", "/testhtml"], set(["test.js"]), set([404]), set())
92926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.assert_handler_response(["/test.html", "/function.one"], set(["test.html"]), set(), set(['function_one']))
93926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        self.assert_handler_response(["/some.html"], set(["some.html"]), set(), set())
9453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
9553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    def test_svn_log_non_ascii(self):
9653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        xmlChangelog = u'<?xml version="1.0"?>\n<log>\n<logentry revision="1">\n<msg>Patch from John Do\xe9.</msg>\n</logentry>\n</log>'
9753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        handler = TestReflectionHandlerServeXML()
9853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        handler.serve_xml(xmlChangelog)
9953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        self.assertEqual(handler.wfile.data, xmlChangelog.encode('utf-8'))
100