####“It’s been an honor serving with you all.”
Add this line to your application's Gemfile:
gem 'optimus_prime'
And then execute:
$ bundle
Or install it yourself as:
$ gem install optimus_prime
OptimusPrime allows developers to persist fake date and tell their API to talk to it and get the desired response.
- localhost:7003 -> TEST default endpoint
- returns 200 status code for GET,POST
- Runs on a single thread
- If multi_threaded is true the default thread pool size is 16
- sets content-type to text
- wait_for is 3 seconds
- Returns empty body when status_code is 404/500
- GET
- POST
- PUT
OptimusPrime::Cannon.fire!(7002)
op = OptimusPrime::Base.new
op.prime("path_name", response, options)
multi threaded mode
#
# OptimusPrime::Cannon.fire!(7002, {multi_threaded: true, thread_pool: 50})
# thread_pool is by default set to 16
#
OptimusPrime::Cannon.fire!(7002, {multi_threaded: true})
op = OptimusPrime::Base.new
op.prime("path_name", response, options)
changing port number:
OptimusPrime::Cannon.fire!(2020)
op = OptimusPrime::Base.new
op.prime("path_name", response, options)
op.prime("users", " response... ", status_code: 200)
response = Faraday.get("http://localhost:7002/get/users")
response.body #=> " response... "
op.prime("users/1", {}, status_code: 201)
response = Faraday.post("http://localhost:7002/get/users/1", { created: true }.to_json)
response.status #=> 201 - Created
response.body #=> { created: true }
op.prime("users/1", { age: 21 }, status_code: 201, persited: true)
response = Faraday.post("http://localhost:7002/get/users/1", { updated_at: "2013" }.to_json)
response.status #=> 201 - Created
Faraday.get("http://localhost:7002/get/users/1").body #=> { age: 21, updated_at: true }
op.prime("users", { users json... }, { content_type: :json })
response = Faraday.get("http://localhost:7002/get/users")
response.headers["content-type"] #=> "application/json"
op.prime("users", " response... ", { status_code: 404 })
response = Faraday.get("http://localhost:7002/get/users")
response.status #=> 404
op.prime("users", " response... ", requested_with: "a body")
response = Faraday.post("http://localhost:7002/get/users", "I am a body")
response.status #=> 200
op.prime("users", " response... ", sleep: 10)
response = Faraday.get("http://localhost:7002/get/users")
# .... 10 seconds later ...
response.status #=> 200
::Faraday.get("http://localhost:7002/get/i-am-not-primed")
Now go to http://localhost:7002/not-primed
get/i-am-not-primed: {
time: "2014-09-07 18:50:57 +0100",
HTTP_METHOD: "GET"
}
op.prime('your/endpoint')
Faraday.get("http://localhost:7002/get/your/endpoint")
op.count('your/endpoint') #=> 1
You will have to pass how long to wait_for, as requests might be in process that is why we need to wait for it.
By default OptimusPrime sets wait_for
flag to 3 seconds.
op = OptimusPrime.new(wait_for: 5)
# GET Requests
op.prime('your/endpoint')
Faraday.get("http://localhost:7002/get/your/endpoint")
op.last_request_for('your/endpoint') #=> ["method" => "GET", "body" => ""]
# POST Requests
op.prime('your/endpoint')
Faraday.post("http://localhost:7002/get/your/endpoint", {some: "data"})
op.last_request_for('your/endpoint') #=> ["method" => "GET", "body" => "some=data", "headers" => { "content_type": "", accept: [] } ]
Sometimes your ajax requests may take a while to fire when running on Continuous Integration Platforms like cicleCI for example. Optimus Prime will return requests made within a given time for you to set your own expectations.
op = OptimusPrime.new(wait_for: 3)
op.wait_until_request("path_name") do |request|
expect( request["body"] ).to include("CALLED")
end
op.clear
- Move server initialisation into rake task in order to prevent it from initialising from its directory.
- Support DELETE, HEAD http methods
- Support REGEX as a path
- Support html templates
- Fork it ( https://github.com/[my-github-username]/optimus_prime/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request