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.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.junit.BrowserRunner;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  
24  /**
25   * Tests for {@link IncorrectnessListener}.
26   *
27   * @author Marc Guillemot
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public final class IncorrectnessListenerTest extends SimpleWebTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      public void notification() throws Exception {
38          final String html = DOCTYPE_HTML
39                  + "<html><head>\n"
40                  + "  <script src='script.js'></script>\n"
41                  + "</head>\n"
42                  + "<body>\n"
43                  + "</body>\n"
44                  + "</html>";
45  
46          final WebClient webClient = getWebClient();
47  
48          final List<String> collectedIncorrectness = new ArrayList<>();
49          final IncorrectnessListener listener = new IncorrectnessListener() {
50              @Override
51              public void notify(final String message, final Object origin) {
52                  collectedIncorrectness.add(message);
53              }
54          };
55          webClient.setIncorrectnessListener(listener);
56  
57          final MockWebConnection webConnection = new MockWebConnection();
58          webClient.setWebConnection(webConnection);
59          webConnection.setResponse(URL_FIRST, html);
60          webConnection.setDefaultResponse("alert('Hello');", "application/x-javascript");
61          webClient.getPage(URL_FIRST);
62  
63          final String[] expectedIncorrectness = {
64              "Obsolete content type encountered: 'application/x-javascript' for "
65                      + "remotely loaded JavaScript element at 'http://localhost:22222/script.js'."
66          };
67          assertEquals(expectedIncorrectness, collectedIncorrectness);
68      }
69  }