Skip to content

acnalesso/optimus_prime

Repository files navigation

OptimusPrime

####“It’s been an honor serving with you all.”

Installation

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.

Default configuration

  • 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

HTTP allowed requests

  • GET
  • POST
  • PUT

Usage

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)

GET requests:

op.prime("users", " response... ", status_code: 200)
response = Faraday.get("http://localhost:7002/get/users")
response.body #=> " response... "

POST requests - creating data:

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 }

PUT requests - editing data:

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 }

Changing Content Type:

op.prime("users", { users json... }, { content_type: :json })
response = Faraday.get("http://localhost:7002/get/users")
response.headers["content-type"] #=> "application/json"

Changing HTTP response method:

op.prime("users", " response... ", { status_code: 404 })
response = Faraday.get("http://localhost:7002/get/users")
response.status #=> 404

Assert on a POST request body

op.prime("users", " response... ", requested_with: "a body")
response = Faraday.post("http://localhost:7002/get/users", "I am a body")
response.status #=> 200

Simulating server processing or timeouts

op.prime("users", " response... ", sleep: 10)
response = Faraday.get("http://localhost:7002/get/users")
# .... 10 seconds later ...
response.status #=> 200

Requests not primed

  ::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"
}

Counting the number of requests made for an endpoint (GET and POST)

  op.prime('your/endpoint')
  Faraday.get("http://localhost:7002/get/your/endpoint")
  op.count('your/endpoint') #=> 1

Get the requests made to an endpoint

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: [] } ]

Waiting for requests

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

Clear all requests

  op.clear

TODO

  • 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

Contributing

  1. Fork it ( https://github.com/[my-github-username]/optimus_prime/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •