View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.http;
6   
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.util.Collection;
10  import java.util.Map.Entry;
11  
12  /**
13   * Utility wire used for injecting a mock object into a {@link Request}
14   * implementation.
15   * <p>
16   * NOTE: This is not threadsafe and access to it should be synchronized.
17   *
18   * @since 1.17.1
19   * @checkstyle ParameterNumberCheck (50 lines)
20   */
21  public class MockWire implements Wire {
22  
23      /**
24       * The actual mock object we delegate the {@code Wire.send} call to.
25       */
26      private static Wire mockDelegate;
27  
28      /**
29       * Creates a new mock wire instance.
30       * <p>
31       * The given target wire is ignored and {@code Wire.send} is delegated
32       * to the static mock delegate.
33       *
34       * @param wire The original wire which is ignored
35       */
36      @SuppressWarnings("PMD.UnusedFormalParameter")
37      public MockWire(final Wire wire) {
38          // Instantiated by a Request implementation, wire is ignored
39      }
40  
41      @Override
42      public final Response send(final Request req, final String home,
43          final String method, final Collection<Entry<String, String>> headers,
44          final InputStream content, final int connect, final int read)
45          throws IOException {
46          return mockDelegate.send(
47              req,
48              home,
49              method,
50              headers,
51              content,
52              connect,
53              read
54          );
55      }
56  
57      /**
58       * Sets the mock the {@code Request.send} method is delegated to.
59       *
60       * @param mock The mock to assert variables passed by the request
61       *  implementation
62       */
63      @SuppressWarnings("PMD.DefaultPackage")
64      static void setMockDelegate(final Wire mock) {
65          MockWire.mockDelegate = mock;
66      }
67  
68  }