Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ResponseHeaderValidator |
|
| 3.1666666666666665;3.167 |
1 | /* | |
2 | * Copyright 1999,2004 The Apache Software Foundation. | |
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 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | package org.apache.commons.latka.validators; | |
18 | ||
19 | import java.util.StringTokenizer; | |
20 | ||
21 | import org.apache.commons.latka.ValidationException; | |
22 | ||
23 | import org.apache.commons.latka.http.Response; | |
24 | ||
25 | /** | |
26 | * ResponseHeaderValidator validates response headers in | |
27 | * an HTTP session. Setting just the header name tests | |
28 | * for the existence of a header. If you set the | |
29 | * headerValue and there is more than one header of that | |
30 | * name, the validator matches on ANY header with that | |
31 | * value. | |
32 | * | |
33 | * @author Morgan Delagrange | |
34 | */ | |
35 | public class ResponseHeaderValidator extends BaseConditionalValidator { | |
36 | ||
37 | // --------------------------------------------------------------- Attributes | |
38 | ||
39 | 0 | protected String _headerName = null; |
40 | 0 | protected String _headerValue = null; |
41 | ||
42 | 0 | protected boolean _checkValue = false; |
43 | ||
44 | protected static final String BARE_MESSAGE_EXISTENT_HEADER = " TO FIND HEADER IN RESPONSE"; | |
45 | protected static final String BARE_MESSAGE_EQUAL_VALUES = " THAT HEADER VALUES EQUAL:"; | |
46 | ||
47 | // need to store the value of the tested header, in order | |
48 | // to generate the right exception | |
49 | 0 | protected String _actualValue = null; |
50 | ||
51 | // ------------------------------------------------------------- Constructors | |
52 | ||
53 | /** | |
54 | * Constructs a validator for the given header with no label. | |
55 | * | |
56 | * @param headerName Name of the header to validate | |
57 | */ | |
58 | public ResponseHeaderValidator(String headerName) { | |
59 | 0 | this(null,headerName,true); |
60 | 0 | } |
61 | ||
62 | /** | |
63 | * Constructs a labeled validator for the given | |
64 | * header | |
65 | * | |
66 | * @param label name of the header to validate | |
67 | * @param headerName | |
68 | */ | |
69 | public ResponseHeaderValidator(String label, String headerName) { | |
70 | 0 | this(label,headerName,true); |
71 | 0 | } |
72 | ||
73 | /** | |
74 | * Constructs a labeled validator for the given | |
75 | * header | |
76 | * | |
77 | * @param label name of the header to validate | |
78 | * @param headerName | |
79 | */ | |
80 | public ResponseHeaderValidator(String label, String headerName, boolean condition) { | |
81 | 0 | super(label,condition); |
82 | 0 | _headerName = headerName; |
83 | 0 | } |
84 | ||
85 | /** | |
86 | * Sets the desired value of the header to be validated. | |
87 | * If this method is not called, then the validator will | |
88 | * only check for the existence of the header and | |
89 | * disregard its value. | |
90 | * | |
91 | * @param headerValue | |
92 | * desired value of the header, or null to look for a | |
93 | * header with the String value "null" | |
94 | */ | |
95 | public void setHeaderValue(String headerValue) { | |
96 | 0 | _checkValue = true; |
97 | ||
98 | 0 | if (headerValue == null) { |
99 | 0 | headerValue = "null"; |
100 | } | |
101 | 0 | _headerValue = headerValue; |
102 | 0 | } |
103 | ||
104 | ||
105 | /** | |
106 | * Checks to see that the header exists, and also checks | |
107 | * its value if setHeaderValue(String) has been called | |
108 | * | |
109 | * @param response The HTTP response to validate | |
110 | * @exception ValidationException | |
111 | * if the header does not meet the criteria of the test | |
112 | */ | |
113 | public boolean assertTrue(Response response) | |
114 | throws ValidationException { | |
115 | ||
116 | 0 | _actualValue = response.getHeader(_headerName); |
117 | ||
118 | 0 | if (_actualValue == null) { |
119 | 0 | return false; |
120 | } | |
121 | ||
122 | // if checkValue == false, we only care if the header exists | |
123 | 0 | if (_checkValue == false) { |
124 | 0 | return true; |
125 | } | |
126 | ||
127 | // check the entire value against the actual value | |
128 | 0 | if (_actualValue.equals(_headerValue)) { |
129 | 0 | return true; |
130 | } | |
131 | ||
132 | // if it does not match, see if the header has multiple values | |
133 | 0 | if (_actualValue.indexOf(",") != -1) { |
134 | 0 | StringTokenizer tokenizer = new StringTokenizer(_actualValue,","); |
135 | 0 | while (tokenizer.hasMoreTokens()) { |
136 | 0 | String token = tokenizer.nextToken().trim(); |
137 | 0 | if (token.equals(_headerValue)) { |
138 | 0 | return true; |
139 | } | |
140 | 0 | } |
141 | ||
142 | } | |
143 | ||
144 | 0 | return false; |
145 | } | |
146 | ||
147 | public String generateBareExceptionMessage() { | |
148 | ||
149 | 0 | if (_actualValue == null) { |
150 | 0 | return BARE_MESSAGE_EXISTENT_HEADER; |
151 | } else { | |
152 | 0 | StringBuffer buffer = new StringBuffer(BARE_MESSAGE_EQUAL_VALUES); |
153 | 0 | buffer.append(" EXPECTED: "); |
154 | 0 | buffer.append(_headerValue); |
155 | 0 | buffer.append(" RECEIVED: "); |
156 | 0 | buffer.append(_actualValue); |
157 | 0 | return buffer.toString(); |
158 | } | |
159 | } | |
160 | ||
161 | } |