<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MMock Archives - Blog IT</title>
	<atom:link href="https://blogit.create.pt/tag/mmock/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/tag/mmock/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Mon, 06 Jul 2020 15:43:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Mocking APIs with MMock</title>
		<link>https://blogit.create.pt/pedrodavid/2020/05/18/mocking-apis-with-mmock/</link>
					<comments>https://blogit.create.pt/pedrodavid/2020/05/18/mocking-apis-with-mmock/#respond</comments>
		
		<dc:creator><![CDATA[Pedro David]]></dc:creator>
		<pubDate>Mon, 18 May 2020 15:18:24 +0000</pubDate>
				<category><![CDATA[Integration]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[MMock]]></category>
		<category><![CDATA[Mock]]></category>
		<guid isPermaLink="false">https://blogit.create.pt/?p=11804</guid>

					<description><![CDATA[<p>There is a time during the development of a project when a developer needs to test the interaction with an external API service. Maybe he needs to get predefined responses to his API calls. In this case, the solution is not to use the original API, but instead to build a mock of said API. [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/pedrodavid/2020/05/18/mocking-apis-with-mmock/">Mocking APIs with MMock</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>There is a time during the development of a project when a developer needs to test the interaction with an external API service. Maybe he needs to get predefined responses to his API calls. In this case, the solution is not to use the original API, but instead to build a <span style="text-decoration: underline">mock</span> of said API.<br><br>There are many ways to mock http APIs, usually by writing a web server that listens to the same endpoints as the API being mocked. Although this can be simple enough to do on some programming languages, there is an even simpler way to achieve the same results without having to write a single line of code: By using <a rel="noreferrer noopener" href="https://github.com/jmartin82/mmock" target="_blank"><strong>MMock</strong></a>.<br><br><strong>MMock</strong> is a fast and simple mock http/https server which allows the definition of responses with a simple configuration file. It also has some handy features that I am going to describe shortly.<br><br>In this article I am going to use <a rel="noreferrer noopener" href="https://github.com/jmartin82/mmock/releases/tag/v2.7.9" target="_blank"><strong>MMock </strong>2.7.9</a>.</p>



<h2 class="wp-block-heading">Install and Run MMock</h2>



<p>There are two ways to install and run MMock:</p>



<p>1. As a docker container, either using the pre-built <a rel="noreferrer noopener" href="https://hub.docker.com/r/jordimartin/mmock" target="_blank">docker image</a> directly or extending it in your own Dockerfile, and then starting the container by running the command:</p>



<pre class="wp-block-preformatted">docker run -v &lt;PATH_TO_CONFIG_FOLDER&gt;:/config -p 8082:8082 -p 8083:8083 &lt;YOUR_IMAGE_NAME&gt;</pre>



<p>2. By running <strong>MMock</strong> directly on your machine. You need <strong>Go 1.8</strong> or higher installed, and the <strong>MMock</strong> module:</p>



<pre class="wp-block-preformatted">go get github.com/jmartin82/mmock/...
mmock -config-path &lt;PATH_TO_CONFIG_FOLDER&gt;</pre>



<p>You should see something like this when running <strong>MMock</strong>:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="889" height="376" src="https://blogit.create.pt////wp-content/uploads/2020/05/console.png" alt="" class="wp-image-11805" srcset="https://blogit.create.pt/wp-content/uploads/2020/05/console.png 889w, https://blogit.create.pt/wp-content/uploads/2020/05/console-300x127.png 300w, https://blogit.create.pt/wp-content/uploads/2020/05/console-768x325.png 768w, https://blogit.create.pt/wp-content/uploads/2020/05/console-696x294.png 696w" sizes="(max-width: 889px) 100vw, 889px" /></figure></div>



<p>The mock http server is accessible at <a rel="noreferrer noopener" href="http://localhost:8083/" target="_blank">http://localhost:8083/</a>, https at <a rel="noreferrer noopener" href="http://localhost:8084/" target="_blank">https</a><a rel="noreferrer noopener" href="https://localhost:8084/" target="_blank">://localhost:8084/</a>, and a handy <strong>MMock</strong> console, where you can track configurations and API calls, is accessible at <a rel="noreferrer noopener" href="http://localhost:8082/" target="_blank">http://localhost:8082/</a>.<br><br>Now, before you start sending requests to your mock API, firstly you need to configure <strong>MMock</strong>.</p>



<h2 class="wp-block-heading">Configuration Options</h2>



<div class="wp-block-jetpack-markdown"><p>To configure an endpoint on your mock API you must create a configuration file for <strong>MMock</strong>.</p>
<p>The configuration file can be written in <code>.json</code> or <code>.yaml</code>, and should be placed inside the <code>&lt;PATH_TO_CONFIG_FOLDER&gt;</code> specified in the <strong>MMock</strong> run command.</p>
<p>The following is an exhaustive list of all the options <strong>MMock</strong> recognizes:</p>
</div>



<div class="wp-block-jetpack-markdown"><ul>
<li><strong>description</strong>: A small description for the current configuration. This will show up in the <strong>MMock</strong> console and is useful if you have lots of configurations</li>
<li><strong>request</strong>: This group of options informs <strong>MMock</strong> about what kind of requests to expect. All options, except for <code>method</code>, allow global matching with <code>*</code>. All options must match for this configuration to be triggered
<ul>
<li><strong>method</strong>: http method (<code>GET</code>, <code>POST</code>, <code>PUT</code>, etc). Multiple methods can be separated by <code>|</code></li>
<li><strong>path</strong>: url endpoint/path, <code>http://localhost/8083/&lt;path&gt;</code></li>
<li><strong>body</strong>: request body</li>
<li><strong>queryStringParameters</strong>: array of url query string parameters, <code>http://localhost/8083/endpoint?key=value</code></li>
<li><strong>headers</strong>: array of http request headers</li>
<li><strong>cookies</strong>: array of request cookies</li>
<li><strong>host</strong>: host from where the request should originate</li>
</ul>
</li>
<li><strong>response</strong>: This group of options specifies the response that <strong>MMock</strong> should return
<ul>
<li><strong>statusCode</strong>: http status code (<code>200</code>, <code>404</code>, etc)</li>
<li><strong>body</strong>: response body</li>
<li><strong>headers</strong>: array of http response headers</li>
<li><strong>cookies</strong>: array of http response cookies</li>
</ul>
</li>
<li><strong>callback</strong>: This group of options informs <strong>MMock</strong> that it should perform a request to an external API (will be introduced in a future release)
<ul>
<li><strong>method</strong>: http method</li>
<li><strong>url</strong>: API url to call</li>
<li><strong>delay</strong>: delay before sending request</li>
<li><strong>timeout</strong>: time to wait for response</li>
<li><strong>headers</strong>: array of headers</li>
<li><strong>body</strong>: http body</li>
</ul>
</li>
<li><strong>control</strong>: This group of options specifies some mechanisms that control some extra behavior of <strong>MMock</strong>
<ul>
<li><strong>pripority</strong>: specifies a value that defines the priority of this configuration over others</li>
<li><strong>proxyBaseURL</strong>: url to where <strong>MMock</strong> should send the received request. <strong>MMock</strong> then returns the response back</li>
<li><strong>delay</strong>: how long <strong>MMock</strong> should wait before responding</li>
<li><strong>crazy</strong>: randomly returns an http <code>500</code> error or <code>200</code> OK</li>
<li><strong>scenario</strong>: This option allows <strong>MMock</strong> to retain some state
<ul>
<li><strong>name</strong>: arbitrary scenario name</li>
<li><strong>requiredState</strong>: the required state of the scenario for this configuration to take effect</li>
<li><strong>newState</strong>: the new state of the scenario after this configuration takes effect</li>
</ul>
</li>
<li><strong>webHookURL</strong>: url that will receive a webhook from <strong>MMock</strong></li>
</ul>
</li>
</ul>
<p>The only really mandatory configuration options are the <code>request.method</code> and <code>request.path</code>, although a configuration file with just those two options does nothing.</p>
</div>



<h2 class="wp-block-heading">Example Configurations</h2>



<div class="wp-block-jetpack-markdown"><p>Now some examples of what I think are relevant configurations, starting with a simple one that expects a request to <code>/hello</code> and returns a response with http code <code>200</code>:</p>
</div>



<pre class="wp-block-code"><code>description: hello
request:
  method: GET
  path: "/hello"
response:
  statusCode: 200
  body: Hello!</code></pre>



<div class="wp-block-jetpack-markdown"><p>With a configuration file as simple as this we have an endpoint which will always return a <code>200</code> OK response, no matter the request’s http body, headers, or cookies!</p>
</div>



<p>Now an example of a configuration that expects some query string parameters:</p>



<pre class="wp-block-code"><code>request:
  method: GET
  path: "/hello"
  queryStringParameters:
    user:
      - *
response:
  statusCode: 200
  body: Hello {{request.query.user}}!</code></pre>



<div class="wp-block-jetpack-markdown"><p>For this configuration to trigger, the mock API must be accessed with the <code>user</code> query parameter, like so: <code>/hello?user=john</code>. In this case the response will include the query parameter value! As you might have noticed, the curly braces in the body option define variables, and in this configuration that variable is the value defined in the <code>user</code> query parameter.</p>
<p>The same kind of configuration is applied to headers and cookies, although for cookies only one value can be defined per cookie.</p>
<p>A more complex example involving multiple configurations which define states based on the requests performed:</p>
</div>



<pre class="wp-block-code"><code>request:
  method: GET
  path: "/user/:name"
response:
  statusCode: 200
  headers:
    Content-Type:
    - application/json
  body: >
    {
      "name": "{{request.path.name}}",
      "email": "{{fake.EmailAddress}}",
      "address": {
        "street": "{{fake.StreetAddress}}",
        "city": "{{fake.City}}",
        "zipcode": "{{fake.Zip}}"
      }
    }
control:
  scenario:
    name: user
    requiredState:
    - created</code></pre>



<pre class="wp-block-code"><code>request:
  method: POST
  path: "/user/:name"
response:
  statusCode: 202
control:
  scenario:
    name: user
    requiredState:
    - not_started
    - deleted
    newState: created</code></pre>



<pre class="wp-block-code"><code>request:
  method: DELETE
  path: "/user/:name"
response:
  statusCode: 200
control:
  scenario:
    name: user
    requiredState:
    - created
    newState: deleted</code></pre>



<div class="wp-block-jetpack-markdown"><p>These three configurations emulate the creation, reading and deletion of a user.</p>
<p>The first configuration handles requesting data on a user by performing a <code>GET</code> request to <code>/scenario/&lt;user_name&gt;</code> (notice the <code>:name</code> variable in the <code>path</code> option). It returns mock user information as json if the scenario is in the <code>created</code> state, otherwise returns a <code>404</code> http error code.</p>
<p>Next, by performing a <code>POST</code> request to the same endpoint the state of the scenario will be set to <code>created</code>, which will enable us the perform a <code>GET</code> request and return the user’s data.</p>
<p>Finally, by performing a <code>DELETE</code> request to the same endpoint the state will be set to <code>deleted</code>, which will, once again, cause the <code>GET</code> request to return a <code>404</code> http error code.</p>
</div>



<h2 class="wp-block-heading">Variables and Fake Data</h2>



<div class="wp-block-jetpack-markdown"><p>As you were able to see in the examples above, <strong>MMock</strong> defines some variables you can use, enclosed in curly braces, like so: <code>{{variable}}</code>.</p>
<p>Most of the request information can be accessed through variables in the response, such as the path, query parameters, cookies, body (respectively: <code>request.path</code>, <code>request.query.&lt;parameter&gt;</code>, <code>request.cookie.&lt;cookie&gt;</code>, <code>request.body</code>), and more.</p>
<p>You can also return the content of files or urls in your response with the variables <code>file.contents(&lt;PATH_TO_FILE&gt;)</code> or <code>http.contents(&lt;URL&gt;)</code>.</p>
<p>Lastly, <strong>MMock</strong> defines some variables that return mock data, such as brands, colors, countries, days, names, ips, phones (respectively, <code>fake.Brand</code>, <code>fake.Color</code>, <code>fake.Country</code>, <code>fake.Day</code>, <code>fake.FullName</code>, <code>fake.IPv4</code>, <code>fake.Phone</code>), and much more!</p>
</div>



<h2 class="wp-block-heading">Conclusion</h2>



<p><strong>MMock </strong>can be an easy way to define simple mock APIs. The configuration options allows a wide range of behaviors which target the more usual API testing scenarios.<br><br>The project is under active development, so expect changes and fixes once in a while!</p>



<p></p>
<p>The post <a href="https://blogit.create.pt/pedrodavid/2020/05/18/mocking-apis-with-mmock/">Mocking APIs with MMock</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/pedrodavid/2020/05/18/mocking-apis-with-mmock/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
