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.util;
16  
17  import java.util.Collections;
18  
19  import org.htmlunit.HttpMethod;
20  import org.htmlunit.SimpleWebTestCase;
21  import org.htmlunit.WebConnection;
22  import org.htmlunit.WebRequest;
23  import org.htmlunit.WebResponse;
24  import org.htmlunit.WebResponseData;
25  import org.htmlunit.http.HttpStatus;
26  import org.junit.Test;
27  
28  /**
29   * Tests for {@link WebConnectionWrapper}.
30   *
31   * @author Marc Guillemot
32   */
33  public class WebConnectionWrapperTest extends SimpleWebTestCase {
34  
35      /**
36       * @throws Exception if the test fails
37       */
38      @Test
39      public void wrapper() throws Exception {
40          final WebResponseData data = new WebResponseData(new byte[]{},
41                  HttpStatus.OK_200, HttpStatus.OK_200_MSG, Collections.emptyList());
42          final WebResponse response = new WebResponse(data, URL_FIRST, HttpMethod.GET, 0);
43          final WebRequest wrs = new WebRequest(URL_FIRST);
44  
45          final WebConnection realConnection = new WebConnection() {
46              @Override
47              public WebResponse getResponse(final WebRequest request) {
48                  assertSame(wrs, request);
49                  return response;
50              }
51              @Override
52              public void close() {
53                  // nothing
54              }
55          };
56  
57          try (WebConnectionWrapper wrapper = new WebConnectionWrapper(realConnection)) {
58              assertSame(response, wrapper.getResponse(wrs));
59          }
60      }
61  
62  }