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