Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Droidcon NYC 2017 - Advanced HTTP Mocking with ...

Sam Edwards
September 25, 2017

Droidcon NYC 2017 - Advanced HTTP Mocking with WireMock

Sam Edwards

September 25, 2017
Tweet

More Decks by Sam Edwards

Other Decks in Technology

Transcript

  1. @HandstandSam #DCNYC17 Why Mock HTTP Calls? • Determinism & Reliability

    • Avoid Rate Limits and Server Costs • Avoid Modifying State • Edge Cases & Error Scenarios • Speed
  2. @HandstandSam #DCNYC17 Why Mock the HTTP Server? • Exercises your

    full networking stack. • Knowledge of the app’s inner-workings are not needed. • Swap your HTTP implementation without issues. • Use recorded HTTP responses to create representative test cases. • Test that your code survives in situations like 500 errors or slow-loading responses.
  3. @HandstandSam #DCNYC17 When should I use HTTP Mocking? • Android

    Tests! • Mock Flavor of App • Not in your normal Android builds
  4. @HandstandSam #DCNYC17 public class ErrorScenariosTest { @Rule public ActivityTestRule<HomeActivity> mActivityTestRule

    = new ActivityTestRule<>(HomeActivity.class); @Test public void http500ErrorTest() { stubFor(get(urlEqualTo("/category/Nintendo/items")) .willReturn(aResponse().withStatus(500))); onView(allOf(withId(R.id.categories), isDisplayed())) .perform(actionOnItemAtPosition(0, click())); onView(withText("Networking Error")).check(matches(isDisplayed())); } }
  5. @HandstandSam #DCNYC17 public class ErrorScenariosTest { @Rule public ActivityTestRule<HomeActivity> mActivityTestRule

    = new ActivityTestRule<>(HomeActivity.class); @Test public void http500ErrorTest() { stubFor(get(urlEqualTo("/category/Nintendo/items")) .willReturn(aResponse().withStatus(500))); onView(allOf(withId(R.id.categories), isDisplayed())) .perform(actionOnItemAtPosition(0, click())); onView(withText("Networking Error")).check(matches(isDisplayed())); } }
  6. @HandstandSam #DCNYC17 public class ErrorScenariosTest { @Rule public ActivityTestRule<HomeActivity> mActivityTestRule

    = new ActivityTestRule<>(HomeActivity.class); @Test public void http500ErrorTest() { stubFor(get(urlEqualTo("/category/Nintendo/items")) .willReturn(aResponse().withStatus(500))); onView(allOf(withId(R.id.categories), isDisplayed())) .perform(actionOnItemAtPosition(0, click())); onView(withText("Networking Error")).check(matches(isDisplayed())); } }
  7. @HandstandSam #DCNYC17 WireMock Mappings HTTP GET - /hello - “Hello!”

    POST - /hi “hi.” Mappings ANY - /goodbye “Goodbye.” File-based REST API JVM
  8. @HandstandSam #DCNYC17 WireMock Mapping { "request": { "method": "GET", "urlPath":

    "/hello" }, "response": { "status": 200, “body": “Hello!”, "headers": { "Content-Type": "text/plain" } } }
  9. @HandstandSam #DCNYC17 Record File-based Mappings to Disk while Proxying Android

    App Web Service /__files /mappings https://shopping-app.s3.amazonaws.com
  10. @HandstandSam #DCNYC17 Record File-based Mappings to Disk Programmatic Java WireMockServer

    wireMockServer = new WireMockServer(wireMockConfig().withRootDirectory(getRootDirectory())); wireMockServer.enableRecordMappings(new SingleRootFileSource(getMappingDirectory()), new SingleRootFileSource(getFileDirectory())); wireMockServer.stubFor(any(urlMatching(“.*”)).willReturn(aResponse() .proxiedFrom(“https://shopping-app.s3.amazonaws.com”))); wireMockServer.start(); Command Line java -jar wiremock-standalone-2.8.0.jar \
 --proxy-all https://shopping-app.s3.amazonaws.com \
 --record-mappings
  11. @HandstandSam #DCNYC17 File-Based Recording & Playback on Android • Caveats:

    • Must configure to use External Storage/SD Card for __files and mappings • Recommendation (getting started): • Use Emulator & Run WireMock on Laptop • Connect Emulator to http://10.0.2.2:8080
  12. @HandstandSam #DCNYC17 Playback File-based Mappings from Disk Programmatic Java WireMockServer

    wireMockServer = new WireMockServer(wireMockConfig().withRootDirectory(getRootDirectory())); wireMockServer.start(); Command Line java -jar wiremock-standalone-2.8.0.jar
  13. @HandstandSam #DCNYC17 WireMock Mapping { "request": { "method": "GET", "urlPath":

    "/hello" }, "response": { "status": 200, “body": “Hello!”, "headers": { "Content-Type": "text/plain" } } }
  14. @HandstandSam #DCNYC17 Running WireMock Standalone JAR • Download Standalone JAR

    from Maven Central • java -jar wiremock-standalone-2.8.0.jar • --port • --https-port • http://localhost:8080/__admin/
  15. @HandstandSam #DCNYC17 Programmatic Stubbing via REST API curl --verbose \

    -X POST \ --data '{ "request": { "method": "GET", "urlPath": "/hello" }, "response": { "status": 200, "body": "Hello world!", "headers": { "Content-Type": "text/plain" } } }' \ 'http://localhost:8080/__admin/mappings'
  16. @HandstandSam #DCNYC17 Stubbing via REST API - String Body curl

    --verbose \ -X POST \ --data '{ "request": { "method": "GET", "url": "/category/Nintendo/items" }, "response": { "status": 200, "body": "[{\"label\": \"?\"}]" } }' \ 'http://localhost:8080/__admin/mappings'
  17. @HandstandSam #DCNYC17 Stubbing via REST API - JSON Response curl

    --verbose \ -X POST \ --data '{ "request": { "method": "GET", "url": "/category/Nintendo/items" }, "response": { "jsonBody": [{ "image": "https://shopping-app.s3.amazonaws.com/droidcon-nyc/droidcon-nyc.png", "label": "Droidcon NYC" }] } }' \ 'http://localhost:8080/__admin/mappings'
  18. @HandstandSam #DCNYC17 Stubbing via REST API - Binary Response curl

    --verbose \ -X POST \ --data '{ "request": { "method": "GET", "url": "/category/Nintendo/items" }, "response": { "base64Body": "W3sibGFiZWwiOiAiQmFzZTY0IEVuY29kZWQifV0=" } }' \ 'http://localhost:8080/__admin/mappings'
  19. @HandstandSam #DCNYC17 Stubbing via REST API - 500 Error Response

    curl --verbose \ -X POST \ --data '{ "request": { "method": "GET", "url": "/category/Nintendo/items" }, "response": { "status": 500 } }' \ 'http://localhost:8080/__admin/mappings'
  20. @HandstandSam #DCNYC17 URL Pattern Stub curl --verbose \ -X POST

    \ --data '{ "request": { "method": "GET", "urlPathPattern": "/category/.*/items$" }, "response": { "jsonBody": [{ "image": "https://shopping-app.s3.amazonaws.com/droidcon-nyc/kittens.jpg", "label": "Kittens" }] } }' \ 'http://localhost:8080/__admin/mappings'
  21. @HandstandSam #DCNYC17 Query Parameter Stub curl --data '{ "request": {

    "method": "GET", "urlPath": "/protected", "queryParameters":{ "password": { "equalTo": "password" } } }, "response": { "body": "<h1>How did you know that my password was 'password'?</h1>" } }' \ 'http://localhost:8080/__admin/mappings'
  22. @HandstandSam #DCNYC17 Other Complex Stubbing • Header Matching (Serve Multiple

    API Versions) • Request Body Matching • Equal To • Contains • JSON Body • Doesn’t care about order of JSON elements
  23. @HandstandSam #DCNYC17 Pre-Defined Fault Types • EMPTY_RESPONSE • Return a

    completely empty response. • MALFORMED_RESPONSE_CHUNK • Send an OK status header, then garbage, then close the connection. • RANDOM_DATA_THEN_CLOSE • Send garbage then close the connection.
  24. @HandstandSam #DCNYC17 Simulating Faults curl --verbose \ -X POST \

    --data '{ "request": { "method": "GET", "url": "/category/Nintendo/items" }, "response": { "fault": "MALFORMED_RESPONSE_CHUNK" } }' \ 'http://localhost:8080/__admin/mappings'
  25. @HandstandSam #DCNYC17 Stub Specific Fixed Delay curl --verbose \ -X

    POST \ --data '{ "request": { "method": "GET", "url": "/category/Nintendo/items" }, "response": { "body": "[{\"label\": \"Better Late Than Never\"}]", "fixedDelayMilliseconds": "2000" } }' \ 'http://localhost:8080/__admin/mappings'
  26. @HandstandSam #DCNYC17 1. One Stateful Behavior with Scenarios Shopping List

    Scenario States GET /items Required: “Started” New State: “Two Items” 1. One 2. Two GET /items Required: “Two Items” New State: “Three Items” 1. One 2. Two 3. Three GET /items Required: “Three Items” New State: “Started”
  27. @HandstandSam #DCNYC17 Verifying Network Calls (Mockito-like Syntax) • Verify Called

    Once
 
 verify(getRequestedFor(urlEqualTo(“/category/Nintendo/items”))); • Verify Called 3 Times
 
 verify(3, getRequestedFor(urlEqualTo(“/category/Nintendo/items”))); • Full Request Log
 
 List<ServeEvent> allServeEvents = wireMockServer.getAllServeEvents();
  28. @HandstandSam #DCNYC17 Programmatic Mocks > File-based Mocks • Save time

    by avoiding time consuming record/playback cycle. • Generate time-sensitive data (yesterday, etc). • Iterate through models to generate multiple ID specific mappings. • Hold a programmatic model of expected results for assertions. • Again, save a LOT of time after a small-ish upfront investment.
  29. @HandstandSam #DCNYC17 File-based Mocks > Programmatic Mocks • Recording •

    Getting Started • Simple Cases • Working with Non-Developers
  30. @HandstandSam #DCNYC17 WireMock on Android - Dependencies dependencies { androidTestCompile(“com.github.tomakehurst:wiremock:2.8.0")

    { exclude group: 'org.apache.httpcomponents', module: 'httpclient' exclude group: 'org.json', module: 'json' exclude group: 'org.ow2.asm', module: 'asm' } androidTestCompile 'org.apache.httpcomponents:httpclient-android:4.3.5.1' }
  31. @HandstandSam #DCNYC17 WireMock on Android - Project Config Recommended, but

    not required: android { defaultConfig { minSdkVersion 21 multiDexEnabled true }
 }
  32. @HandstandSam #DCNYC17 Recommendations • Play with WireMock as a desktop/server

    tool immediately. It’s awesome. • Evaluate other libraries. Don’t use WireMock in your app until you’ve looked at other options. • Mock Web Server - HTTP Server • Mock Web Server + RESTMock - More powerful • OkReplay - OkHttp Interceptor + File Based Recording • Retrofit - Behavior Delegate • Mockito • 70,000 methods??? - Multidex and get over it