The Easiest Way to Get Started with Ajax and REST

an article added by: Sonja Lande at 05312007


In: Root » Computers and technology » AJAX » The Easiest Way to Get Started with Ajax and REST

French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic

The Easiest Way to Get Started with Ajax and REST

Problem You want to know the best way to get started with writing Ajax and REST. Solution When developing an Ajax and REST application, you must decide on the tools and frameworks you’ll use. The choice is simple: Use whatever you’re using today, and write some Ajax applications. You don’t need to change the tools you’re using today. Whether you’re using ASP.NET, JavaServer Pages (JSP), PHP, Ruby, or Python, you can continue with those tools. Ajax uses JavaScript, DHTML, and the XMLHttpRequest object, but ASP.NET, PHP, and similar technologies don’t hinder you from writing HTML pages that make use of Ajax techniques. If your technology does hinder you from writing Ajax applications, then you should think hard about continuing using the technology.

After all, you’re reading an Ajax and REST recipe article, so I assume that you plan on implementing Ajax and REST solutions. When decoupling the client from the server, you can create the content on either side independently. You can develop the client using technologies such as DHTML and JavaScript. Within the client, you can code references to services offered by the client. The client-side code provides an infrastructure where the content generated by the services can be injected. The client and server interact with each other using contracts. Using contracts, you can develop the client independently and test it using mock objects. Using contracts, you can develop the server independently and test it using tests from a test suite. Then when the client is combined with the server, the application will work without requiring a large amount of further testing. Of course, this assumes that the tests for the client and server are implemented properly normal testing is often also required. Having decoupled the client from the server, you can easily modularize and delegate the

implementation work to individual team members. Allowing each team member to focus on the task makes it possible to specialize and create innovative content. For example, delegating the database work to the server allows a client developer to make more use of graphics and innovative representations of the data generated by the service. Delegating the UI work from the server to the client side makes it possible for the server developer to focus on database optimization and access speeds. Having decoupled the client from the server, you can make use of specific frameworks to make it simpler to implement specific pieces of logic. For example, a client-side developer could use the Prototype8 or Dojo9 frameworks. Which toolkit you end up using is your choice, and there is no right or wrong answer. You need to investigate what you need and see if the framework offers that functionality. When getting started with Ajax and REST, remember the following points:

• You can use Ajax and REST today with existing technologies. You generally don’t need to throw out old technologies and replace them with new ones.

• Ajax and REST are about decoupling the client from the server and making using of Web services.

• Ajax and REST frameworks can make it simpler for you to implement your applications, but because there are so many frameworks, you need to inspect them to see if they fulfill your needs.

Implementing an Ajax and REST Application Using Test-Driven Development Techniques After you’re convinced that you want to develop Ajax and REST applications, you’ll want to execute some testing routines. Problem You want to know the best way to test your Ajax and REST applications. Solution This recipe explains the different layers of test-driven development techniques.10 There are different layers because an Ajax and REST application involves both client-side and server-side code. To put it simply, you don’t hire a few users to test an Ajax and REST application and get them to try out application scenarios. As stated earlier, the server side and client side are decoupled from each other.

This is a good approach for testing purposes, because you can develop and test the client and server independently of each other. An architect has the ability to define a contract between the client and server, enabling each to work independently of each other. URLs. The four layers range in implementation complexity from complicated to straightforward. Each layer, which is explained as follows, is associated with a numeric identifier:

• GUI-level tests involve testing the Ajax and DHTML user interface.

• REST-level tests test the REST and Web service interfaces for correct implementation of the defined contracts.

• Server-side class-level tests test the implementation of the functionality using test-driven development techniques.

• Mock URL-level tests are not actually tests, but rather implement the contracts defined by the REST and Web service interfaces. The mock implementations allow you to test the GUI without needing a completed server-side implementation. Each layer requires using a different testing toolkit, as each layer tests a different aspect of the Ajax and REST application. However, this raises a question: Do you start developing with the server side or the client side? Do you develop using top-down techniques or bottom-up techniques? You could develop all the layers at once using agile techniques, though it’s not a good idea. The problem is that by using agile techniques on all of the layers at once, you instantly create a communications overhead and defeat the purpose of decoupling the client from the server. In a complete agile manner, the client, contract, and server are all developed at once. If the client has a problem, that might cause a change in the contract and the server, causing the client and server to become coupled. It is not to say that you shouldn’t develop using agile techniques. What you need to do is direct the agile techniques so that the client and server are decoupled from each other. Thus, the first thing you should develop are the contracts that the client uses and the server provides. The idea behind this architecture is to test and implement a complete user case without actually implementing the client or server. The testing layer 2 represents a set of tests used to verify that the server-side implementation is complete.

