/*
Copyright 2012 Selenium committers
Copyright 2012 Software Freedom Conservancy

Licensed 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.server;

import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;

import junit.framework.TestCase;

import org.openqa.jetty.http.HttpRequest;
import org.openqa.jetty.http.HttpResponse;
import org.openqa.jetty.util.URI;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

public class ProxyHanderUnitTest extends TestCase {

  private final int port = 8086;

  public void testSendNotFoundSends404ResponseCode() throws Exception {
    ProxyHandler proxyHandler = new ProxyHandler(true, "", "", false, false, port, new Object());
    HttpResponse httpResponseMock = createMock(HttpResponse.class);
    httpResponseMock.sendError(HttpResponse.__404_Not_Found, "Not found");
    expectLastCall().once();
    replay(httpResponseMock);
    proxyHandler.sendNotFound(httpResponseMock);
    verify(httpResponseMock);
  }

  public void testUnknownHostExceptionDoesNotBubble() throws Exception {
    ProxyHandler proxyHandler = new ProxyHandler(true, "", "", false, false, port, new Object());
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpResponse response = new HttpResponse() {
      @Override
      public OutputStream getOutputStream() {
        return out;
      }
    };
    HttpRequest request = new HttpRequest();
    request.setMethod("GET");
    request.setURI(new URI("http://does-not-exist.invalidtld/"));
    proxyHandler.handle("foo", "bar", request, response);
  }

  public void testUnknownHostExceptionProvidesUsefulErrorMessage() throws Exception {
    ProxyHandler proxyHandler = new ProxyHandler(true, "", "", false, false, port, new Object());
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpResponse response = new HttpResponse() {
      @Override
      public OutputStream getOutputStream() {
        return out;
      }
    };
    HttpRequest request = new HttpRequest();
    request.setMethod("GET");
    request.setURI(new URI("http://does-not-exist.invalidtld/"));
    proxyHandler.handle("foo", "bar", request, response);

    String responseText = new String(out.toByteArray());
    assertTrue(responseText.contains("Check the address for typing errors"));
  }

  public void testConnectExceptionDoesNotBubble() throws Exception {
    ProxyHandler proxyHandler = new ProxyHandler(true, "", "", false, false, port, new Object());
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpResponse response = new HttpResponse() {
      @Override
      public OutputStream getOutputStream() {
        return out;
      }
    };
    HttpRequest request = new HttpRequest();
    request.setMethod("GET");
    request.setURI(new URI("http://localhost:60999/"));
    proxyHandler.handle("foo", "bar", request, response);
  }

  public void testConnectExceptionProvidesUsefulErrorMessage() throws Exception {
    ProxyHandler proxyHandler = new ProxyHandler(true, "", "", false, false, port, new Object());
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpResponse response = new HttpResponse() {
      @Override
      public OutputStream getOutputStream() {
        return out;
      }
    };
    HttpRequest request = new HttpRequest();
    request.setMethod("GET");
    request.setURI(new URI("http://localhost:60999/"));
    proxyHandler.handle("foo", "bar", request, response);

    String responseText = new String(out.toByteArray());
    assertTrue(responseText.contains("The site could be temporarily unavailable or too busy"));
  }

  public void testHandleCallsSendNotFoundWhenAskingForNonExistentResource()
      throws Exception {
    ProxyHandler proxyHandlerMock = createMock(ProxyHandler.class,
        ProxyHandler.class.getDeclaredMethod(
            "sendNotFound", HttpResponse.class));

    String pathInContext = "/invalid";
    String pathParams = "";
    HttpRequest httpRequest = new HttpRequest();
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setAttribute("NotFound", "True");

    proxyHandlerMock.sendNotFound(httpResponse);
    expectLastCall().once();
    replay(proxyHandlerMock);

    proxyHandlerMock.handle(pathInContext, pathParams, httpRequest,
        httpResponse);
    assertNull(httpResponse.getAttribute("NotFound"));
    verify(proxyHandlerMock);
  }
}

@@ -642,16 +642,227 @@
 erver;%0A%0A
+import org.junit.Test;%0Aimport org.openqa.jetty.http.HttpRequest;%0Aimport org.openqa.jetty.http.HttpResponse;%0Aimport org.openqa.jetty.util.URI;%0A%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.OutputStream;%0A%0A
 import s
@@ -1090,228 +1090,92 @@
 fy;%0A
-%0A
 import 
-junit.framework.TestCase;%0A%0Aimport org.openqa.jetty.http.HttpRequest;%0Aimport org.openqa.jetty.http.HttpResponse;%0Aimport org.openqa.jetty.util.URI;%0A%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.OutputStream
+static org.junit.Assert.assertNull;%0Aimport static org.junit.Assert.assertTrue
 ;%0A%0Ap
@@ -1210,25 +1210,8 @@
 est 
-extends TestCase 
 %7B%0A%0A 
@@ -1243,16 +1243,24 @@
  8086;%0A%0A
+  @Test%0A
   public
@@ -1269,13 +1269,9 @@
 oid 
-testS
+s
 endN
@@ -1695,32 +1695,40 @@
 onseMock);%0A  %7D%0A%0A
+  @Test%0A
   public void te
@@ -1717,37 +1717,33 @@
 t%0A  public void 
-testU
+u
 nknownHostExcept
@@ -2287,32 +2287,40 @@
 response);%0A  %7D%0A%0A
+  @Test%0A
   public void te
@@ -2321,13 +2321,9 @@
 oid 
-testU
+u
 nkno
@@ -3028,32 +3028,40 @@
 errors%22));%0A  %7D%0A%0A
+  @Test%0A
   public void te
@@ -3050,37 +3050,33 @@
 t%0A  public void 
-testC
+c
 onnectExceptionD
@@ -3606,32 +3606,40 @@
 response);%0A  %7D%0A%0A
+  @Test%0A
   public void te
@@ -3640,13 +3640,9 @@
 oid 
-testC
+c
 onne
@@ -4359,16 +4359,24 @@
 );%0A  %7D%0A%0A
+  @Test%0A
   public
@@ -4385,13 +4385,9 @@
 oid 
-testH
+h
 andl

Go, go, Gadget JUnit4. Converting the ProxyHanderUnitTest to use junit4