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.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link ImmediateRefreshHandler}.
23   *
24   * @author Marc Guillemot
25   * @author Ronald Brill
26   */
27  public final class ImmediateRefreshHandlerTest extends SimpleWebTestCase {
28  
29      /**
30       * Regression test for bug 1211980: redirect on the same page after a post.
31       * @throws Exception if the test fails
32       */
33      @Test
34      public void refreshSamePageAfterPost() throws Exception {
35          final WebClient client = getWebClient();
36          client.setRefreshHandler(new ImmediateRefreshHandler());
37  
38          // connection will return a page with <meta ... refresh> for the first call
39          // and the same page without it for the other calls
40          final MockWebConnection webConnection = new MockWebConnection() {
41              private int nbCalls_ = 0;
42              @Override
43              public WebResponse getResponse(final WebRequest request) throws IOException {
44                  String content = DOCTYPE_HTML + "<html><head>\n";
45                  if (nbCalls_ == 0) {
46                      content += "<meta http-equiv='refresh' content='0;url="
47                          + URL_FIRST.toExternalForm()
48                          + "'>\n";
49                  }
50                  content += "</head><body></body></html>";
51                  nbCalls_++;
52                  final StringWebResponse response = new StringWebResponse(content, request.getUrl());
53                  response.getWebRequest().setHttpMethod(request.getHttpMethod());
54                  return response;
55              }
56          };
57          client.setWebConnection(webConnection);
58  
59          final WebRequest request = new WebRequest(URL_FIRST);
60          request.setHttpMethod(HttpMethod.POST);
61          client.getPage(request);
62      }
63  }