Esempio n. 1
0
  // This is going to be a whole heap of fun.
  public void testShouldAllowAPacToBeBasedOffAnExistingPacFile() throws IOException {
    // We should allow people to override the settings in the given pac
    // The strategy will be to rename the method we care about to something else
    // And then include the original (JS) source code. How badly can this fail?

    File example = File.createTempFile("example", "pac");
    example.deleteOnExit();
    FileWriter out = new FileWriter(example);
    out.append(EXAMPLE_PAC);
    out.close();

    DoNotUseProxyPac pac = new DoNotUseProxyPac();
    pac.map("/foobar/*").toNoProxy();
    pac.defaults().toProxy("http://example.com:8080/se-server");
    pac.deriveFrom(example.toURI());
    String converted = captureOutput(pac);

    assertEquals(
        converted,
        "function originalFindProxyForURL(u, h) {  if (u.contains('fishy') return 'DIRECT'; }\n"
            + "function isFishy() { return false; }\n"
            + "function FindProxyForURL(url, host) {\n"
            + "  if (shExpMatch(url, '/foobar/*')) { return 'DIRECT'; }\n"
            + "\n"
            + "  var value = originalFindProxyForURL(host, url);\n"
            + "  if (value) { return value; }\n"
            + "\n"
            + "  return 'PROXY http://example.com:8080/se-server';\n"
            + "}\n",
        converted);
  }
Esempio n. 2
0
  public void testShouldNotReturnAnythingIfNothingIsConfigured() throws IOException {
    DoNotUseProxyPac pac = new DoNotUseProxyPac();

    StringWriter writer = new StringWriter();
    pac.outputTo(writer);

    assertEquals(writer.toString(), EMPTY_PAC, writer.toString());
  }
Esempio n. 3
0
  public void testShouldAllowSpecificUrlsToBePassedThroughDirectly() throws IOException {
    DoNotUseProxyPac pac = new DoNotUseProxyPac();

    pac.map("http://seleniumhq.org/cheese").toNoProxy();
    String config = captureOutput(pac);

    assertTrue(
        config,
        config.contains(
            "if (shExpMatch(url, 'http://seleniumhq.org/cheese')) {" + " return 'DIRECT'; " + "}"));
  }
Esempio n. 4
0
  // See: http://support.microsoft.com/kb/274204
  public void testShouldUseJsRegexIfIEWouldNotHandleTheMappingUrl() throws IOException {
    DoNotUseProxyPac pac = new DoNotUseProxyPac();

    pac.map("/[a-zA-Z]{4}.microsoft.com/").toProxy("http://localhost:8080/selenium-server/");
    String config = captureOutput(pac);

    assertTrue(
        config,
        config.contains(
            "if (/[a-zA-Z]{4}.microsoft.com/.test(url)) {"
                + " return 'PROXY http://localhost:8080/selenium-server/'; "
                + "}"));
  }
Esempio n. 5
0
  public void testShouldAllowBasicWildCarding() throws IOException {
    DoNotUseProxyPac pac = new DoNotUseProxyPac();

    pac.map("*/selenium-server/*").toProxy("http://localhost:8080/selenium-server/");
    String config = captureOutput(pac);

    assertTrue(
        config,
        config.contains(
            "if (shExpMatch(url, '*/selenium-server/*')) {"
                + " return 'PROXY http://localhost:8080/selenium-server/'; "
                + "}"));
  }
Esempio n. 6
0
  public void testShouldAllowSpecificUrlsToBeProxied() throws IOException {
    DoNotUseProxyPac pac = new DoNotUseProxyPac();

    pac.mapHost("www.google.com").toProxy("http://localhost:8080");
    String config = captureOutput(pac);

    assertTrue(
        config,
        config.contains(
            "if (shExpMatch(host, 'www.google.com')) {"
                + " return 'PROXY http://localhost:8080'; "
                + "}"));
  }
Esempio n. 7
0
  public void testFinalLineOfFunctionShouldRedirectToDefaultProxy() throws IOException {
    DoNotUseProxyPac pac = new DoNotUseProxyPac();

    String config = captureOutput(pac);
    assertTrue(config, config.endsWith("{\n}\n"));

    pac.defaults().toProxy("http://localhost:1010");
    config = captureOutput(pac);
    assertTrue(config, config.endsWith("return 'PROXY http://localhost:1010';\n}\n"));

    pac.defaults().toNoProxy();
    config = captureOutput(pac);
    assertTrue(config, config.endsWith("return 'DIRECT';\n}\n"));
  }
  public void testShouldConvertAProxyPacProperly() throws JSONException {
    DoNotUseProxyPac pac = new DoNotUseProxyPac();
    pac.map("*/selenium/*").toProxy("http://localhost:8080/selenium-server");
    pac.map("/[a-zA-Z]{4}.microsoft.com/").toProxy("http://localhost:1010/selenium-server/");
    pac.map("/flibble*").toNoProxy();
    pac.mapHost("www.google.com").toProxy("http://fishy.com/");
    pac.mapHost("seleniumhq.org").toNoProxy();
    pac.defaults().toNoProxy();

    String json = new BeanToJsonConverter().convert(pac);
    JSONObject converted = new JSONObject(json);

    assertEquals(
        "http://localhost:8080/selenium-server",
        converted.getJSONObject("proxiedUrls").get("*/selenium/*"));
    assertEquals(
        "http://localhost:1010/selenium-server/",
        converted.getJSONObject("proxiedRegexUrls").get("/[a-zA-Z]{4}.microsoft.com/"));
    assertEquals("/flibble*", converted.getJSONArray("directUrls").get(0));
    assertEquals("seleniumhq.org", converted.getJSONArray("directHosts").get(0));
    assertEquals(
        "http://fishy.com/", converted.getJSONObject("proxiedHosts").get("www.google.com"));
    assertEquals("'DIRECT'", converted.get("defaultProxy"));
  }
Esempio n. 9
0
 private String captureOutput(DoNotUseProxyPac pac) throws IOException {
   StringWriter writer = new StringWriter();
   pac.outputTo(writer);
   return writer.toString();
 }