Skip to content
Markus Krogemann edited this page Jun 6, 2013 · 19 revisions

Please note that you can find examples for all Matchers, Pollers and Probes in the unit and integration tests located in the /spec folder (eg in http_poller_spec.rb).

Plain HTTP Response Matcher Examples

Find below an example usage of HttpPoller and a ResponseBodyContains Matcher

require 'poller'
matcher = Matchers::HTTP::ResponseBodyContains.new(/your regex/)
# alternatively pass in a String
poller = Poller::HTTP::HttpPoller.new("http://your.sut.example.com", matcher, 5.0, 1.0)
#  timeout 5 seconds, poll once per second
poller.check

What this will do: Construct an HttpPoller which will in turn construct an HttpProbe from the given URL. These two objects will fetch the http response from given URL once per second, starting one second after receiving the #check message and trying until the timeout of 5 seconds is exceeded in which case a RuntimeError will be raised. The response body (if there is a response) will be fed into a ResponseBodyContains matcher that will match the response body with the given Regexp (as in the example above) or alternatively search a given String in it in case you passed in a String instead of a Regexp.

XPath Matcher Examples

XPathContainsText Matcher

Here is a usage example of HttpPoller in combination with an XPathContainsText matcher

require 'poller'
matcher = Matchers::XML::XPathContainsText.new('/CATALOG/CD/TITLE', 'Empire Burlesque')
# instead of a search String you can also pass in a Regexp
poller = Poller::HTTP::HttpPoller.new("http://www.w3schools.com/xml/cd_catalog.xml", matcher, 5.0, 1.0)
# timeout set to 5 seconds, poll every second
poller.check

In this example we pass an XPath expression and a search String into an XPathContainsText matcher. The HttpPoller's constructor receives the matcher and will in turn construct an HttpProbe from the given URL. As in the previous example, the http response is fetched from given URL once per second, starting one second after receiving the #check message and the fetching will be repeated until the timeout of 5 seconds is exceeded in which case a RuntimeError will be raised. The response body (if there is a response) will be fed into the given Matcher which will perform an XPath search for the given XPath expression and search the given String in the node's text. The match will fail if the node cannot be found or if the search string is not contained in the node's text.

DocumentContainsXPath Matcher

Next we have a usage example of HttpPoller in combination with a DocumentContainsXPath Matcher

require 'poller'
matcher = Matchers::XML::DocumentContainsXPath.new('/CATALOG/CD/ARTIST', 11)
poller = Poller::HTTP::HttpPoller.new("http://www.w3schools.com/xml/cd_catalog.xml", matcher, 5.0, 1.0)
#  timeout 5 seconds, poll once per second
poller.check

Here we pass an XPath expression and an expected number of occurrences into a DocumentContainsXPath matcher. The Polling behavior is as in the previous examples: HTTP response is fetched from given URL once per second, starting one second after receiving the #check message and the fetching will be repeated until the timeout of 5 seconds is exceeded in which case a RuntimeError will be raised. The response body (if there is a response) will again be fed into the given Matcher which will perform an XPath search for the given XPath expression and validate whether the XPath expression evaluates to a list of nodes of the expected size (eleven in the example above).

Using an Http Proxy with HttpPoller/HttpProbe

In case you need to go through an HTTP Proxy between test executor and the system under test, you can specify a Proxy definition as the last argument to the constructor of the HttpPoller. The properties can be given in a Hash like in the following example

proxy = { :hostname => 'proxy.internal.example.com', :port => 8080, :user => 'user', :password => '_secret' }
poller = Poller::HTTP::HttpPoller.new("http://your.sut.example.com", matcher, 5.0, 1.0, proxy)

Using Basic Auth with HttpPoller/HttpProbe

Basic Auth credentials can be included in the URL of the system under test as shown below

poller = Poller::HTTP::HttpPoller.new("http://user:[email protected]", matcher, 5.0, 1.0)