/*
 * Copyright (C) 2011 Google Inc.
 *
 * 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 com.google.mockwebserver;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import junit.framework.TestCase;

public final class MockWebServerTest extends TestCase {
    public void testRegularResponse() throws Exception {
        // 1. Enqueue a response.
        MockWebServer server = new MockWebServer();
        server.enqueue(new MockResponse().setBody("hello world"));
        server.play();

        // 2. Make a request that retrieves that response.
        URL url = server.getUrl("/");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Accept-Language", "en-US");
        InputStream in = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
        assertEquals("hello world", reader.readLine());

        // 3. Validate the request.
        RecordedRequest request = server.takeRequest();
        assertEquals("GET / HTTP/1.1", request.getRequestLine());
        assertTrue(request.getHeaders().contains("Accept-Language: en-US"));
    }
}

@@ -778,16 +778,47 @@
 et.URL;%0A
+import java.net.URLConnection;%0A
 import j
@@ -903,43 +903,100 @@
 e %7B%0A
+%0A
     p
-ublic void testRegularResponse
+rivate MockWebServer server = new MockWebServer();%0A%0A    @Override protected void setUp
 () t
@@ -1024,86 +1024,206 @@
     
+ 
- // 1. Enqueue a response.%0A        MockWebServer server = new MockWebServer();
+super.setUp();%0A    %7D%0A%0A    @Override protected void tearDown() throws Exception %7B%0A        server.shutdown();%0A        super.tearDown();%0A    %7D%0A%0A    public void testRegularResponse() throws Exception %7B
 %0A   
@@ -1314,67 +1314,8 @@
 );%0A%0A
-        // 2. Make a request that retrieves that response.%0A
     
@@ -1769,44 +1769,8 @@
 );%0A%0A
-        // 3. Validate the request.%0A
     
@@ -1969,11 +1969,910 @@
 );%0A    %7D
+%0A%0A    public void testRedirect() throws Exception %7B%0A        server.enqueue(new MockResponse()%0A                .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)%0A                .addHeader(%22Location: /new-path%22)%0A                .setBody(%22This page has moved!%22));%0A        server.enqueue(new MockResponse().setBody(%22This is the new location!%22));%0A        server.play();%0A%0A        URLConnection connection = server.getUrl(%22/%22).openConnection();%0A        InputStream in = connection.getInputStream();%0A        BufferedReader reader = new BufferedReader(new InputStreamReader(in));%0A        assertEquals(%22This is the new location!%22, reader.readLine());%0A%0A        RecordedRequest first = server.takeRequest();%0A        assertEquals(%22GET / HTTP/1.1%22, first.getRequestLine());%0A        RecordedRequest redirect = server.takeRequest();%0A        assertEquals(%22GET /new-path HTTP/1.1%22, redirect.getRequestLine());%0A    %7D
 %0A%7D%0A

Test for redirect