Betamax
Upcoming SlideShare
Loading in...5
×
 

Betamax

on

  • 1,930 views

Betamax as presentet on GR8Conf EU 2012 http://gr8conf.eu/Presentations/Betamax

Betamax as presentet on GR8Conf EU 2012 http://gr8conf.eu/Presentations/Betamax

Statistics

Views

Total Views
1,930
Views on SlideShare
1,704
Embed Views
226

Actions

Likes
3
Downloads
15
Comments
0

6 Embeds 226

http://localhost 138
http://gr8conf.eu 72
http://2012.gr8conf.eu 7
http://m.gr8conf.eu 4
http://lanyrd.com 3
http://gr8conf.org 2

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • As you scroll around AJAX requests are triggered to pull in TV listings\nData from REST services\nChannel list\n‘Chunks’ of programme data (4 hours x 10 channels)\nIndividual programme details\nWe have tests for…\nAll kinds of data conditions\nError handling, 404s, 503s\nEmpty responses\n
  • Want to talk about the problems Betamax addresses…\n\nIn unit tests components-under-test should be isolated\nEven end-to-end shouldn’t rely on resources outside your control\nHTTP makes tests brittle:\n3rd party outages break your tests\nResource limits break your tests\nCorporate network environment break your tests\nCoding on the go breaks your tests\nCan only test edge cases accidentally:\nwhat does your client do when… server is down?\nrate limit hit?\nit gets an invalid response?\nThis is *really* unacceptable on CI servers\n\n
  • Spin up an in-process instance of Sun webserver or jetty\n\nIt’s tedious to hand-craft responses\nTemptation is to oversimplify\n\nSmall differences may matter (headers, text encoding, content length)\nReinventing the wheel\nApplication needs configurable URLs or you have to DNS hack\n
  • May not be easy to inject mock\neasy for DI collaborators like an HttpClient instance\nnot so easy for new URL("http://example.com").text\nHttpClient not the most mock-friendly API\nRemember… every time a mock returns a mock a fairy dies\n
  • Repeatable:\nreturn consistent, predictable responses not just whatever server happens to be returning right now\n\nReliable:\nnot affected by 3rd party downtime\nnot affected by network availability\n\nRealistic:\nsimulate the real response correctly\n
  • Layer on top of Ruby HTTP mocking libraries like WebMock\nUses the metaphor of a ‘cassette’ with recorded responses\n
  • Goals:\nIdeally not just Groovy - JVM\nHandle various ways of connecting:\nURL\nURLConnection\nHttpClient\nHTTPBuilder\nChallenges:\nCan’t use Groovy MOP in non-Groovy scenario\nEven in Groovy the underlying code where HTTP connection is made is often Java\nAOP might work for DI but can’t intercept new URL("http://example.com").text\nCan’t even use metaClass as Groovy implementation written in Java\nThe name - Why Betamax? \n
  • Can intercept just about anything\nEasy to enable and disable programatically with system properties\nEven proxy is problematic:\nHttpClient ignores it by default\nHTTPS - it’s a man-in-the-middle attack\nProxy implemented with embedded Jetty\nTried different things but used because of lifecycle callbacks\nvert.x would be even nicer but would require Java 7\n\n\n
  • Explain what’s going on here:\nTwitterService makes REST connections to Twitter - let’s assume with URL but could be anything\n\n@Rule - JUnit\n@Betamax annotation on each test\nMultiple tests can share tapes\nTapes can hold multiple recordings\nPer-test configuration via annotation\nExplain request matching\nDefault method & uri\nDefault ideal for REST\nCan use other things; path, headers, request content, etc. (next slide)\n\n
  • Nothing different\nJUnit rules work in Spock\n
  • Default = method and uri\nIdeal for connecting to REST services\nMatch any request using any method going to the same host\nMatch requests based on POSTed data\nDifferent responses for different headers\ntest response to cache control headers\nsimulate different responses for different user-agent strings\nimprovements to be made - currently it matches all the headers\nIgnore query string\nuseful if requests add things like cache-busters or session ids (when not relevant to test)\n
  • 1 - default - can record new exchanges or play back existing ones\n2 - useful when you want to ensure you’ve captured everything - proxy returns error if recording not found\n3 - Overwrite mode - any existing recordings are overwritten\n
  • Why YAML?\nTried JSON - delimiter collision\nEditing\nBinary\n
  • Where does Betamax fit into your test architecture?\nUsing Betamax doesn’t mean you don’t need to isolate your HTTP interactions\nBuild a higher-level API around the HTTP connection code\ntest that with @Betamax\nmock it out in other code that depends on it\nDon’t just slap @Betamax on every test\nHowever, Betamax can be useful in end-to-end tests\n\n“You’re probably thinking…”\n
  • \n
  • Current status: early days but fully usable\n1.1-SNAPSHOT\n\nHTTPS support\nPrettified response bodies in tapes?\nVCR feature envy…\nCustom recording matching\nSpecific headers or body content\nGroovy expressions in tape files?\nVCR allows ERB expressions in cassette files\nSnakeyaml allows for this kind of thing\nWould have made a good hackergarten project\nAuto-expiring tapes?\nSimple HttpClient decorator as an alternative to the proxy?\nCould use MOP to intercept URL.text\nSolution for URL.openConnection?\n\n
  • Example Grails app included\n

Betamax Betamax Presentation Transcript

  • βetamax Rob FletcherFreeside Software / Energized Work
  • The impetus
  • HTTP connections in tests
  • Solution 1: Fake HTTP Server
  • Solution 2: Mock HTTP layer
  • Tests need to be…
  • Tests need to be… Repeatable Reliable Realistic
  • Meanwhile in Ruby…https://www.relishapp.com/myronmarston/vcr
  • Porting to JVM / Groovy
  • The Betamax Proxy
  • Using Betamax: JUnitclass TwitterServiceTest { @Rule public Recorder recorder = new Recorder() def twitterService = new TwitterService() @Betamax(tape = "twitter ok") @Test void searchesTweets() { def tweets = twitterService.search("#cocktailclub") assert tweets.size() == 20 assert tweets.text.every { it =~ /#cocktailclub/ } }}
  • Using Betamax: Spockclass TwitterServiceSpec extends Specification { @Rule Recorder recorder = new Recorder() def twitterService = new TwitterService() @Betamax(tape = "twitter ok") def "searches tweets"() { when: def tweets = twitterService.search("#cocktailclub") then: tweets.size() == 20 tweets.text.every { it =~ /#cocktailclub/ } }}
  • Matching Requests to Tapesimport static betamax.MatchRule.*@Betamax()@Betamax(match = host)@Betamax(match = [method, uri, body])@Betamax(match = [method, uri, headers])@Betamax(match = [method, host, path])
  • Tape modesimport static betamax.TapeMode.*@Betamax(mode = READ_WRITE)@Betamax(mode = READ_ONLY)@Betamax(mode = WRITE_ONLY)
  • Tape Files
  • Testing Practices
  • An example would be nice right now…
  • The future of Betamaxgithub.com/robfletcher/betamax/issues
  • http://freeside.co/betamax@betamaxtesthttp://freeside.co@rfletcherEW