View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import java.io.IOException;
18  
19  import org.htmlunit.junit.BrowserRunner;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for {@link ImmediateRefreshHandler}.
25   *
26   * @author Marc Guillemot
27   */
28  @RunWith(BrowserRunner.class)
29  public final class ImmediateRefreshHandlerTest extends SimpleWebTestCase {
30  
31      /**
32       * Regression test for bug 1211980: redirect on the same page after a post.
33       * @throws Exception if the test fails
34       */
35      @Test
36      public void refreshSamePageAfterPost() throws Exception {
37          final WebClient client = getWebClient();
38          client.setRefreshHandler(new ImmediateRefreshHandler());
39  
40          // connection will return a page with <meta ... refresh> for the first call
41          // and the same page without it for the other calls
42          final MockWebConnection webConnection = new MockWebConnection() {
43              private int nbCalls_ = 0;
44              @Override
45              public WebResponse getResponse(final WebRequest request) throws IOException {
46                  String content = DOCTYPE_HTML + "<html><head>\n";
47                  if (nbCalls_ == 0) {
48                      content += "<meta http-equiv='refresh' content='0;url="
49                          + URL_FIRST.toExternalForm()
50                          + "'>\n";
51                  }
52                  content += "</head><body></body></html>";
53                  nbCalls_++;
54                  final StringWebResponse response = new StringWebResponse(content, request.getUrl());
55                  response.getWebRequest().setHttpMethod(request.getHttpMethod());
56                  return response;
57              }
58          };
59          client.setWebConnection(webConnection);
60  
61          final WebRequest request = new WebRequest(URL_FIRST);
62          request.setHttpMethod(HttpMethod.POST);
63          client.getPage(request);
64      }
65  }