pusher-fake Latest Version Build Status Dependency Status Code Climate Coverage Status

A fake Pusher server for development and testing.

When run, an entire fake service is started on two random open ports. Connections can then be made to the service without needing a Pusher account. The host and port for the socket and web servers can be found by checking the configuration.

The project is intended to fully replace the Pusher service with a local version for testing and development purposes. It is not intended to be a replacement for production usage!

Why?

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

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"
Zeus

A custom plan is required. See an example plan for the configuration necessary.

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:

# Ensure it's only run in a development environment if it's a global file.
if Rails.env.development?
  # Ensure Pusher configuration is set if you're not doing so elsewhere.
  Pusher.app_id = "MY_TEST_ID"
  Pusher.key    = "MY_TEST_KEY"
  Pusher.secret = "MY_TEST_SECRET"

  # Require the base file, which immediately starts the socket and web servers.
  #
  # If you're including this file in multiple processes, you may want to add an
  # additional check or even possibly hard code the socket and web ports.
  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))

Binary

If you need to run the fake as a standalone service, perhaps when using Docker, there is a pusher-fake binary available.

$ pusher-fake --help
Usage: pusher-fake [options]
    -i, --id ID                      Use ID as the application ID for Pusher
    -k, --key KEY                    Use KEY as the key for Pusher
    -s, --secret SECRET              Use SECRET as the secret token for Pusher
        --socket-host HOST           Use HOST for the web socket server
        --socket-port PORT           Use PORT for the web socket server
    -v, --[no-]verbose               Run verbosely
        --web-host HOST              Use HOST for the web server
        --web-port PORT              Use PORT for the web server

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

SSL

The WebSocket server is provided all socket_options, allowing you to set the secure and tls_options options to create a secure server.

The web server passes all web_options, besides host and port, to the Thin backend via attribute writers, allowing you to set the ssl and ssl_options options.

Examples

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

License

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



pusher-fake Latest Version Build Status Dependency Status Code Climate Coverage Status

假冒推送器服务器进行开发和测试。

运行时,在两个随机打开的端口上启动完整的假服务。然后可以连接到服务,而不需要Pusher帐户。通过检查配置,可以找到套接字和Web服务器的主机和端口。

该项目旨在用本地版本完全替代Pusher服务,用于测试和开发。它不是替代生产用途!

Why?

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

用法

测试环境

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"
Zeus

需要自定义计划。有关必要的配置,请参阅示例计划

Other
require "pusher-fake/support/base"

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

开发环境

在Rails初始化程序中,或在加载期间执行的任何文件:

# Ensure it's only run in a development environment if it's a global file.
if Rails.env.development?
  # Ensure Pusher configuration is set if you're not doing so elsewhere.
  Pusher.app_id = "MY_TEST_ID"
  Pusher.key    = "MY_TEST_KEY"
  Pusher.secret = "MY_TEST_SECRET"

# Require the base file, which immediately starts the socket and web servers. # # If you're including this file in multiple processes, you may want to add an # additional check or even possibly hard code the socket and web ports. require "pusher-fake/support/base" end

如果您使用Foreman或类似的东西,您只需要为单个流程运行假冒软件:

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

客户

如果您正在创建一个 Pusher :: Client 实例,并希望使用假的,则需要提供选项。

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

二进制

如果您需要作为独立服务运行假冒,或许在使用Docker时,可以使用 push-fake 二进制文件。

$ pusher-fake –help
Usage: pusher-fake [options]
    -i, –id ID                      Use ID as the application ID for Pusher
    -k, –key KEY                    Use KEY as the key for Pusher
    -s, –secret SECRET              Use SECRET as the secret token for Pusher
        –socket-host HOST           Use HOST for the web socket server
        –socket-port PORT           Use PORT for the web socket server
    -v, –[no-]verbose               Run verbosely
        –web-host HOST              Use HOST for the web server
        –web-port PORT              Use PORT for the web server

配置

请注意,使用包含的支持文件时,应用程序ID,API密钥和令牌将自动设置为 Pusher 值。

设置

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.

用法

# Single setting.
PusherFake.configuration.verbose = true

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

SSL

WebSocket服务器提供了所有 socket_options ,允许您将 secure tls_options 选项设置为创建安全的服务器

Web服务器通过属性编写器将除代码和端口之外的所有 web_options 传递给Thin后端,允许您设置<代码> ssl 和 ssl_options 选项。

示例

  • 推荐假示例 - 使用RSpec推送假冒测试Rails应用程序的示例。

许可证

推荐假使用麻省理工学院的许可证。有关详细信息,请参阅许可证。




相关问题推荐