{"id":1515,"date":"2024-09-14T12:58:26","date_gmt":"2024-09-14T04:58:26","guid":{"rendered":"http:\/\/www.leyiwangyou.top\/?p=1515"},"modified":"2024-09-14T13:12:25","modified_gmt":"2024-09-14T05:12:25","slug":"docker-install-mockserver-to-mock-response","status":"publish","type":"post","link":"http:\/\/www.leyiwangyou.top\/?p=1515","title":{"rendered":"Docker Install MockServer to mock response"},"content":{"rendered":"<h2>What is MockServer<\/h2>\n<p>Official documentation: <a href=\"https:\/\/www.mock-server.com\/\">https:\/\/www.mock-server.com\/<\/a><\/p>\n<p>For any system you integrate with via HTTP or HTTPS MockServer can be used as:<\/p>\n<ul>\n<li>a mock configured to return specific responses for different requests<\/li>\n<li>a proxy recording and optionally modifying requests and responses<\/li>\n<li>both a proxy for some requests and a mock for other requests at the same time<\/li>\n<\/ul>\n<p>When MockServer receives a request it matches the request against active expectations that have been configured. Then, if no matches are found, it proxies the request if appropriate; otherwise a 404 is returned.<\/p>\n<p>return a &quot;mock&quot; response when a request matches an expectation<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.leyiwangyou.top\/wp-content\/uploads\/2024\/09\/expectation_response_action-300x164.png\" alt=\"\" \/><\/p>\n<h2>Use Docker Compose to start<\/h2>\n<ol>\n<li>\n<p>create a new folder then define docker-compose.yml file<\/p>\n<pre><code class=\"language-version:\">    services:\n     mockServer:\n        image: mockserver\/mockserver:latest\n        container_name: mockServer\n        ports:\n          - 1080:1080\n        environment:\n          MOCKSERVER_WATCH_INITIALIZATION_JSON: true\n          MOCKSERVER_LOG_LEVEL: DEBUG\n          MOCKSERVER_PROPERTY_FILE: \/config\/mockserver.properties\n          MOCKSERVER_INITIALIZATION_JSON_PATH: \/config\/initializerJson.json\n          TZ: Asia\/Shanghai\n        volumes:\n          - .\/config:\/config <\/code><\/pre>\n<\/li>\n<li>\n<p>create the initializerJson.json file<\/p>\n<pre><code class=\"language-bash\">[\n    {\n      \"httpRequest\" : {\n        \"method\" : \"GET\",\n        \"path\" : \"\/api\/v1\/hello\"\n    },\n      \"httpResponse\" : {\n        \"body\" : \"Hello World !\",\n        \"statusCode\": 200\n      }\n    }\n]<\/code><\/pre>\n<\/li>\n<li>\n<p>docker compose start<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.leyiwangyou.top\/wp-content\/uploads\/2024\/09\/Snipaste_2024-09-13_11-03-35.png\" alt=\"\" \/><\/p>\n<\/li>\n<li>\n<p>test the result<br \/>\n<img decoding=\"async\" src=\"http:\/\/www.leyiwangyou.top\/wp-content\/uploads\/2024\/09\/Snipaste_2024-09-13_17-39-29.png\" alt=\"\" \/><\/p>\n<\/li>\n<li>\n<p>MockServer UI<br \/>\nBrowser<br \/>\nThe UI can be opened in any browser using the URL<br \/>\nhttp(s):\/\/<host>:<port>\/mockserver\/dashboard<br \/>\nFor example if MockServer is running on localhost on port 1080 use<br \/>\n<a href=\"http:\/\/localhost:1080\/mockserver\/dashboard\">http:\/\/localhost:1080\/mockserver\/dashboard<\/a><br \/>\n<img decoding=\"async\" src=\"http:\/\/www.leyiwangyou.top\/wp-content\/uploads\/2024\/09\/Snipaste_2024-09-14_13-08-47.png\" alt=\"\" \/><\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>What is MockServer O<\/p>\n","protected":false},"author":1,"featured_media":1516,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[384],"tags":[383],"class_list":["post-1515","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker","tag-docker"],"_links":{"self":[{"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/posts\/1515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1515"}],"version-history":[{"count":4,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/posts\/1515\/revisions"}],"predecessor-version":[{"id":1521,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/posts\/1515\/revisions\/1521"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=\/wp\/v2\/media\/1516"}],"wp:attachment":[{"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1515"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.leyiwangyou.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}