Permalink
Browse files

Added new method for overriding the content handler

  • Loading branch information...
1 parent edbfcd4 commit 7ab18d994d99dc2e58259fa60742e67a2afd4b17 @gdunn gdunn committed May 5, 2012
@@ -902,7 +902,12 @@ protected void completeHttpMethodExecution() {
// Split out of completeHttpMethodExecution so RestScriptFixture can call this
protected BodyTypeAdapter createBodyTypeAdapter()
{
- ContentType ct = getContentTypeOfLastResponse();
+ return createBodyTypeAdapter(getContentTypeOfLastResponse());
+ }
+
+ // Split out of completeHttpMethodExecution so RestScriptFixture can call this
+ protected BodyTypeAdapter createBodyTypeAdapter(ContentType ct)
+ {
String charset = getCharsetOfLastResponse();
BodyTypeAdapter bodyTypeAdapter = partsFactory.buildBodyTypeAdapter(ct, charset);
bodyTypeAdapter.setContext(namespaceContext);
@@ -4,6 +4,7 @@
import smartrics.rest.client.RestData.Header;
import smartrics.rest.fitnesse.fixture.support.BodyTypeAdapter;
+import smartrics.rest.fitnesse.fixture.support.ContentType;
import smartrics.rest.fitnesse.fixture.support.HeadersTypeAdapter;
import smartrics.rest.fitnesse.fixture.support.LetHandler;
import smartrics.rest.fitnesse.fixture.support.LetHandlerFactory;
@@ -97,6 +98,13 @@ public boolean hasBody(String expected) throws Exception {
return equalsWithAdapter(expected, actual, bodyTypeAdapter);
}
+ public boolean hasBodyUsingType(String expected, String type) throws Exception {
+ ContentType ct = ContentType.typeFor(type);
+ BodyTypeAdapter bodyTypeAdapter = createBodyTypeAdapter(ct);
+ String actual = responseBody();
+ return equalsWithAdapter(expected, actual, bodyTypeAdapter);
+ }
+
public boolean hasHeaders(String expected) throws Exception {
return hasHeader(expected);
}
@@ -0,0 +1,42 @@
+!3 Overriding content handlers
+
+!-RestFixture-! uses three content handlers to parse and set expectations used to verify the behaviour of HTTP calls.
+These are the handlers currently available:
+ * '''XML''' interprets the body as an xml string and allows XPath expressions as expectations on that content
+ * '''JSON''' interprets the body as a JSON string and allows expectations to be written in JavaScript.
+ * '''TEXT''' interprets the body as a string and allows regular expression expectations.
+
+These are the defaults
+
+ * default=XML
+ * application/xml=XML
+ * application/json=JSON
+ * text/plain=TEXT
+
+!*** Note: unrecognised content types.
+Content types not recogised will be mapped to the default content handler (XML).
+***!
+
+For example, XML handler is used for for content types of application/xml; hence XPaths can be used to set expectations:
+
+|script|!-smartrics.rest.fitnesse.fixture.RestScriptFixture-!|http://${jettyHost}:${jettyPort}|
+|get|/resources/0|
+|check|status code|200|
+|ensure|has header|Content-Type : application/xml;.+|
+|ensure|has body|!-
+ /resource/name[text()='a funky name'] <br/>
+ /resource/data[text()='an important message']
+-!|
+
+With the !-RestScriptFixture-! you can specify a type to use with !-hasBodyUsingType-!
+
+|script|!-smartrics.rest.fitnesse.fixture.RestScriptFixture-!|http://${jettyHost}:${jettyPort}|
+|get|/resources/0|
+|check|status code|200|
+|ensure|has header|Content-Type : application/xml;.+|
+|ensure|has body|<resource>[\s.]*<name>a funky name</name>[\s\w\d<>/=\:'.]*</resource>[\s]*|using type|text/plain|
+|ensure|has body|!-
+ /resource/name[text()='a funky name'] <br/>
+ /resource/data[text()='an important message']
+-!|
+
@@ -0,0 +1,7 @@
+<?xml version="1.0"?>
+<properties>
+ <Edit/>
+ <Properties/>
+ <Test/>
+ <Versions/>
+</properties>

0 comments on commit 7ab18d9

Please sign in to comment.