
## Wiremock
### Table of Contents
* What is wiremock?
* Why we need it?
* How to use?
* Features
* Demo
### What is wiremock?
Dynamic stubbing and proxy system for HTTP services (json, xml, HTML, SOAP etc)
### Why we need it?
* To test an application without depended applications
* To test extreme cases at remote applications (like internal server errors, too slow services etc)
* Forwarding requests to another container/server.
* Reproducing user interactions.
### How to use? (1/2)
_Standalone Usage (1/2)_
```
java -jar wiremock-standalone.jar \
--port=80 \
--https-port=443 \
--no-request-journal \
--verbose \
--global-response-templating \
--preserve-host-header
```
### How to use? (1/2)
_Standalone Mapping Format (2/2)_
```
{
"request": {
"method": "GET",
"url": "/some/thing"
},
"response": {
"status": 200,
"body": "Hello world!",
"headers": {
"Content-Type": "text/plain"
}
}
}
```
### How to use? (2/2)
_As a library_
```
@Rule
public WireMockRule service1 = new WireMockRule(8081);
```
_Mapping Format_
```
stubFor(get(urlEqualTo("/some/thing"))
.willReturn(aResponse()
.withHeader("Content-Type", "text/plain")
.withBody("Hello world!")));
```
### Features (1/6)
Stubbing
```
{
"request": {
"method": "GET",
"url": "/some/thing"
},
"response": {
"status": 200,
"body": "Hello world!",
"headers": {
"Content-Type": "text/plain"
}
}
}
```
### Features (2/6)
Request matching
```
{
"request": {
"urlPattern": "/your/([a-z]*)\\?and=query"
...
},
...
}
```
### Features (3/6)
Request Templating
```
{
"request": {
"urlPath": "/templated"
},
"response": {
"body": "{{request.path.[0]}}",
"transformers": ["response-template"]
}
}
```
```
request.url - URL path and query
request.path - URL path
request.path.[<n>]- URL path segment (zero indexed) e.g. request.path.[2]
request.query.<key>- First value of a query parameter e.g. request.query.search
request.query.<key>.[<n>]- nth value of a query parameter (zero indexed) e.g. request.query.search.[5]
...
```
### Features (4/6)
Verifying

### Features (5/6)
Proxying

### Features (6/6)
Recording

### Demo

###### Demo sources: [wiremock-case-study/wiremock-demo](https://github.com/veysiertekin/wiremock-case-study/tree/master/wiremock-demo)
###### Verify Java 8 Version
```
➜ ./mvnw -version
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T10:58:13+03:00)
Maven home: /Users/<user>/.m2/wrapper/dists/apache-maven-3.5.2-bin/28qa8v9e2mq69covern8vmdkj0/apache-maven-3.5.2
Java version: 1.8.0_152, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home/jre
Default locale: en_TR, platform encoding: UTF-8
OS name: "mac os x", version: "10.13.3", arch: "x86_64", family: "mac"
```
###### Is Docker Daemon Running?
```
➜ docker -v
Docker version 17.09.0-ce, build afdb6d4
➜ docker-compose -v
docker-compose version 1.16.1, build 6d1ac21
```
##### Building applications
```
➜ ./mvnw clean verify
```
##### Starting applications
```bash
➜ docker-compose -p wiremock-demo -f dc-wiremock-demo.yml up -d
```