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  
16  package org.htmlunit.util.mocks;
17  
18  import java.io.InputStream;
19  import java.nio.charset.Charset;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.htmlunit.WebRequest;
25  import org.htmlunit.WebResponse;
26  import org.htmlunit.util.NameValuePair;
27  
28  /**
29   * Simple mock for {@link WebResponse}.
30   *
31   * @author Ronald Brill
32   */
33  public class WebResponseMock extends WebResponse {
34      private Map<String, String> headers_;
35  
36      private Map<String, Integer> callCounts_ = new HashMap<>();
37  
38      /**
39       * Ctor.
40       * @param request the request
41       * @param headers the headers
42       */
43      public WebResponseMock(final WebRequest request, final Map<String, String> headers) {
44          super(null, request, 0);
45  
46          if (headers == null) {
47              headers_ = new HashMap<>();
48          }
49          else {
50              headers_ = headers;
51          }
52      }
53  
54      @Override
55      public InputStream getContentAsStream() {
56          throw new RuntimeException("not implemented");
57      }
58  
59      @Override
60      public String getContentAsString() {
61          throw new RuntimeException("not implemented");
62      }
63  
64      @Override
65      public Charset getContentCharset() {
66          throw new RuntimeException("not implemented");
67      }
68  
69      @Override
70      public String getContentType() {
71          throw new RuntimeException("not implemented");
72      }
73  
74      @Override
75      public long getLoadTime() {
76          throw new RuntimeException("not implemented");
77      }
78  
79      @Override
80      public List<NameValuePair> getResponseHeaders() {
81          throw new RuntimeException("not implemented");
82      }
83  
84      @Override
85      public String getResponseHeaderValue(final String headerName) {
86          count("getResponseHeaderValue");
87          for (final Map.Entry<String, String> pair : headers_.entrySet()) {
88              if (pair.getKey().equalsIgnoreCase(headerName)) {
89                  return pair.getValue();
90              }
91          }
92          return null;
93      }
94  
95      @Override
96      public int getStatusCode() {
97          throw new RuntimeException("not implemented");
98      }
99  
100     @Override
101     public String getStatusMessage() {
102         throw new RuntimeException("not implemented");
103     }
104 
105     @Override
106     public WebRequest getWebRequest() {
107         count("getWebRequest");
108         return super.getWebRequest();
109     }
110 
111     @Override
112     public void cleanUp() {
113         count("cleanUp");
114         super.cleanUp();
115     }
116 
117     /**
118      * @param method the method name
119      * @return the number of call for the given method name
120      */
121     public int getCallCount(final String method) {
122         return callCounts_.getOrDefault(method, 0);
123     }
124 
125     private void count(final String method) {
126         callCounts_.put(method, getCallCount(method) + 1);
127     }
128 }