Skip to content

A fake Pusher server for development and testing.

License

Notifications You must be signed in to change notification settings

wordset/pusher-fake

 
 

Repository files navigation

pusher-fake Build Status Dependency Status Code Climate Coverage Status

A fake Pusher server for development and testing. When you run your code, we start up an entire fake service, on a random port that we find open. You can then connect to that service and run your development environment without needing an actual key. To find out where the service is running, check the logs for the startup messages.

The project is intended to fully replace the Pusher service with a local version for testing, and can also be used for development purposes. It is not intended to be a replacement for production usage! If you try, bad things might happen to you.

Usage

Test Environment

1. Use the PusherFake JS for the Pusher JS instance.

<script>
  <% if defined?(PusherFake) %>
    // Test environment.
    //
    // Note: Ensure output is not HTML escaped, such as with the raw helper in Rails.
    var instance = <%= PusherFake.javascript %>;
  <% else %>
    // Other environments, such as production.
    var instance = new Pusher(...);
  <% end %>
</script>

2. Start PusherFake in your environment.

RSpec
require "pusher-fake/support/rspec"
Cucumber
require "pusher-fake/support/cucumber"
Other
require "pusher-fake/support/base"

# Reset the channels after each test:
PusherFake::Channel.reset

Development Environment

In a Rails initializer, or any file executed during loading do the following. Please note that requiring that file immediately starts up the websocket server.

if Rails.env == "development"
  Pusher.app_id = "testapp"
  Pusher.key = "testkey"
  Pusher.secret = "super secret"
  require "pusher-fake/support/base"
end

If you're using Foreman or something similar you'll only want to run the fake for a single process:

if ENV["PUSHER_FAKE"]
  require "pusher-fake/support/base"
end
web: PUSHER_FAKE=1 bundle exec unicorn ...
worker: bundle exec ...

Clients

If you're creating a Pusher::Client instance and wish to use the fake, you need to provide the options.

Pusher::Client.new({
  key:    Pusher.key,
  app_id: Pusher.app_id,
  secret: Pusher.secret
}.merge(PusherFake.configuration.web_options))

Configuration

Note that the application ID, API key, and token are automatically set to the Pusher values when using an included support file.

Settings

Setting Description
app_id The Pusher application ID.
key The Pusher API key.
logger An IO instance for verbose logging.
secret The Pusher API token.
socket_options Socket server options. See EventMachine::WebSocket.start for options.
verbose Enable verbose logging.
web_options Web server options. See Thin::Server for options.
webhooks Array of webhook URLs.

Usage

# Single setting.
PusherFake.configuration.verbose = true

# Multiple settings.
PusherFake.configure do |configuration|
  configuration.logger  = Rails.logger
  configuration.verbose = true
end

Examples

  • pusher-fake-example - An example of using pusher-fake with RSpec to test a Rails application.

Goal

Fully recreate the Pusher API service for development and testing.

Why?

  • Using a remote API for testing is slow.
  • Working offline is currently impossible.
  • Wasting connections and messages in development is unreasonable.
  • Stubbing the JavaScript, such as with pusher-test-stub, is suboptimal and tedious for integration tests.

License

pusher-fake uses the MIT license. See LICENSE for more details.

About

A fake Pusher server for development and testing.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 97.8%
  • HTML 2.2%