# Mock Server
https://www.mock-server.com/
---
## What is MockServer
- A **mock** configured to return specific responses for different requests
- A **proxy** recording and optionally modifying requests and responses
- Both a **proxy** for some requests and a **mock** for other requests at the same time
---
## Steps for each request received
1. Find matching expectation and perform action
1. If no matching expectation proxy request
1. If not a proxy request return 404
An **expectation** defines the **action** that is taken, for example, a response could be returned.
---
## MockServer actions
- return a "mock" response
- forward a request
- execute a callback
- return an invalid response
- verify requests
- retrieve logs, requests or expectations
---
### Action: Return a "mock" response
Return a "mock" response when a request matches an expectation

---
### Action: Forward a request
Forward a request when the request matches an expectation (i.e. a dynamic port forwarding proxy)

---
### Action: Execute a callback
Execute a callback when a request matches an expectation, allowing the response to be created dynamically

---
### Action: Return an invalid response
Return an invalid response or close the connection when a request matches an expectation

---
### Action: Verify requests
Verify requests have been sent (i.e. as a test assertion)

---
### Action: Retrieve data
Retrieve logs, requests or expectations to help debug

---
## Proxying with MockServer
- proxy all requests
- Port Forwarding
- Web Proxying (i.e. HTTP proxy)
- HTTPS Tunneling Proxying
- SOCKS Proxying
- verify proxied requests have been sent (i.e. in a test assertion)
- record proxied requests and responses to analyse how a system behaves
---
## Why use MockServer
- de-coupling development
- testing
- isolate single service

<!--
---
### Why use MockServer as a proxy
- testing
- analyse existing system
- debug HTTP interactions
- record & replay
-->
---
## Running MockServer
[<<skip>>](https://www.mock-server.com/mock_server/running_mock_server.html)
```
docker run -d --rm -p 5566:5566 --env MOCKSERVER_SERVER_PORT=5566 mockserver/mockserver
```
---
## MockServer Clients
- REST API
- [document](https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/5.15.x)
- Java
- JavaScript
- browser API: [mockServerClient.js](https://raw.githubusercontent.com/mock-server/mockserver-client-node/mockserver-5.14.0/mockServerClient.js)
- Node.js module [mockserver-client](https://www.npmjs.org/package/mockserver-client)
---
## Notice
- All data is only stored in memory, please backup your **expectations**
- The certificate signed by ASUS cannot be applied, and further research is needed
<!--
---
## Rest API
https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/5.15.x
- Method: PUT
- URL: {domain}/mockserver/{action}
- Body: [request matcher](https://www.mock-server.com/mock_server/creating_expectations.html#request_matchers)
-->
---
## Example
- login with correct/wrong password
- correct: return token and set cookie session
- wrong: delay 5s, return 401 and error msg
- verify call count and sequence
- Proxy to backend
- import OpenAPI
- export/import expectations
---
## Precautions
- id: [updating expectation](
https://www.mock-server.com/mock_server/creating_expectations.html#updating_expectations)
- priority: [matching order](
https://www.mock-server.com/mock_server/creating_expectations.html#matching_order)
- matcher: [request property matchers](
https://www.mock-server.com/mock_server/creating_expectations.html#request_property_matchers)
- [curl_examples](https://github.com/mock-server/mockserver/blob/master/mockserver-examples/curl_examples.md)
---
### Links
- [Mock-server Offical Site](https://www.mock-server.com/)
- [Mock-server GitHub Repository](https://github.com/mock-server/mockserver)
- [Postman examples](https://api.postman.com/collections/5728714-355ed08c-6464-4107-a390-049d0fb73d57?access_key=PMAT-01H0S8PEQH69D2C44P3X801MQ0)
* Our mockserver site: http://bpza003kjc.corpnet.asus:8078/mockserver/dashboard
* Test site: http://bpza003kjc.corpnet.asus:8077/mockserver/dashboard
{"metaMigratedAt":"2023-06-18T04:36:22.718Z","metaMigratedFrom":"YAML","title":"Mock Server","breaks":true,"slideOptions":"{\"theme\":\"moon\",\"transition\":\"fade\"}","contributors":"[{\"id\":\"49ca1989-d3a7-4146-8083-db0b8adfe60e\",\"add\":5878,\"del\":1312},{\"id\":\"d4b9be57-f752-49a3-99d7-8a7df97f32bd\",\"add\":366,\"del\":153}]"}