The Mock URL layer represents a set of tests used to verify that the client-side implementation is complete. By having the testing layer 2 verify the data generated by the Mock URL layer, the contracts for completeness are verified. Practically speaking, you could use a programming language such as Java to make a series of Web service calls that define a contract. These Web service calls represent scenarios that the client implementation would execute. You would implement the scenarios using agile techniques defined by application use cases. For example, if a use case is to open a bank account, then you would create a test that would make the appropriate Web service calls to open a bank account. A test cannot function without some implementation. And since you don’t have an implementation, you must fake the request and response or, more appropriately, use the Mock URL framework. The role of the Mock URL framework is to anticipate the client tests. When a test is underway, the Mock URL framework verifies the data sent by the test and then generates the appropriate response. The verification and generation are the result of performing some logic and loading and sending pregenerated application data. It is important that the Mock URL not implement business logic, but rather use canned logic and pregenerated requests and responses as much as possible. When the contracts are implemented properly, the tests should not be able to tell if a live implementation is generating the data or if some layer has faked the data.

Appropriately, the Mock URL framework should not be able to tell if it is being called by a series of tests or if it is live client implementation. The combination of tests and Mock URLs allows you to use agile and test-driven techniques to create the contracts that the client and server need to implement. If you feel that creating a complete mock layer is too much work, then you could create an implementation that has canned returned values. Having defined the contract, the implementations of the client and server know what they need to do. In order for you to use agile techniques to implement the client and server, the tests have to be a finer granularity than the contracts. The implementation tests need to be extensive and go beyond the contract and may include other aspects such as data initialization and presentation. For example, the test layers 1 and 3, are not related directly to the contracts and are used to test the client- and server-side implementations. Starting with layer 3, you use the tests to test the functionality of the implemented serverside logic.

From a programmatic perspective, this means that a clear separation exists between the implemented logic and the technology used to present that logic using the HTTP protocol. The test layer 3 doesn’t depend on or care about how the logic is exposed to the HTTP protocol. The tests in layer 3 focus on making sure that the server-side logic is implemented correctly. The contracts that the tests verify are not exposed externally, and the client doesn’t care what the tests are. Because the tests are private, the server developer can define their class structure, using whatever technology desired without affecting the client. Testing the server requires using the correct test framework; a few of these frameworks are outlined as follows:

