/** * Verify that response matches supplied status */ public RequestExecutor assertStatus(int expected) { assertNotNull(this.toString(), response); int status = response.getStatusLine().getStatusCode(); assertEquals(this + ": expecting status " + expected + " (content: "+contentString+")", expected, status); return this; }
/** * For each supplied string, fail unless content contains it */ public RequestExecutor assertContentContains(String... expected) throws ParseException { assertNotNull(this.toString(), response); for (String exp : expected) { if (!contentString.contains(exp)) { fail(this + ": content does not contain '" + exp + "', content=\n" + contentString); } } return this; }
/** * Verify that response matches supplied charset */ public RequestExecutor assertCharset(String expected) { assertNotNull(this.toString(), response); if (entity == null) { fail(this + ": no entity in response, cannot check content type"); } assertEquals(this + ": expecting charset " + expected, expected, charset == null ? null : charset.name()); return this; }
/** * For each supplied regexp, fail unless <ul> * <li><code>expected</code>: content contains at least one line that matches. * <li><code>!expected</code>: content contains any line that mathces. * <ul> * Regexps are automatically prefixed/suffixed with .* so as * to have match partial lines. */ public RequestExecutor assertContentRegexp(boolean expected, String... regexp) { assertNotNull(this.toString(), response); nextPattern: for (String expr : regexp) { final Pattern p = Pattern.compile(".*" + expr + ".*"); final LineIterator it = new LineIterator(new StringReader(contentString)); while (it.hasNext()) { final String line = it.nextLine(); if (expected & p.matcher(line).matches()) { continue nextPattern; } if(!expected & p.matcher(line).matches()) { fail(this + ": match found for regexp '" + expr + "', content=\n" + contentString); } } if(expected){ fail(this + ": no match for regexp '" + expr + "', content=\n" + contentString); } } return this; }
public RequestExecutor assertHeader(String key, String...values){ assertNotNull(this.toString(),response); Set<String> expectedValues; if(values == null || values.length<1){ expectedValues = null; } else { expectedValues = new HashSet<String>(Arrays.asList(values)); } Header[] headers = response.getHeaders(key); if(headers.length < 1){ headers = null; } if(expectedValues == null){ assertTrue("The header "+key+" MUST NOT have any values (values: "+headers+")", headers == null); } else { assertNotNull("There are no values for header "+key+"!", headers); for(Header header : headers){ assertTrue("Unexpected header value "+header.getValue(), expectedValues.remove(header.getValue())); } assertTrue("Missing header values "+expectedValues+"!", expectedValues.isEmpty()); } return this; }
/** * Verify that response matches supplied content type */ public RequestExecutor assertContentType(String expected) { assertNotNull(this.toString(), response); if (entity == null) { fail(this + ": no entity in response, cannot check content type"); } // And check for match assertEquals(this + ": expecting content type " + expected, expected, contentType.getMimeType()); return this; } /**