1
2
3
4
5
6
7
8
9
10
11
12
13
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
30
31
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
40
41
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
119
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 }