Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added blog: Why mock servers that don't mock matter

  • Loading branch information...
commit ce9ced55aeca3b109080c35e6a7de08a53204f7a 1 parent 1509704
@adriancole adriancole authored
Showing with 39 additions and 0 deletions.
  1. +38 −0 blogs/mockwebserver.html
  2. +1 −0  js/models.js
View
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="http://techblog.netflix.com/2013/03/denominator-multi-vendor-interface-for.html">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="https://twitter.com/adrianfcole/status/309152829778575361">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="http://javasourcecode.org/html/open-source/jdk/jdk-6u23/sun/net/www/protocol/http/HttpURLConnection.java.html">restricted header</a>, eventhough it isn't <a href="http://www.docjar.com/html/api/sun/net/www/protocol/http/HttpURLConnection.java.html">afterwards</a>. &nbsp;Typical mock tests cannot prove your application can work when they test above a&nbsp;<a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">library or runtime</a>&nbsp;that can change. &nbsp;These changes occur more than you think, especially with <a href="http://www.oracle.com/technetwork/java/eol-135779.html">Java 6 EOL</a> migrations and Java 8 around the corner.<br />
+<br />
+<a href="http://code.google.com/p/mockwebserver/">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="http://stackoverflow.com/questions/393099/mocking-http-server">similar to some</a>, mockwebserver is only several small classes with one <a href="http://code.google.com/p/mockwebserver/source/browse/trunk/src/main/java/com/google/mockwebserver/MockWebServer.java">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="https://github.com/Netflix/denominator">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="https://github.com/Netflix/denominator/blob/master/providers/denominator-route53/src/test/java/denominator/route53/Route53ResourceRecordSetApiMockTest.java">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 />
+<pre>@Test
+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));
+ server.play();
+
+ 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("www.foo.com.", "5.6.7.8"));
+ } 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?name=www.foo.com.&amp;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();
+ }
+}
+</pre>
+<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.
View
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')
];
Please sign in to comment.
Something went wrong with that request. Please try again.