1 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 35 public class ResponseHeaderValidator extends BaseConditionalValidator { 36 37 39 protected String _headerName = null; 40 protected String _headerValue = null; 41 42 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 protected String _actualValue = null; 50 51 53 58 public ResponseHeaderValidator(String headerName) { 59 this(null,headerName,true); 60 } 61 62 69 public ResponseHeaderValidator(String label, String headerName) { 70 this(label,headerName,true); 71 } 72 73 80 public ResponseHeaderValidator(String label, String headerName, boolean condition) { 81 super(label,condition); 82 _headerName = headerName; 83 } 84 85 95 public void setHeaderValue(String headerValue) { 96 _checkValue = true; 97 98 if (headerValue == null) { 99 headerValue = "null"; 100 } 101 _headerValue = headerValue; 102 } 103 104 105 113 public boolean assertTrue(Response response) 114 throws ValidationException { 115 116 _actualValue = response.getHeader(_headerName); 117 118 if (_actualValue == null) { 119 return false; 120 } 121 122 if (_checkValue == false) { 124 return true; 125 } 126 127 if (_actualValue.equals(_headerValue)) { 129 return true; 130 } 131 132 if (_actualValue.indexOf(",") != -1) { 134 StringTokenizer tokenizer = new StringTokenizer (_actualValue,","); 135 while (tokenizer.hasMoreTokens()) { 136 String token = tokenizer.nextToken().trim(); 137 if (token.equals(_headerValue)) { 138 return true; 139 } 140 } 141 142 } 143 144 return false; 145 } 146 147 public String generateBareExceptionMessage() { 148 149 if (_actualValue == null) { 150 return BARE_MESSAGE_EXISTENT_HEADER; 151 } else { 152 StringBuffer buffer = new StringBuffer (BARE_MESSAGE_EQUAL_VALUES); 153 buffer.append(" EXPECTED: "); 154 buffer.append(_headerValue); 155 buffer.append(" RECEIVED: "); 156 buffer.append(_actualValue); 157 return buffer.toString(); 158 } 159 } 160 161 } 162 | Popular Tags |