Devtools: fix MIME type inferrence.

This broke when we moved to use temporary files for gzipped content,
hence losing the file extension. This patch fixes the mime type
inferrence to use the original path and adds a test.

R=qsr@chromium.org

Review URL: https://codereview.chromium.org/1278153002 .

Cr-Mirrored-From: https://github.com/domokit/mojo
Cr-Mirrored-Commit: 6596c152c7d40ebfa021dd9726ad8873b83c0d84
diff --git a/devtoolslib/http_server.py b/devtoolslib/http_server.py
index 482e2db..16c76a9 100644
--- a/devtoolslib/http_server.py
+++ b/devtoolslib/http_server.py
@@ -74,6 +74,7 @@
     def __init__(self, *args, **kwargs):
       self.etag = None
       self.gzipped_file = None
+      self.original_file_name = None
       SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(self, *args, **kwargs)
 
     def get_etag(self):
@@ -153,6 +154,7 @@
               if gzipped:
                 if not self.gzipped_file:
                   self.gzipped_file = tempfile.NamedTemporaryFile(delete=False)
+                  self.original_file_name = candidate
                   with open(candidate, 'rb') as source:
                     with gzip.GzipFile(fileobj=self.gzipped_file) as target:
                       shutil.copyfileobj(source, target)
@@ -170,9 +172,12 @@
       # thanks to content-type mappings.
       # TODO(ppi): drop this part once we can rely on the Sky files declaring
       # correct shebang.
-      if path.endswith('.dart') or path.endswith('.dart.gz'):
+      actual_path = self.original_file_name or path
+
+      if actual_path.endswith('.dart'):
         return 'application/dart'
-      return SimpleHTTPServer.SimpleHTTPRequestHandler.guess_type(self, path)
+      return SimpleHTTPServer.SimpleHTTPRequestHandler.guess_type(self,
+                                                                  actual_path)
 
     def log_message(self, *_):
       """Override the base class method to disable logging."""
diff --git a/devtoolslib/http_server_unittest.py b/devtoolslib/http_server_unittest.py
index f279712..b089e70 100644
--- a/devtoolslib/http_server_unittest.py
+++ b/devtoolslib/http_server_unittest.py
@@ -33,6 +33,8 @@
             hello_file
         other_dir
           other_file
+        apps
+          app.dart
     """
     self.parent_dir = tempfile.mkdtemp()
     self.parent_file = tempfile.NamedTemporaryFile(delete=False,
@@ -47,6 +49,11 @@
     self.other_dir = tempfile.mkdtemp(dir=self.parent_dir)
     self.other_file = tempfile.NamedTemporaryFile(delete=False,
                                                   dir=self.other_dir)
+    self.other_file.close()
+
+    self.apps_dir = tempfile.mkdtemp(dir=self.parent_dir)
+    self.dart_app_path = os.path.join(self.apps_dir, 'app.dart')
+    open(self.dart_app_path, 'w').close()
 
   def tearDown(self):
     shutil.rmtree(self.parent_dir)
@@ -135,6 +142,23 @@
         fileobj=StringIO.StringIO(hello_response.read())).read()
     self.assertEquals('hello', content)
 
+  def test_dart_mime_type(self):
+    """Verifies that files of '.dart' extension are served with MIME type
+    'application/dart'.
+    """
+    mappings = [
+        ('', [self.apps_dir]),
+    ]
+    server_address = ('http://%s:%u/' %
+                      http_server.start_http_server(mappings))
+
+    app_relpath = os.path.relpath(self.dart_app_path, self.apps_dir)
+    hello_response = urllib2.urlopen(server_address + app_relpath)
+    self.assertEquals(200, hello_response.getcode())
+    self.assertTrue('Content-Type' in hello_response.info())
+    self.assertEquals('application/dart',
+                      hello_response.info().get('Content-Type'))
+
 
 if __name__ == "__main__":
   unittest.main()