Section 19.3. Service Test


19.3. Service Test

Automated, JavaScript, Remoting, UnitTest, TDD, Test, WebService, XMLHttpRequest

Figure 19-4. Service Test


19.3.1. Developer Story

Dave is creating a trading application. On the server, there are services to give quotes and accept orders. To help develop and test the web service, he builds a suite of Service Tests. One test, for example, submits an order and checks that the database registered it. These tests are automated and re-executed every time the project changes.

19.3.2. Problem

How can you test web services?

19.3.3. Solution

Build up automated tests of web services, using HTTP clients to interact with the server as the browser normally would. The browser-server interaction of an Ajax App is usually a sequence of XMLHttpRequest Calls (Chapter 6). That's fortunate, because testing the service is usually as easy as simulating the browserpassing a certain request and checking the response. Sometimes, verification might also involve checking what happened on the server, e.g., a database state change or a message to an external system.

The test itself can be built in any language and testing framework you like, because it's completely decoupled from both the browser and the server. The only practical constraint is that you should be able to run it from a script, to allow for continuous integration. Many languages provide an HTTP client library, so it's easy to build a test pass request to the server and make assertions against the response. Indeed, there are client frameworks like HTTPUnit (http://httpunit.sourceforge.net/) that are specifically for the purposes of testing web apps. They're typically used for testing conventional applications, where the response contains an entire page, but they work fine for the terse responses usually provided by web services.

A RESTful Service (Chapter 9), in particular, should be straightforward to test because it will usually output structured responses which are easily parsed; and there won't be any conversational state to set up and track. A service that outputs an HTML Message (Chapter 9) makes tests a bit more fragile; passing in 5+5 will always let you assert 10 as the Plain-Text Message (Chapter 9), but not always The <span >answer</class> is <span >10<span>!!! if an HTML Message is used.

19.3.4. Tool Support

19.3.4.1. Ruby Net::HTTP

Net::HTTP (http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/) is an HTTP client library for Ruby, shipping with the standard distribution. It's used in the refactoring illustration below.

19.3.4.2. Jakarta HTTPClient

Jakarta HTTPClient (http://jakarta.apache.org/commons/httpclient/) is a richly featured HTTP client framework for Java.

19.3.4.3. PHP HTTPClient

Simon Willison's HTTPClient (http://scripts.incutio.com/httpclient/) is a basic HTTP client framework for Perl.

19.3.5. Code Example: Service Test

The Basic Ajax Pattern Reader (http://ajaxify.com/run/reader/) uses some Simulation Services to get information from the server. A Service Test was first created against the Simulation Service, and once the Simulation Service passed, the test was used to drive the design of the real services. Earlier, the Solution mentioned that the programming language of the HTTP client is completely independent of the server or browser languages. That's not just theoretical, since some languages (e.g., Ruby and Perl) are particularly well-suited to string manipulation. To prove the point, the example here uses Ruby's Net::HTTP library.

The service being tested is patternSummary.phtml (http://ajaxify.com/run/reader/patternSummary.phtml), a pattern text provider that accepts a pattern name and outputs its solution. The test case is very basicit just requests the Slider pattern and checks that the output matches a simple regular expression:

   require "test/unit"   require "net/http"   class PatternServiceTest < Test::Unit::TestCase     def test_slider       sliderResponse = grabPattern("Slider");       regex = /^\s*<h1><a\ href=\".*?\">Slider<\/a> <\/h1>\s*\                 <span\ class=\"summary\">.+?<\/span>.+                 /mx;       assert_match(regex, sliderResponse.body);     end     ...     def grabPattern(patternName)       session = Net::HTTP.new('ajaxlocal', 80);       response = session.get("/run/reader/logging/realService/" +                               "patternSummary.phtml?patternName=" + patternName);       assert_equal("200", response.code);       return response;     end   end 

19.3.6. Related Patterns

19.3.6.1. Simulation Service

You can sometimes start creating a test against a Simulation Service (see earlier), and gradually refactor it into a real service, while evolving the test as well.

19.3.6.2. Browser-Side Test

A Browser-Side Test (see earlier) is a good complement to a Service Test, since the former tests browser behavior while the latter ensures the server will support the browser in the correct way.




Ajax Design Patterns
Ajax Design Patterns
ISBN: 0596101805
EAN: 2147483647
Year: 2007
Pages: 169

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net