JUnit (http://www.junit.org): Java test-driven development framework. JUnit is the original unit-testing tool.

NUnit (http://www.nunit.org): .NET unit-testing framework that uses .NET attributes.

PyUnit (http://pyunit.sourceforge.net/): Python unit-testing framework.

PHPUnit (http://www.phpunit.de/wiki/Main_Page): PHP unit-testing framework.

Test::Unit (included with Ruby distribution): Ruby unit-testing framework. If your programming language is not mentioned, do a search for the term "[Insert your language] unit test". Regardless of the programming language, the unit-testing framework and approach are the same. You use agile and test-driven techniques to implement server-side logic. In the architecture of testing the client-side logic, most if not all of what’s tested is the correctness of the JavaScript code. Notice in the architecture how the test layer 1 tests the scripts and not the DHTML user interface. This is on purpose and relates to the complexities of testing DHTML user interfaces. When JavaScript and DHTML are combined, you get a mostly predictable user interface. Contrast that to a traditional user interface, where elements are designed to occupy fixed areas.

Knowing that a user interface has to look a certain way makes it possible to use GUI test tools that take image snapshots and compare them to each other. While it’s possible to control the exact look of a DHTML user interface, it is not recommended because it contradicts the purpose of DHTML. Remember that DHTML contains the word dynamic, which indicates the ability to determine the layout of a user interface at runtime. Therefore, you cannot use classical user-interface testing techniques. Instead, you need to employ a thin-layer testing approach.11 Using a utility such as JsUnit,12 you could write a series of scripts for the server side and to execute user-interface logic. The test scripts would exercise the client logic and ensure that the application works properly. However, this solution is not ideal, because any logic that is embedded into the DHTML is not tested, so errors could potentially occur. When figuring out how to implement test-driven development techniques, remember the following points:

• An Ajax application contains four main test layers: client side, server side, contract, and Mock URL.

• The contract and Mock URL tests are developed simultaneously using agile development techniques, and they implement application use cases.

• You can use the contract and Mock URL tests for REST, SOAP, and other protocols.

• The client- and server-side tests are specific to the client or server and are used to implement test-driven development.

• The client side should not be dependent on the implementation details of the server, and the server should not be dependent on the implementation details of the client.

legal disclaimer

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

related articles

1. Ajax
Given the recent interest in Ajax, you’d be forgiven for thinking it was a new technology. In fact, the XMLHttpRequest object has been around for years. In technical terms, asynchronous JavaScript interaction with the server is nothing new. All of the other elements of the Ajax model have also been around for quite some time: CSS, (X)HTML, and DOM Scripting. Yet in 2005, interest in this methodology soared. Could it really be that simply giving this approach a snappy name like Ajax was responsible for the sudde...

2. Understanding the Definition and Philosophy of Ajax
The focus of this article is to provide solutions to some common, general problems and questions that are bound to arise before or during development of Asynchronous JavaScript and XML (Ajax) and Representational State Transfer (REST) applications. These common questions are not always technical in nature, often leaning more toward theory or philosophy of development. The problem with these kinds of questions is that once you begin to think about them, you keep going in a circle and end up where you star...

3. Understanding the Definition and Philosophy of Web Services and SOA
Understanding the Definition and Philosophy of Web Services and SOA Wikipedia offers the following definition of Web services:4 The W3C defines aWeb service as a software system designed to support interoperable machine-to-machine interaction over a network. This definition encompasses many different systems, but in common usage the term refers to those services that use SOAPformatted XML envelopes and have their interfaces described by WSDL. For ex...

4. Understanding the Definition and Philosophy of REST
Understanding the Definition and Philosophy of REST REST is a controversial topic among Web service enthusiasts, because it’s considered to stand for the opposite of what Web services and SOA are trying to achieve. The problem with this thinking is that REST is not in contradiction with the abstract definition of SOA and Web services. REST is in contradiction with technologies such as SOAP, WSDL, and WS-* specifications. The following offers a quick definition of REST:...

5. Testing a Dynamic Contract with Ajax
Coding the Contract Using Test-Driven Development Techniques Coding the contract using agile and test-driven development techniques requires writing a number of tests and implementing aMock URL layer. Problem You want to code the contract using these development techniques. Solution To demonstrate, let’s define a use case, implement the use case as a contract, write a test case(s) to implement the contract, implement the contract in the Mock URL, and finally...

6. Testing the Client Side Logic
Problem You want to effectively test your application’s client-side logic. Theory Testing GUI code tends not to be a productive task because of the complications that arise. The main complication is how to test the correctness of a user interface. Imagine a situation where clicking a button causes a table to be filled with data. Now imagine that when a check box is checked and the button is clicked again, a different table is filled with content. The fact that clicking the same button results in two ...

7. Understanding JavaScript and Types
Understanding JavaScript and Types Problem You want to work around the fact that JavaScript does not have types declared for its variables. Theory JavaScript code does not have any variables with a declared type. The lack of typed variables is apparent when you declare functions. That said, not having typed variable declarations does not mean JavaScript has no types or no type safety. Let’s start out with the simple declaration of a function, as illustrated by the following ex...

8. Coding Using Conventions and Not Configurations
Coding Using Conventions and Not Configurations Problem You want to make your JavaScript constructs more efficient by applying the Rails “convention over configuration” principle to them. Theory You may already be familiar with the programming platform Ruby on Rails, which is used to build Web applications. The focus of this recipe is not Ruby on Rails, but one aspect of Ruby on Rails namely, convention over configuration (see http://en.wikipedia.org/wiki/ Ruby_on_Rails for m...

9. Advantage of parameterless functions in JavaScript
Using Parameterless Functions Problem You want to take advantage of parameterless functions in JavaScript. Theory JavaScript functions for the most part have parameters. You may think that the previous sentence states the obvious after all, without parameters, what data could be passed to a function? JavaScript has the ability to declare functions that have no parameters, even though the caller of the function has passed parameters to the function. For example, let’s look at...

10. JavaScripot Functions
Treating Functions Like Objects Problem You want to take advantage of the fact that functions are objects (remember, everything is an object in JavaScript). Theory Many people think that a function is some keyword used in JavaScript. A function is also an object that can be manipulated. Knowing that a function is an object makes it very interesting from the perspective of writing JavaScript code, because the code can treat the function like another other object. This mean...