Module: FunctionsFramework::Testing

Extended by:
Testing
Included in:
Testing
Defined in:
lib/functions_framework/testing.rb

Overview

Helpers for writing unit tests.

Methods on this module can be called as module methods, or this module can be included in a test class.

Example

Suppose we have the following app that uses the functions framework:

# app.rb

require "functions_framework"

FunctionsFramework.http "my-function" do |request|
  "Hello, world!"
end

The following is a test that could be run against that app:

# test_app.rb

require "minitest/autorun"
require "functions_framework/testing"

class MyTest < Minitest::Test
  # Make the testing methods available.
  include FunctionsFramework::Testing

  def test_my_function
    # Load app.rb and apply its functions within this block
    load_temporary "app.rb" do
      # Create a mock http (rack) request
      request = make_get_request "http://example.com"

      # Call the function and get a rack response
      response = call_http "my-function", request

      # Assert against the response
      assert_equal "Hello, world!", response.body.join
    end
  end
end

Instance Method Summary collapse

Instance Method Details

#call_event(name, event) ⇒ nil

Call the given event function for testing. The underlying function must be of type :event or :cloud_event.



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/functions_framework/testing.rb', line 113

def call_event name, event
  function = ::FunctionsFramework.global_registry[name]
  case function&.type
  when :event, :cloud_event
    function.call event
    nil
  when nil
    raise "Unknown function name #{name}"
  else
    raise "Function #{name} is not a CloudEvent function"
  end
end

#call_http(name, request) ⇒ Rack::Response

Call the given HTTP function for testing. The underlying function must be of type :http.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/functions_framework/testing.rb', line 93

def call_http name, request
  function = ::FunctionsFramework.global_registry[name]
  case function&.type
  when :http
    Testing.interpret_response { function.call request }
  when nil
    raise "Unknown function name #{name}"
  else
    raise "Function #{name} is not an HTTP function"
  end
end

#load_temporary(path) ⇒ Object

Load the given functions source for the duration of the given block, and restore the previous status afterward.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/functions_framework/testing.rb', line 73

def load_temporary path
  registry = ::FunctionsFramework::Registry.new
  old_registry = ::FunctionsFramework.global_registry
  ::FunctionsFramework.global_registry = registry
  begin
    ::Kernel.load path
    yield
  ensure
    ::FunctionsFramework.global_registry = old_registry
  end
end

#make_cloud_event(data, id: nil, source: nil, type: nil, spec_version: nil, data_content_type: nil, data_schema: nil, subject: nil, time: nil) ⇒ FunctionsFramework::CloudEvents::Event

Make a simple CloudEvent, for passing to a function test. The event data is required, but all other parameters are optional (i.e. a reasonable or random value will be generated if not provided).



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/functions_framework/testing.rb', line 169

def make_cloud_event data,
                     id: nil, source: nil, type: nil, spec_version: nil,
                     data_content_type: nil, data_schema: nil, subject: nil, time: nil
  id ||= "random-id-#{rand 100_000_000}"
  source ||= "functions-framework-testing"
  type ||= "com.example.test"
  spec_version ||= "1.0"
  CloudEvents::Event.new id: id, source: source, type: type, spec_version: spec_version,
                         data_content_type: data_content_type, data_schema: data_schema,
                         subject: subject, time: time, data: data
end

#make_get_request(url, headers = []) ⇒ Rack::Request

Make a simple GET request, for passing to a function test.



132
133
134
135
136
# File 'lib/functions_framework/testing.rb', line 132

def make_get_request url, headers = []
  env = Testing.build_standard_env URI(url), headers
  env[::Rack::REQUEST_METHOD] = ::Rack::GET
  ::Rack::Request.new env
end

#make_post_request(url, data, headers = []) ⇒ Rack::Request

Make a simple POST request, for passing to a function test.



145
146
147
148
149
150
# File 'lib/functions_framework/testing.rb', line 145

def make_post_request url, data, headers = []
  env = Testing.build_standard_env URI(url), headers
  env[::Rack::REQUEST_METHOD] = ::Rack::POST
  env[::Rack::RACK_INPUT] = ::StringIO.new data
  ::Rack::Request.new env
end