Module: RightHook::SpecHelpers

Includes:
Rack::Test::Methods
Defined in:
lib/right_hook/spec_helpers.rb

Overview

Helpers for specs! Typical usage is to include this module into your spec context, just like you would with Rack::Test::Methods.

Instance Method Summary collapse

Instance Method Details

#generate_secret_header(secret, body) ⇒ Object

:nodoc:



25
26
27
28
29
# File 'lib/right_hook/spec_helpers.rb', line 25

def generate_secret_header(secret, body)
  sha = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), secret, body)
  # GitHub sends it as 'X-Hub-Signature', but Rack provides it as HTTP_X_HUB_SIGNATURE... :/
  {'HTTP_X_HUB_SIGNATURE' => "sha1=#{sha}"}
end

#post_with_signature(opts) ⇒ Object

Post to the given path, including the correct signature header based on the payload and secret.

Parameters:

  • opts (Hash)

    The options to use when crafting the request.

Options Hash (opts):

  • :path (String)

    The full path of the endpoint to which to post

  • :payload (String)

    A JSON-parseable string containing a payload as described in developer.github.com/v3/activity/events/types/

  • :secret (String)

    The secret to use when calculating the HMAC for the X-Hub-Signature header



16
17
18
19
20
21
22
# File 'lib/right_hook/spec_helpers.rb', line 16

def post_with_signature(opts)
  path = opts.fetch(:path)
  payload = opts.fetch(:payload)
  secret = opts.fetch(:secret)

  post path, {payload: payload}, generate_secret_header(secret, URI.encode_www_form(payload: payload))
end