{"id":1870,"date":"2016-01-06T15:00:21","date_gmt":"2016-01-06T15:00:21","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=1870"},"modified":"2024-08-28T12:07:09","modified_gmt":"2024-08-28T12:07:09","slug":"how-to-mock-endpoints-in-automated-acceptance-tests","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/how-to-mock-endpoints-in-automated-acceptance-tests\/","title":{"rendered":"How To Mock Endpoints in Automated Acceptance Tests"},"content":{"rendered":"<p>The purpose of acceptance testing is to reproduce production environment as much as possible in order to evaluate the software in a &#8216;real-world&#8217; situation similar to what the customers will experience and this includes using real endpoints.<\/p>\n<p>But using real endpoints has some disadvantages. In automated acceptance tests, the endpoint will be stressed out and this is not performance testing. Also, you must use production data (even usernames and passwords) and this is not good because you might break something there. The list with the reasons could go on and on and this is why you should mock some endpoints in automated acceptance tests. Also, you should keep your endpoint&#8217;s URL stored in environment variables to be able to change it easily (production URL or mock URL).<\/p>\n<p>I will show you some basic examples of mocking endpoints written in Ruby, Python and GO.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_68_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/intelligentbee.com\/blog\/how-to-mock-endpoints-in-automated-acceptance-tests\/#Mock_endpoint_in_Ruby\" title=\"Mock endpoint in Ruby\">Mock endpoint in Ruby<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/intelligentbee.com\/blog\/how-to-mock-endpoints-in-automated-acceptance-tests\/#Mock_endpoint_in_Python\" title=\"Mock endpoint in Python\">Mock endpoint in Python<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Mock_endpoint_in_Ruby\"><\/span><strong><span style=\"font-weight: 400;\">Mock endpoint in Ruby<\/span><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">I have used Sinatra (DSL<\/span><span style=\"font-weight: 400;\"> for quickly creating web applications in Ruby) which is very easy to use.<\/span><\/p>\n<p>Install the gem:<\/p>\n<p><code>gem install sinatra<\/code><\/p>\n<pre class=\"lang:ruby decode:true\" title=\"myapp.rb\">#myapp.rb\r\n\r\nrequire \u2018json\u2019\r\nrequire \u2018sinatra\u2019\r\n\r\nget '\/:username' do\r\n  if params[:username] != 'your_username'\r\n  \tstatus 404\r\n  else\r\n  \tcontent_type :json\r\n  \t{'message' =&gt; 'success'}.to_json\r\n  end\r\nend<\/pre>\n<p>&nbsp;<\/p>\n<p><code>ruby myapp.rb -o 0.0.0.0 -p 5000\u00a0&gt; \/dev\/null 2&gt;&amp;1 &amp; <\/code><\/p>\n<p>This will start the process in background and your endpoint on <strong>http:\/\/localhost:5000<\/strong>.<\/p>\n<p>If you make a GET\u00a0request on <strong>http:\/\/localhost:5000\/your_username<\/strong> you will get a \u2018success\u2019 message, else a 404 status code.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Mock_endpoint_in_Python\"><\/span>Mock endpoint in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In Python it is very easy to create an endpoint with Flask.<\/p>\n<p>To install it you have to run:<\/p>\n<p><code>pip install flask-restful<\/code><\/p>\n<pre class=\"lang:python decode:true\" title=\"myapp.py\">#myapp.py\r\n\r\n#!flask\/bin\/python\r\nfrom flask import Flask, jsonify\r\nfrom flask import Response\r\nimport json\r\n\r\napp = Flask(__name__)\r\n\r\n@app.route(\"\/&lt;username&gt;\", methods=['GET'])\r\ndef put_username(username):\r\n    if username == 'your_username':\r\n        resp = Response(\"success!\\n\", mimetype='application\/json')\r\n    else:\r\n        resp = Response(\"\", status=404, mimetype='application\/json')\r\n\r\n    return resp\r\n\r\nif __name__ == '__main__':\r\n    app.run(debug=True)<\/pre>\n<p>&nbsp;<\/p>\n<p>As you can see, this does the exact same thing as the endpoint created in Ruby.<\/p>\n<p>You simply run it with<\/p>\n<p><code>python myapp.py<\/code><\/p>\n<h3>Mock endpoint in GO<\/h3>\n<pre class=\"lang:default decode:true \" title=\"myapp.go\">\/\/myapp.go\r\npackage main\r\n\r\nimport (\r\n\t\"fmt\"\r\n\t\"net\/http\"\r\n\t\"github.com\/julienschmidt\/httprouter\"\r\n)\r\n\r\nfunc username(w http.ResponseWriter, r *http.Request, p httprouter.Params) {\r\n\tif p.ByName(\"username\") == \"your_username\" {\r\n\t\tw.Header().Set(\"Content-Type\", \"application\/json\")\r\n\t\tw.WriteHeader(200)\r\n\t\tfmt.Fprint(w, `{\"message\":\"success\"}`)\r\n\t} else {\r\n\t\tw.WriteHeader(404)\r\n\t}\r\n}\r\n\r\nfunc main() {\r\n\tr := httprouter.New()\r\n\tr.GET(\"\/:username\", username)\r\n\thttp.ListenAndServe(\"localhost:5000\", r)\r\n}<\/pre>\n<p>This does the same thing as the previous two endpoints and you run it with:<\/p>\n<p><code>go run myapp.go<\/code><\/p>\n<p>Thanks for reading this. What other ways of mocking an endpoint did you find?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The purpose of acceptance testing is to reproduce production environment as much as possible in order to evaluate the software [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":1885,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,77,84],"tags":[87,95,146,171,201,202,231],"yst_prominent_words":[530,531,533,639,672,970,1052,1063],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/1870"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=1870"}],"version-history":[{"count":2,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/1870\/revisions"}],"predecessor-version":[{"id":133188,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/1870\/revisions\/133188"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media\/1885"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=1870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=1870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=1870"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=1870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}