Package: bakedbeans
Baked Beans is a terribly named HTTP stub server for REST services that deal in JSON payloads written in Python but easily runnable as a Docker container.It takes a super simple approach of using a folder of contents that provide the canned responses it produces.e.gGEThttp://localhost:3000/path/to/some/resourcewould map to the file:<base_folder>/path/to/some/resource.get.json.Note in the example above that the file name looked for includes the HTTP method used.In it’s basic mode, Baked Beans will simply load the matching file and pipe it back to the client.It also supports a limited amount of smarts. JSON file constructed to a specific schema can be used by Baked Beans to return specific responses. For example, it can match responses based on GET parameters.InstallationCreate a python virtual environment with your favourite tools. Then use pip to installbakedbeans:(venv)$pipinstallbakedbeansRunningBaked Beans provides a simple command line executable to run the server:(venv)$baked/path/to/contents/folder*Runningonhttp://127.0.0.1:3000/(PressCTRL+Ctoquit)You can specify the host name with--hostand the port it binds to with--port.Baked Beans is also built with Docker in mind. To run Baked Beans in docker you will need to mount your local contents directory as a volume.  For example:dockerrun-it--rm-p3000:3000-v/full/path/to/contents:/contentsmoose/bakedbeansThis will run Baked Beans in a container that will remove itself once exit, mounting the/full/path/to/contentsinside the container at/contentswhich is where Baked Beans is configured to load the contents from.Writing contentsBaked Beans maps URLs to file paths, adding.<method>.jsonto the end of the path. There are a couple of exceptions, for example if the URL ends with/it will instead addindex.<method>.json.Examples:Request URLMapped PathGEThttp://localhost:3000/somewhere<base>/somewhere.get.jsonPOSThttp://localhost:3000/another<base>/another.post.jsonGEThttp://localhost:3000/<base>/index.get.jsonBy default the contents of the file once loaded to feed back to the request verbatim.However, if you want to get clever with the response you can write the JSON file in thebeanformat.Beansare just something I made up for this project and take the form of a JSON object. For example:{"_bean":true,"responses":[{"params":{"product":"1111"},"contents":{"id":1111,"name":"Product A"}},{"params":{"product":"2222"},"status":203,"contents":{"id":2222,"name":"Product B"}}]}With thebeanabove, it is matching the response based on the GET parameters that accompany the request.  e.g.http://localhost:3000/path?product=1111would match the first response and return the value ofcontents. A GET param ofproduct=2222would match the second. If no match is found, then the first one is selected regardless.Note also that the second response specifies astatus, this allow you to specify a specific status code to use for the response. If not specified then a default value based on the HTTP method is used.The default status codes are:MethodCodeGET200POST201DELETE204PUT200PATCH200<OTHER>200Currently only GET parameter matching is supported but header and body matching would be a nice addition as would regex of values and use of matched criteria within the content…Hits & MissesThe above describes have things work if everything is perfect. The url matches a content file, the content file is valid JSON, and if needed a validbean.  But what happens when things don’t match up:Content not found404status with a body of{"error": "content not found the/missing/path"}Invalid content file500status with a body of{"error": "content invalid"}Bean fails validation500status with a body of{"error": "This is one mouldy bean"}More detailed descriptions of the error encountered can be found in the logs.