// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.environment.webserver;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.net.MediaType;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GeneratedJsTestServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    String symbol = Strings.nullToEmpty(req.getPathInfo()).replace("../", "").replace("/", "$");
    byte[] data =
        ("<!DOCTYPE html>\n"
         + "<html>\n"
         + "<head>\n"
         + "<meta http-equiv=\"X-UA-Compatible\" content=\"IE-Edge\">\n"
         + "<!-- File generated by " + getClass().getName() + " -->\n"
         + "<title>" + req.getPathInfo() + "</title>\n"
         + "<script src=\"/third_party/closure/goog/base.js\"></script>\n"
         + "<script src=\"/javascript/deps.js\"></script>\n"
         + "<script>\n"
         + "  goog.addDependency('../../.." + req.getPathInfo() + "',\n"
         + "      ['" + symbol + "'],\n"
         + "      goog.dependencies_.requires['../../.." + req.getPathInfo() + "'] || []);\n"
         + "  goog.require('" + symbol + "');\n"
         + "</script></head><body></body></html>").getBytes(Charsets.UTF_8);

    resp.setStatus(HttpServletResponse.SC_OK);
    resp.setContentType(MediaType.HTML_UTF_8.toString());
    resp.setContentLength(data.length);

    OutputStream stream = resp.getOutputStream();
    stream.write(data);
    stream.flush();
    stream.close();
  }
}

@@ -1954,27 +1954,52 @@
  %22  
-goog.addDependency(
+(function() %7B%5Cn%22%0A         + %22    var path = 
 '../
@@ -2029,17 +2029,17 @@
 o() + %22'
-,
+;
 %5Cn%22%0A    
@@ -2050,17 +2050,40 @@
  + %22    
- 
+goog.addDependency(path,
  %5B'%22 + s
@@ -2106,24 +2106,26 @@
          + %22
+  
       goog.d
@@ -2188,16 +2188,80 @@
 '%5D %7C%7C %5B%5D
+,%5Cn%22%0A         + %22        !!goog.dependencies_.pathIsModule%5Bpath%5D
 );%5Cn%22%0A  
@@ -2270,16 +2270,18 @@
      + %22
+  
   goog.r
@@ -2305,24 +2305,46 @@
 l + %22');%5Cn%22%0A
+         + %22  %7D)()%5Cn%22%0A
          + %22

Update GeneratedJsTestServlet to work with test files that use Closure's module syntax.