Skip to content
Browse files

Merge pull request #49 from adriancole/blog-mockwebserver

added blog: Why mock servers that don't mock matter
  • Loading branch information...
2 parents 1509704 + ce9ced5 commit cb2ddca41a21dc046ad8fa4f5e58a7ef8c3dafbc @stonse stonse committed Mar 8, 2013
Showing with 39 additions and 0 deletions.
  1. +38 −0 blogs/mockwebserver.html
  2. +1 −0 js/models.js
38 blogs/mockwebserver.html
@@ -0,0 +1,38 @@
+<h1>Why mock servers that don't mock matter</h1>
+Operations folk can relate to the idea that best intentions, and cleverest libraries, do not a stable system make. &nbsp;More recently in <a href="">denominator</a>, I've relied on the ability to check what's going onto the wire as a foundation for crafting http api requests. &nbsp;A <a href="">quick poll</a> suggests I should say more.<br />
+<br />
+When using libraries or otherwise creating http requests, a false sense of security is your worst nightmare. &nbsp;For example,&nbsp;in java 6&nbsp;<code>Expect</code> is a <a href="">restricted header</a>, eventhough it isn't <a href="">afterwards</a>. &nbsp;Typical mock tests cannot prove your application can work when they test above a&nbsp;<a href="">library or runtime</a>&nbsp;that can change. &nbsp;These changes occur more than you think, especially with <a href="">Java 6 EOL</a> migrations and Java 8 around the corner.<br />
+<br />
+<a href="">mockwebserver</a>&nbsp;is a great way to setup truths that your application must adhere to, without regard to how it is crafted. &nbsp;Despite its name, mockwebserver literally reads bytes off the wire to assert the expected behavior. &nbsp;Unlike many libraries, but <a href="">similar to some</a>, mockwebserver is only several small classes with one <a href="">main controller</a>. &nbsp;The small code base is easy to read when you are in the middle of debugging a gnarly issue.<br />
+<br />
+Case in point: in <a href="">denominator</a>, I want to assure any aggregate commands needed for a dns change are ordered and have the expected result. &nbsp;In a <a href="">mock test</a>, setup tests like below to make assert that behavior. &nbsp;The only requirement is that my code can accept a url.<br />
+<br />
+public void addSecondRecordRecreatesRRSetAndRetainsTTL() throws IOException, InterruptedException {
+ MockWebServer server = new MockWebServer();
+ // enqueue responses your code expects. Too few here and your test will hang!
+ server.enqueue(new MockResponse().setResponseCode(200).setBody(oneRecord));
+ server.enqueue(new MockResponse().setResponseCode(200).setBody(changeSynced));
+ try {
+ // ensure your constructors can take a url so that you can wire in mockwebserver
+ Route53ResourceRecordSetApi api = new Route53ResourceRecordSetApi(mockRoute53Api(server.getUrl("/").toString()));
+ api.add(a("", ""));
+ } finally {
+ // *optionally* check the requests you sent.
+ // I skip this when I enqueue errors to validate exception messages
+ RecordedRequest listNameAndType = server.takeRequest();
+ assertEquals(listNameAndType.getRequestLine(),
+ "GET /2012-02-29/hostedzone/Z1PA6795UKMFR9/rrset?;type=A HTTP/1.1");
+ RecordedRequest createRRSet = server.takeRequest();
+ assertEquals(createRRSet.getRequestLine(), "POST /2012-02-29/hostedzone/Z1PA6795UKMFR9/rrset HTTP/1.1");
+ assertEquals(new String(createRRSet.getBody()), replaceWith2ElementRecordSet);
+ server.shutdown();
+ }
+<br />
+Tests written in mockwebserver, applied across a number of JREs, will bullet-proof your tests for your supported configurations. &nbsp;In summary, it doesn't really matter if you choose to use mockwebserver or something else to assure your http traffic shapes as expected. &nbsp;Main idea is to be strict so that regardless of runtime shifts, you know what your application sends and what it is resilient to.
1 js/models.js
@@ -51,6 +51,7 @@ var tabs = [
var blogs = [
+ new BlogSpec('mockwebserver, 'March 8, 2013', 'Why mock servers that don't mock matter')
new BlogSpec('meetup-2', 'March 7, 2013', 'NetflixOSS Meetup - Season 1, Episode 2')

0 comments on commit cb2ddca

Please sign in to comment.
Something went wrong with that request. Please try again.