ELABORATION Other ways to stub out the Internet for AJAX Section 86 discusses

Elaboration other ways to stub out the internet for

This preview shows page 242 - 244 out of 517 pages.

ELABORATION: Other ways to stub out the Internet for AJAX Section 8.6 discusses how stubbing the Internet to isolate tests from external services can be done either “near the client” or “far from the client.” In Section 6.7 we stubbed “near the client” by stubbing $.ajax and forcing it to immediately call the success function rather than allowing it to proceed with the external HTTP request. This technique is similar to how we stubbed find_in_tmdb in Section 8.6 to return a value immediately rather than allowing it to make a real HTTP request. An alternative, which would more thoroughly exercise the code that handles the actual AJAX server responses, is to stub at the network level, just as FakeWeb does for Rails apps. Just as FakeWeb lets you provide “canned” XML or HTML responses based on the arguments of an XHR call, jasmine-ajax , a Jasmine extension from Pivotal Labs, lets you provide “canned” XML, HTML or JSON responses to
Image of page 242
AJAX XHR calls that are used instead of allowing the XHR call to proceed. You can then spy on the handler functions success , failure , timeout , and so on passed to $.ajax to make sure the correct handler is called depending on the server’s response. Self-Check 6.8.1. In Figure 6.28 showing the use of a JSON fixture, why do we also still need the HTML fixture to be loaded in line 4? Line 9 tries to trigger the click handler for an element matching #movies a , and if we don’t load the HTML fixture representing a row of the movies table, no such element will exist. (Indeed, the MoviePopupJson.setup function tries to bind a click handler on this element, so that would also fail.) This is an example of using both an HTML fixture to simulate the user clicking on a page element and a JSON fixture to simulate a successful response from the server in response to that click. ELABORATION: Same-origin policy You can also arrange for your SPA to communicate with a RESTful server façade (Section 11.6 ), as Figure 6.29 shows. You might do this if your SPA relies on content from multiple sites: for security, JavaScript browser apps are bound by a same origin policy , which says that a JavaScript app can only make AJAX requests to the same origin (scheme, host name, and port number, as described in Section 2.2 ) from which the app itself was served. Figure 6.29: Architecture of in-browser SPAs that retrieve assets from multiple distinct services. Left: If the JavaScript code was served from RottenPotatoes.com, the default same-origin policy that browsers implement for JavaScript will forbid the code from making AJAX calls to servers in other domains. The cross-origin resource sharing (CORS) specification relaxes this restriction but is only supported by very recent browsers. Right: in the traditional SPA architecture, a single server serves the JavaScript code and interacts with other remote services. This arrangement respects the same-origin policy and also allows the main server to do additional work on behalf of the client if needed.
Image of page 243
Image of page 244

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors