001 /* 002 * Copyright 1999,2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.apache.commons.latka.validators; 018 019 import java.util.StringTokenizer; 020 021 import org.apache.commons.latka.ValidationException; 022 023 import org.apache.commons.latka.http.Response; 024 025 /** 026 * ResponseHeaderValidator validates response headers in 027 * an HTTP session. Setting just the header name tests 028 * for the existence of a header. If you set the 029 * headerValue and there is more than one header of that 030 * name, the validator matches on ANY header with that 031 * value. 032 * 033 * @author Morgan Delagrange 034 */ 035 public class ResponseHeaderValidator extends BaseConditionalValidator { 036 037 // --------------------------------------------------------------- Attributes 038 039 protected String _headerName = null; 040 protected String _headerValue = null; 041 042 protected boolean _checkValue = false; 043 044 protected static final String BARE_MESSAGE_EXISTENT_HEADER = " TO FIND HEADER IN RESPONSE"; 045 protected static final String BARE_MESSAGE_EQUAL_VALUES = " THAT HEADER VALUES EQUAL:"; 046 047 // need to store the value of the tested header, in order 048 // to generate the right exception 049 protected String _actualValue = null; 050 051 // ------------------------------------------------------------- Constructors 052 053 /** 054 * Constructs a validator for the given header with no label. 055 * 056 * @param headerName Name of the header to validate 057 */ 058 public ResponseHeaderValidator(String headerName) { 059 this(null,headerName,true); 060 } 061 062 /** 063 * Constructs a labeled validator for the given 064 * header 065 * 066 * @param label name of the header to validate 067 * @param headerName 068 */ 069 public ResponseHeaderValidator(String label, String headerName) { 070 this(label,headerName,true); 071 } 072 073 /** 074 * Constructs a labeled validator for the given 075 * header 076 * 077 * @param label name of the header to validate 078 * @param headerName 079 */ 080 public ResponseHeaderValidator(String label, String headerName, boolean condition) { 081 super(label,condition); 082 _headerName = headerName; 083 } 084 085 /** 086 * Sets the desired value of the header to be validated. 087 * If this method is not called, then the validator will 088 * only check for the existence of the header and 089 * disregard its value. 090 * 091 * @param headerValue 092 * desired value of the header, or null to look for a 093 * header with the String value "null" 094 */ 095 public void setHeaderValue(String headerValue) { 096 _checkValue = true; 097 098 if (headerValue == null) { 099 headerValue = "null"; 100 } 101 _headerValue = headerValue; 102 } 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 _actualValue = response.getHeader(_headerName); 117 118 if (_actualValue == null) { 119 return false; 120 } 121 122 // if checkValue == false, we only care if the header exists 123 if (_checkValue == false) { 124 return true; 125 } 126 127 // check the entire value against the actual value 128 if (_actualValue.equals(_headerValue)) { 129 return true; 130 } 131 132 // if it does not match, see if the header has multiple values 133 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 }